- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - OpenVMS
- >
- Re: Problems with simple threaded program
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2008 08:20 AM
тАО11-09-2008 08:20 AM
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;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2008 08:34 AM
тАО11-09-2008 08:34 AM
Re: Problems with simple threaded program
$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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2008 09:10 AM
тАО11-09-2008 09:10 AM
Re: Problems with simple threaded program
pthread_exit(0);
just prior to your
return NULL;
in print_msg().
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2008 09:36 AM
тАО11-09-2008 09:36 AM
Re: Problems with simple threaded program
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2008 12:19 PM
тАО11-09-2008 12:19 PM
Re: Problems with simple threaded program
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2008 12:24 PM
тАО11-09-2008 12:24 PM
Re: Problems with simple threaded program
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2008 03:23 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2008 11:14 PM
тАО11-09-2008 11:14 PM
Re: Problems with simple threaded program
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-10-2008 01:40 AM
тАО11-10-2008 01:40 AM
Re: Problems with simple threaded program
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()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-10-2008 06:26 AM
тАО11-10-2008 06:26 AM