Thread: first ownership of a mutex

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    35

    first ownership of a mutex

    How can I give the first ownership of a mutex to a thread?

    I mean, I have two threads in my applications. thread 2 must wait for thread1 but it doesn't because thread2 runs before and takes the control, so how can I give the control to the other the first time?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How about setting the mutex to a "wait" state in the main thread, before thread2 is even started?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    35
    in the main I create the mutex: CreateMutex(NULL,FALSE,NULL) I think with the second parameter to FALSE the main thread doesn't take the mutex the first time. After that I create the second thread and in its function wait for this mutex but this thread is the first so it takes the mutex.
    in the main function I call and another function at the same time and wait for the mutex too, but I need that this function takes the mutex first...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you need a bit of handshaking between the main thread and the first thread, as well as the second thread. You set the main thread as owner of the mutex, then wait for a mutex from the first thread to say "I'm ready to start work". At that point you release the Thread2 mutex in the main thread.

    Alternatively, create thread2 in thread1, after thread1 has taken ownership of the mutex.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    35
    thread1 is my main function...

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by radeberger View Post
    thread1 is my main function...
    So, where's the problem?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by radeberger View Post
    How can I give the first ownership of a mutex to a thread?

    I mean, I have two threads in my applications. thread 2 must wait for thread1 but it doesn't because thread2 runs before and takes the control, so how can I give the control to the other the first time?
    Mutexes are for mutual exclusion, not sequencing.

    If thread1 is creating thread2, then why not lock the mutex before creating thread2, guaranteeing it can't get it first.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    35
    The thing is that with semaphore I can control what thread begins first but with mutex I don't know how I can do it.
    I have two threads:
    The first thread is the main function, so it starts first. Than it creates another thread. Afterwards the first thread must sleep 2 seconds because the thread 2 must open a connection and then both have a wait for the mutex. I wanted that thread 1 takes the mutex before. I wanted to know how can I do it.
    |
    is it possible that thread one first take the mutex and then second thread is created??

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Thread1 taking the mutex first is exactly what setting the second parameter to TRUE means when you create the mutex. And yes, that's what I suggested you do.

    And sleeping for 2 seconds is definitely a bad plan of action. If thread2 has to do something before thread1 can do something, then thread2 should use some sort of IPC (Semaphore for example) to indicate the success of it's connecting somewhere. What if your connecting somewhere suddenly takes 1.5x or 10x the original time (due to something outside of your applicaiton) - you would need to change your application.

    Sure, you may want to put a timeout on waiting for the event so that your applicaiton can say "Couldn't connect", rather than waiting forever. But a fixed 2 second delay is not the right thing!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    35
    I tried what you say and it works but I tried other thing too: I create the mutex in the second thread function with the second parameter TRUE and the main function takes it. Is that because main function thread has priority? I thought the thread that create the mutex should own it...

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by radeberger View Post
    I tried what you say and it works but I tried other thing too: I create the mutex in the second thread function with the second parameter TRUE and the main function takes it. Is that because main function thread has priority? I thought the thread that create the mutex should own it...
    It certainly says that if you pass TRUE as the second parameter, the mutex is owned by the current thread immediately. If you pass in FALSE, then it just creates a mutex - it is not owned by any thread, so first to request it will get it, and hold it until it's released. Which thread runs when is hard to determine, especially if there is more than one CPU.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. mutex problem
    By radeberger in forum C++ Programming
    Replies: 16
    Last Post: 03-20-2009, 04:24 PM
  3. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  4. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  5. Mutex - location of lock/unlock
    By cjschw in forum C++ Programming
    Replies: 1
    Last Post: 08-14-2003, 12:28 PM