Thread: Questions about Multiple Threads and Sleep

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    47

    Questions about Multiple Threads and Sleep

    I have an assignment, not asking you to do it for me just a question, where I have to schedule people using a restroom and each person is a thread. I need to have each thread "use" the restroom for x seconds and I am tring to use the wait function to put the thread to sleep. The problem is that it appears that sometimes after a sleep all threads stop responding and the program appears to deadlock and at other times it just stops for 30 - 90 seconds and then everything just piles in like it was still working but not outputting. Ideas?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    how do you synchronize the usage of the "restroom" by threads?

    Show the code of the thread main function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Quote Originally Posted by vart View Post
    how do you synchronize the usage of the "restroom" by threads?

    Show the code of the thread main function
    I am using mutex locks and ocndition variables. I am 100% sure that it is not deadlocking because of a thread holding a lock. I have debug output and its like ALL threads have died.

    In the function with sleep it will make it to person sleeping but when it hangs I never get the person waking up debug. For some reason the thread will not wake up. Again this doesn't happen all the time and sometimes it just sleeps for up to 10 times longer than it should

    Main function of thread
    Code:
    void *OnePerson(void *threadarg) {      
       struct person *newPerson;
       newPerson = (struct person *) threadarg;
       const int use_fac_time=5;  
       int gender = newPerson->gender ;
       int id = newPerson->id;
        
       Arrive(id,gender);
       UseFacilities(id,gender,use_fac_time);
       Depart(id,gender);
    }//end OnePerson
    Thread with sleep
    Code:
    void UseFacilities(int id, int gender, int time){      
      
       printf("Person using restroom:%d\nGender:%d\nNumber in Restroom:%d\nMen Waiting:%d\nWomen Waiting:%d\n"
       "-------------------------------------\n",id,gender,num_in_restroom,num_men_waiting,num_women_waiting);    
       printf("Person %d sleeping\n", id);
       sleep(time);
       printf("Person %d waking\n", id);
    }//end UseFacilities
    Last edited by BENCHMARKMAN; 02-24-2008 at 12:25 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Post some code!

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Ok. With the code posted I do not see problems
    Here is the next question
    Could you show the Arrive,Depart function (they do locking /unlocking - Am I right?)

    And the code where you allocate the threadarg parameter
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Yes, the arrive/depart do the locking but if the thread goes to sleep shouldn't it wake up? Its not waking up. There are no locks between the printf("Person %d sleeping\n", id); and printf("Person %d waking\n", id); If that part of the code is not waiting on a lock how could a lock effect it waking up?

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Question. Could it have anything to do with the server I am running it on. I am ssh into a university mainframe. If the load was extremely high that could cause the "strange" anomolies I have seen? I just tried running it 10 times now and there has not been a single problem.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You probably have race conditions. Post your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    You really think I would have a race condition when it when it runs 0/20 times and now I have successfully run it 20/20 times. I think it was a server load issue. I'm going to continue testing though.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes. It's a race. Sometimes it happens, sometimes it doesn't. That's why it's called race condition.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Just because you call printf() doesn't mean that you're going to see something on the screen.

    http://www.cppreference.com/stdio/fflush.html

    gg

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    6
    Also, it's standard convention to use stderr for debug/warning/alert/etc. output as it's a dedicated output stream and it's unbuffered by default. Use fprintf().

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Quote Originally Posted by Elysia View Post
    Yes. It's a race. Sometimes it happens, sometimes it doesn't. That's why it's called race condition.
    But lets say you are right that there is a race condition how is that going to stop a thread from waking? If the thread goes to sleep he should wake up. I underestand the concept that a race condition doesn't always happend but when a program fails 20 times in a row and then 5 hours later has worked flawlessly 50 + times leads me to believe it was network load earlier.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But race conditions can be at their peak when there is load.
    Say thread A does some for and enters a critical section named AA.
    At the same time, thread B is inside critical section AA and performing some work, but before it leaves, it waits for an event named BB.
    Thread A waits for thread B to exit the critical section AA. Inside that critical section AA, thread A signals the event BB that would release thread B.
    Thread B would then exit critical section AA and allow thread A to continue.

    But unfortunately, this doesn't happen. Thread A comes to the critical section AA, then falls asleep since thread B is inside it. Thread B then waits for an event to be signaled, so it falls asleep, inside the critical section AA, but that event will never be signaled, because thread A will signal it, but it's asleep. So you get a problem.

    Now, what if A actually gets to the critical section first? Then everything is fine since it can signal the event BB and thread B can proceed as normal.
    When there's high load, threads can get less time, and it's a bigger chance thread B might get there before thread A.

    It's called a race condition.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  2. sleep() and detached threads
    By karas in forum Linux Programming
    Replies: 7
    Last Post: 02-19-2006, 07:20 PM
  3. Sleep works with just one thread, but not 2
    By finkus in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2005, 09:17 PM
  4. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM