Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

multithreaded mutex

Posted on 2001-06-06
6
Medium Priority
?
421 Views
Last Modified: 2012-06-27
hai experts,
bud in c in trouble here

i put down my question in following steps

1.i am having a class A which has vector of objects B.
2.i am having a global function i am accessing those vector objects b through objcet of A.
3.this global function is acced by some function through function pointer.
the accessing of global function is multithreaded.

then do i need to use any mutex inorder to protect my vector?

eg:
class B   class A
{         {
           m_vecorobj// vector for b objects
};        };

int foo()// global function
{
A a;
a.vectorobj.pushback();

};

int(*fptr)();
fptr=foo;

thread()
{
*fptr();
}

this thread function is called in a multithreaded way.

then do i need a mutex  before pushing or poping it!


how realy such situation works in Os level and memory managemnt.


i am very much thankful who give me answer with good explantion.

0
Comment
Question by:havman56
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 450 total points
ID: 6159287
>>then do i need a mutex  before pushing or poping it!

Well, not necessarily a mutex, but you do have to synchronize the access to the lists. I'd suggest using a critical section instead of a mutex, as these are much less a performance penalty.

Just do it like this:

class CCriticalSection
{
   public:

   CCriticalSection ()     { InitializeCriticalSection ( &m_cs);};
   ~CCriticalSection ()    { DeleteCriticalSection ( &m_cs);};

   void    Lock()          {   EnterCriticalSection    (   &m_cs);};
   void    Unlock()        {   LeaveCriticalSection    (   &m_cs);};

   private:

   CRITICAL_SECTION    m_cs;

};

CCriticalSection g_cs;

int foo()// global function
{
g_cs.Lock();
A a;
a.vectorobj.pushback();
g_cs.Unlock();
};

BTW: In YOUR sample, 'a' is local to 'foo()', so there is *actually* no need to protect access - however, it would make no sense to add an object to a list that is destroyed right afterwards, so I think that might just be a mistake...
0
 
LVL 2

Expert Comment

by:kejin
ID: 6160477
No, absolutely not. You do *not* need a mutex (or critical section) in your example's foo() to protect the 'a'. The automatic object 'a' is defined on the stack of function foo(). Each thread has its own stack and therefore access its own instance
of 'a'. Therefore, 'a' is not a critical section you need to protect. If you put 'a' in global memory area other than stack, for instance, you defined it as

foo()
{
   static A a;
   ....
}

Then, you do need a mutex (or critical section, or other similar) to protect because could be accessed by multiple threads concurrently.
0
 
LVL 2

Expert Comment

by:kejin
ID: 6160498
After posting the answer, I saw jkr already made the same comment
at the end of his followup. So, you should give your point to him, not me.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:havman56
ID: 6162883
thanks for your response JKR and kejin i feel my question is ambiqous sorry i would have posted it clearer ...

class B
{
};
class A
{
public:
B m_vectorobj;
};

A a;    //object of A is global

int foo()// global function
{
// do i need to create mutex and lock the vector
a.m_vectorobj.pushback();

};

this function gets called multi threaded fashion.

so do i need create a mutex..?

explain in detail how the memory managemnt done in this program

i am again repeating sorry for my mistake and also not accepting the answer since no memory managemnt explanation is given your answer..








0
 
LVL 86

Expert Comment

by:jkr
ID: 6163554
Please check my comment - I used a critical section instead of a mutex. It works mainly the same way, but uses less resources.
0
 
LVL 4

Author Comment

by:havman56
ID: 6184654
jkr

i realy understood the clear explantion provided by u but i need clear explnation of how memory mangaement takes place in threading so i awarded B grade.

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question