Jump to content
  • Advertisement
Sign in to follow this  
law_order

Stupid problem with my simple "multithread" program..

This topic is 5135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi people...i have a stupid problem with a very simple multithread program i created... Inside my "main" function(line 35),i have put a "printf("miracle");" statement.... However,the word "miracle" never appears... Here's the weird part..(at least for me...) When on line 20 ,i add the "scanf("%d", n);" command,then the previous "printf("miracle");" works fine!!! Could someone please explain me WHY that happens? Let me also say,that no matter WHERE i put my "print("miracle");" statement inside my "main",the outcome doesn't change... Any ideas please? Here's the code... ****************************************************************

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define  agents  5

#define  tickets 20

int sold=0;
int n;

pthread_mutex_t tick_lock = PTHREAD_MUTEX_INITIALIZER;




void* sell (void* data) {

int n_done = 1;

while(n_done) {
pthread_mutex_lock(&tick_lock);
if(sold < tickets) { sold++;  scanf("%d", &n); } //LINE 20:If i remove that "scanf",then my print on line 35 doesn't work!!!//

else {n_done = 0; }
pthread_mutex_unlock(&tick_lock); } }


main() {

int i=0;

pthread_t tas[agents];
void* returnv[agents];



printf("miracle");  /////MY "problematic" "printf"...

for(i=0; i<agents; i++) 
pthread_create(&tas, NULL, sell, NULL);

for(i=0; i<agents; i++)
pthread_join(tas, &returnv);

 }


[Edited by - law_order on November 26, 2004 5:35:10 PM]

Share this post


Link to post
Share on other sites
Advertisement
I am not familiar with pthread.h but i can assure you the problem is not with print or scanf. Something is hanging before the output buffer is flushed, ie somewhere in your code a mutex is never released. Your scanf is forcing the main thread to wait for input; without it execution is passed to another thread that isn't released so the buffer can flush.

Share this post


Link to post
Share on other sites
Quote:
Original post by body electric
I am not familiar with pthread.h but i can assure you the problem is not with print or scanf. Something is hanging before the output buffer is flushed, ie somewhere in your code a mutex is never released. Your scanf is forcing the main thread to wait for input; without it execution is passed to another thread that isn't released so the buffer can flush.



thanks a lot body electric for your answer.
i checked mycode again and again,but i can't find any mistake...
obviously something escapes from me..

i can't really underestand why "printf" doesn't work...

it is put even BEFORE any new threads except "main" are created...

any other help would be really appreciated...

Share this post


Link to post
Share on other sites
I ran this code on my MacOS X and Solaris boxen and could not reproduce your problem. I removed the scanf line and the printf was called as expected. This may be an implementation-specific issue.

Share this post


Link to post
Share on other sites
Quote:
Original post by igni ferroque
I ran this code on my MacOS X and Solaris boxen and could not reproduce your problem. I removed the scanf line and the printf was called as expected. This may be an implementation-specific issue.


really igni?

i run my code on Mandrake Linux 8.2 AND WINDOWS XP with Cygwin installed...

Unfortunately,i always had the same problem...

thanks a lot anyway igni.

oh...and i compiled my code with the gcc compiler using the "-lpthread" option.

Share this post


Link to post
Share on other sites
Just ran it on a Red Hat (kernel 2.2 I think) and HPUX box (with GCC and HP's C compiler). Worked fine all three times.

In the future when posting code, remember to use the [source][/source] tags.

Share this post


Link to post
Share on other sites
Original post by igni ferroque
Just ran it on a Red Hat (kernel 2.2 I think) and HPUX box (with GCC and HP's C compiler). Worked fine all three times.

In the future when posting code, remember to use the

tags.[/quote]

i'm sorry...i'll remember that...

i'd like to ask one more a question...

if i put the "printf" statement at the end of my code (after the "pthread_join" "for" loop),what result should i see?

should the word "miracle" appear or not?

thanks again...

Share this post


Link to post
Share on other sites
Quote:
Original post by igni ferroque
If you move the printf after the calls to pthread_join, printf won't be called until after the other threads finish. It will still be called.



that's what i also thought...
thanks a lot...again...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!