Menu

URGENT: Mutex not working?

Help
2002-05-28
2002-05-28
  • Anders Backman

    Anders Backman - 2002-05-28

    Hi all.

    I have a problem.

    The following code:

    ost::Mutex m;

    m.enterMutex();
    m.enterMutex();

    should lock on the second call, right?

    Well It doesnt under Linux nor under Windows.
    Im using the latest release 2.0.95
    Im I lost here or?

    The following works:

    ost::Event e;
    e.reset();
    e.wait();

    It locks alright.

     
    • Nick Liebmann

      Nick Liebmann - 2002-05-28

      No. It wont block on the second call, unless the second call is made from a different thread. Mutexes are designed to prevent different threads accessing the same data, concurrently.
      Depending on the type of mutex created, you may have to LeaveMutex() the same number of times as you entered it (per thread) before another thread will be able to enterMutex on it.

      So you code probably just locks the mutex twice, and another thread will block on the mutex until the first thread unlocks it twice.

      Events are different babies altogether. The code (calling wait) will block until another thread signals the Event.
      You might want to use this in a thread that runs an endless loop, but to save processor power, waits on the event, before continuing its loop.The event would be signalled when another thread determines that the waiting thread has something to do, i.e. process a message!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.