Solved

multithreaded mutex

Posted on 2001-06-06
6
416 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 150 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

830 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