Public Group

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.

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 <stdio.h>
#include <stdlib.h>

#define  agents  5

#define  tickets 20

int sold=0;
int n;

void* sell (void* data) {

int n_done = 1;

while(n_done) {
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; }

main() {

int i=0;

void* returnv[agents];

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

for(i=0; i<agents; i++)

for(i=0; i<agents; i++)

}


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

Share on other sites
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 on other sites
Quote:
 Original post by body electricI 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.

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 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 on other sites
Quote:
 Original post by igni ferroqueI 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 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 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 on other sites
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.

Share on other sites
Quote:
 Original post by igni ferroqueIf 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...

1. 1
2. 2
Rutin
20
3. 3
khawk
17
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 11
• Forum Statistics

• Total Topics
633758
• Total Posts
3013714
×