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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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? |
| |||
| 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 / \ |
| |||
| 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 |
| ||||
| 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 |