?
Solved

multithreaded mutex

Posted on 2001-06-06
6
Medium Priority
?
423 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
  • 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

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!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

850 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