Context switching and mutex
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Context switching and mutex

  1. #1
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Smile Context switching and mutex

    Hello Guys I am new in multithreading programming.

    Lets says I have a situation like the following.

    global variable

    std::vector<int> vec;

    Thread 1

    Code:
     
    mutex.acquire();
     
    int size = vec.size(); //context switch to thread 2
     
    for(int i = 0; i<size;i++)
    {
      vec[i] = 10;
    }
    thread 2

    vec.erase(vec.first(), vec.first()+3);

    This can create a major havoc.. Is there any threading approache other than careful programming to solve this kind of problem..???
    Dont forget to rate my post if you find it useful.

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Context switching and mutex

    It depends on what the thread 2 is doing. Mutexes are guards that can help, but it needs all participants to be good citizens i.e. you have to think about what you want to guard as resources. So, in your case, vec seems to be a resource that needs to be shared and hence guarded. Hence, all those participants that need access to this resource SHOULD adhere to rules. In your case, it is insufficient to use a acquire in just thread1, but also in all other threads before they lay hands on vec.

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,553

    Re: Context switching and mutex

    Agreement with Kiran. There will not be "major havoc" if thread 2 also tries to acquire the same mutex as thread 1, before doing anything to the shared vec.

  4. #4
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Context switching and mutex

    (Everything Kirants and MikeAthon have said is right!!!)

    There are two basic approaches do dealing with multi-threading issues.

    1) Works by implementation/usage
    2) Works by design.

    In the first scenario, you use non-thread safe classes, and explicitly code the proper guards around the necessary items

    In the second scenarion, you develop robust classes that will give the proper behaviour in all multi-threaded environments.

    My experience has been that the first approach is the more common, but is definately mor fragile. It leaves lots of opportunities to miss things in the initial implementation, and for things to break in the face of changes that are unrelated to the actual problem.

    The second approach (which I use 90%+ of the time) is to develop classes which are throughly tested to be thread safe. These can then be utilized at the higher levels without having to woory about many of the details.

    note: there is actually a third approach which is gaining in popularity, and has some major advantages in multi-processor / multi-core systems (consider the next generation 16,24,32 core systems...). This approach dictates that ALL objects are immutable ALWAYS. An objects state is totally controlled by the constuctor, and NOTHING can change it once it is created.

    Since EVERYTHING (except object construction) is read-only, most of the issues simply disappear. And only one thread will be in the process of constructing a specific instance.

    This approach requires a MAJOR change in mindset and philosophy, so is normally not viable for existing applications. But the benefits are quite significant!!!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,553

    Re: Context switching and mutex

    Immutability is one of three patterns discussed in the October 2008 MSDN Magazine, for improving thread safety. In the order given in the article, the three patterns are: immutability, purity, isolation.

    Unfortunately, the article barely mentions these concepts before concluding.

    It's worth a read anyway, for the other points made by the article (whose title I really like): "Solving 11 Likely Problems In Your Multithreaded Code" by Joe Duffy at http://msdn.microsoft.com/en-us/magazine/cc817398.aspx

    Mike

  6. #6
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Context switching and mutex

    Quote Originally Posted by MikeAThon
    Immutability is one of three patterns discussed in the October 2008 MSDN Magazine, for improving thread safety. In the order given in the article, the three patterns are: immutability, purity, isolation.

    Unfortunately, the article barely mentions these concepts before concluding.

    It's worth a read anyway, for the other points made by the article (whose title I really like): "Solving 11 Likely Problems In Your Multithreaded Code" by Joe Duffy at http://msdn.microsoft.com/en-us/magazine/cc817398.aspx

    Mike
    That was an excellent article, and one can only hope that is is the first of a series.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center