Operating System - OpenVMS
1828352 Members
3166 Online
109976 Solutions
New Discussion

Re: 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
Alex Chupahin
Super Advisor

Re: Problems with simple threaded program

Spasibo, Ruslan!
Eto tozhe vazhno.
Ruslan R. Laishev
Super Advisor

Re: Problems with simple threaded program

Hi, Alex!

Using link/thread=(upcalls[,mult]) is not essential for normal behaviour of threads. :-)
Hoff
Honored Contributor

Re: Problems with simple threaded program

If we're exchanging aphorisms, remember that thread-safe is an oxymoron. And that per-thread security probably isn't.

And slightly more seriously, having $hiber and $wake flying around in any application can snarl an AST-driven or a thread-based program.
Ruslan R. Laishev
Super Advisor

Re: Problems with simple threaded program

Agreed. :-)
GuentherF
Trusted Contributor

Re: Problems with simple threaded program

Without the LINK/THREAD=UPCALLS the the threads interfere with each others sleep(). Eventually one $WAKE is lost and the process hange in HIB. Check the return value from sleep(). It's not always 0 meaning the sleep() returned earlier. That's the $WAKE (part of the sleep() implementation) that finally is missing.

/Guenther
Hoff
Honored Contributor

Re: Problems with simple threaded program

Back when I was more regularly tossing $hiber and $wake calls and ASTs around (and this was back in the era when the debugger itself would interfere with $hiber and $wake), a periodic $schdwk was a handy thing to have chugging along in the background. This was a deliberately-induced spurious wake-up, and it did tend to unwedge things until you could figure out what really happened.

(This all seems so primitive now. Ah, well.)

Willem Grooters
Honored Contributor

Re: Problems with simple threaded program

$HIBER / $WAKE in a multi-threaded program: Isn't that asking for trouble? IIRC, it used to be discouraged; but things may have chnaged in the years...
Willem Grooters
OpenVMS Developer & System Manager