Operating System - OpenVMS
cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with simple threaded program

 
SOLVED
Go to solution
Alex Chupahin
Super Advisor

Problems with simple threaded program

Hello!
Please help to point me to my error:

This is simple code:

#include
#include
#define NUM 5

main()
{
pthread_t t1,t2;

void *print_msg(void *);

pthread_create(&t1,NULL, print_msg, (void *)"hello");
pthread_create(&t2,NULL, print_msg, (void *)"world\n");
pthread_join(t1,NULL);
printf("end1\n");
pthread_join(t2,NULL);
}

void *print_msg(void *m)
{
char *s = (char *)m;

printf("%s",s);
fflush(stdout);
sleep(1);

printf("%s",s);
fflush(stdout);
sleep(1);

printf("%s",s);
fflush(stdout);
sleep(1);

printf("%s",s);
fflush(stdout);
sleep(1);

printf("%s",s);
fflush(stdout);
sleep(1);

return NULL;
}

16 REPLIES 16
Alex Chupahin
Super Advisor

Re: Problems with simple threaded program

When I start it:
$ru simple

helloworld
helloworld
world
helloworld
helloworld
hello

and then program freezes. I can exit via ctrl-C only.
What I forget or doing wrong?
Jim_McKinney
Honored Contributor

Re: Problems with simple threaded program

I don't currently have access to a system to test but, from memory, I think you may need to call

pthread_exit(0);

just prior to your

return NULL;

in print_msg().
Alex Chupahin
Super Advisor

Re: Problems with simple threaded program

Thank you, but during experimentation, I have added pthread_exit function before return. Without success.
John Gillings
Honored Contributor

Re: Problems with simple threaded program

Alex,

what are you expecting the main program to do while the threads are running? I wouldn't expect this program to hang, I'd expect it to just exit (like the code says). Maybe you need a sleep before the end? First cut I'd add sleep(10) after the second pthread_join so you know the main thread will still be around when the threads complete. Better would be to use the pthread routine to synchronize with the completions of each executing thread.

What about the printf in main? Should that have an fflush too?
A crucible of informative mistakes
Alex Chupahin
Super Advisor

Re: Problems with simple threaded program

John I expect my program will be successfully finished and exit. But it doesnt. I should press ctrl-C to exit. printf() in main() between pthread_join is not working.
Hoff
Honored Contributor
Solution

Re: Problems with simple threaded program

LINK /THREAD=UP
Alex Chupahin
Super Advisor

Re: Problems with simple threaded program

Thank you, Hoff!
Ruslan R. Laishev
Super Advisor

Re: Problems with simple threaded program

Hi, Alex!

First at all you need to be ensure that the treads has been created with JOINABLE attributes (a default JOINABLE/DETACHED attribute depends frov OS version and platform).

I suggest to check threads state with SDA:
ana/sys
SDA>set proc /id=
SDA>pthread thread [-f]


sleep() - is not reenterable routine, you should use pthread_delay_np()
EdgarZamora_1
Respected Contributor

Re: Problems with simple threaded program

Hoff that was your shortest reply ever and it was worth 10 points a character! lol