Stupid problem with my simple "multithread" program..

Started by
7 comments, last by law_order 19 years, 4 months ago
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]
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.
Praise Gaia!
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...
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.
Free Mac Mini (I know, I'm a tool)
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.
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.
Free Mac Mini (I know, I'm a tool)
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.

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...
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.
Free Mac Mini (I know, I'm a tool)
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...

This topic is closed to new replies.

Advertisement