Unix Technical Forum

posix threads

This is a discussion on posix threads within the comp.unix.bsd.openbsd.misc forums, part of the OpenBSD category; --> hi i wrote a programm using libpthread unter obsd 3.3. i create two threads but only one of them ...


Go Back   Unix Technical Forum > Unix Operating Systems > OpenBSD > comp.unix.bsd.openbsd.misc

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-16-2008, 05:35 AM
Stephan Sa?
 
Posts: n/a
Default posix threads

hi

i wrote a programm using libpthread unter obsd 3.3.

i create two threads but only one of them is running:

--- snip of source ---
pthread_t th1, th2;
char *mes1="Hello";
char *mes2="World";

if(pthread_create(&th1, NULL, print_message, mes1)!=0)
perror("pthread_create");
pthread_detach(th1);

if(pthread_create(&th2, NULL, print_message, mes2)!=0)
perror("pthread_create");
pthread_detach(th2);
--- snap -------------

the print_message function looks like this:

void *print_message(void *ptr)
{
char *mes=(char*)ptr;
printf("%s\n", mes);
return NULL;
}

If i compile the source with

gcc -o thread thread.c -lpthread

it produces no errors or warnings. but if i execute the binary,
i only get the output "Hello" from the first thread:

$ ./thread
Hello

If i put a sleep(1) behind every pthread_create()-call, i get
the right output:

$ ./thread
Hello
World

But why did this source need a sleep()-call?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-16-2008, 05:35 AM
Eric Enright
 
Posts: n/a
Default Re: posix threads

On Thu, 06 May 2004 07:10:02 -0700, Stephan Sa? wrote:

> --- snip of source ---
> pthread_t th1, th2;
> char *mes1="Hello";
> char *mes2="World";
>
> if(pthread_create(&th1, NULL, print_message, mes1)!=0)
> perror("pthread_create");
> pthread_detach(th1);
>
> if(pthread_create(&th2, NULL, print_message, mes2)!=0)
> perror("pthread_create");
> pthread_detach(th2);
> --- snap -------------

<snip>
> it produces no errors or warnings. but if i execute the binary,
> i only get the output "Hello" from the first thread:
>
> $ ./thread
> Hello
>
> If i put a sleep(1) behind every pthread_create()-call, i get
> the right output:
>
> $ ./thread
> Hello
> World
>
> But why did this source need a sleep()-call?


I have to guess without seeing the rest of the code, but does the program
exit shortly after the last pthread_detach() ? If so, then the reason why
you never see the second thread's output is because your program
terminates before it ever gets a chance to run.

One solution you could consider is not using pthread_detach(), and then
using pthread_join() on each of the threads after you spawn the last one
-- that will ensure that your program doesn't return until the threads are
finished.

HTH,
--
Eric Enright /"\
sauronAtiptsoftDcom \ / ASCII Ribbon Campaign
X Against HTML E-Mail
Public Key: 0xBEDF636F / \

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-16-2008, 05:36 AM
M Khomo
 
Posts: n/a
Default Re: posix threads

Eric Enright wrote:
> On Thu, 06 May 2004 07:10:02 -0700, Stephan Sa? wrote:
>
>
>>--- snip of source ---
>>pthread_t th1, th2;
>>char *mes1="Hello";
>>char *mes2="World";
>>
>>if(pthread_create(&th1, NULL, print_message, mes1)!=0)
>> perror("pthread_create");
>>pthread_detach(th1);
>>
>>if(pthread_create(&th2, NULL, print_message, mes2)!=0)
>> perror("pthread_create");
>>pthread_detach(th2);
>>--- snap -------------

>
> <snip>
>
>>it produces no errors or warnings. but if i execute the binary,
>>i only get the output "Hello" from the first thread:
>>
>>$ ./thread
>>Hello
>>
>>If i put a sleep(1) behind every pthread_create()-call, i get
>>the right output:
>>
>>$ ./thread
>>Hello
>>World
>>
>>But why did this source need a sleep()-call?

>
>
> I have to guess without seeing the rest of the code, but does the program
> exit shortly after the last pthread_detach() ? If so, then the reason why
> you never see the second thread's output is because your program
> terminates before it ever gets a chance to run.
>
> One solution you could consider is not using pthread_detach(), and then
> using pthread_join() on each of the threads after you spawn the last one
> -- that will ensure that your program doesn't return until the threads are
> finished.
>
> HTH,

Another possibility is for print_message to flush its print buffer.
Likewise, sight unseen.

MK

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-16-2008, 05:36 AM
Peter Strömberg
 
Posts: n/a
Default Re: posix threads

Der_gruene_Punkt@gmx.de (Stephan Sa?) wrote in
news:1ef9cfaa.0405060610.e0924ed@posting.google.co m:

> ...
> If i compile the source with
>
> gcc -o thread thread.c -lpthread
> ...


read gcc-local(1), use gcc -pthread instead and try again

--
Peter Strömberg
C2K2 C2K3 ISCCIV02 ISCCIV03
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:03 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com