Solved

multithreaded mutex

Posted on 2001-06-06
6
412 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Author Comment

by:havman56
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now