LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


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

Rep: Reputation: 0
Unhappy pthread signal problem


Hi,

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:
.........
pthread_mutex_lock(myMutex);
.........
cout<<"thread A signals signal1"<<endl;
pthread_cond_signal(signal1);
cout<<"thread A waits for signal2"<<endl;
pthread_cond_wait(signal2);
.........
pthread_mutex_unlock(myMutex);
.........

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

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, 03:27 AM   #2
MartinN
Member
 
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
pthread_cond_wait(signal2);
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?

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

Original Poster
Rep: Reputation: 0
Martin,

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.)

Thanks,

Xin
 
  


Reply


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 03:50 PM
pthread signal problem again xinwang00 Programming 4 11-18-2003 03:21 PM
pthread problem robnob Programming 16 11-12-2003 08:49 PM
pthread signal problem xinwang00 Programming 12 11-11-2003 03:30 PM
pthread , signal/wait ldamian Programming 3 07-31-2003 06:49 PM


All times are GMT -5. The time now is 06:56 AM.

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