Solved

multithreaded mutex

Posted on 2001-06-06
6
415 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

778 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