Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 11-06-2003, 09:14 PM   #1
LQ Newbie
Registered: Oct 2003
Posts: 12

Rep: Reputation: 0
Unhappy pthread signal problem


I am working on a pthread project and this problem really confuses me. It seems that the broadcast doesn't wake up all waiting threads. Below is the pseudocode (it has been simplified a lot to show the problem):

Thread A:
cout<<"thread A signals signal1"<<endl;
cout<<"thread A waits for signal2"<<endl;

Thread B:
cout<<"thread B waits for signal1"<<endl;
(if I add "sleep(1)" here, then the bug is gone)
cout<<"thread B broadcasts signal2);

The calling order is:
thread B waits for signal1
thread A signals signal1
thread A waits for signal2
thread B broadcasts signal2 // this broadcast doesn't wake up thread A!

If let B sleeps for 1 sec before broadcast (as commented above in the code), then the bug is gone. Also the bug seems only happen when I run it on a single processor machine.

Do you have any clue why this happens? Thank you ver much!
Old 11-07-2003, 02:27 AM   #2
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
There is a forum for programming questions where you could possibly get better answers. I have to admit I'm a little out of training on threads.

From your description of the problem, I'd say you have a race condition. There is no guarantee that your "Thread A" really does wait on signal 2. Even though you have a trace print-out just above the line
there is nothing that says that this line is executed.
It could be that thread A's execution is interrupted between the print-out and the waiting. Then thread B sends the broadcast before thread A waits for it.

The more I think about it, the more likely this scenario seems to me. Do you agree?

Old 11-07-2003, 12:05 PM   #3
LQ Newbie
Registered: Oct 2003
Posts: 12

Original Poster
Rep: Reputation: 0

Thanks for your reply!

It seems to me that after thread B awakens by signal1 of thread A, since thread A holds myMutex, thread B must immediately block on the myMutex. Thread A continues until it waits for signal2 and release myMutex. THen thread B gets the chance to run. I think by the time thread B broadcasts, thead A should have already been waiting.

Also if it is the scenario as you said, the problem should not be solved by letting thread B sleep(1) before broadcast.

(Please note that I only have this problem when I run it on a single processor machine. No problem if run on 2-CPU machine.)




Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Possible pthread permission problem Zotty Programming 1 06-05-2005 02:50 PM
pthread signal problem again xinwang00 Programming 4 11-18-2003 02:21 PM
pthread problem robnob Programming 16 11-12-2003 07:49 PM
pthread signal problem xinwang00 Programming 12 11-11-2003 02:30 PM
pthread , signal/wait ldamian Programming 3 07-31-2003 05:49 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:10 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration