LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2004, 05:28 PM   #1
sleepymish
LQ Newbie
 
Registered: Oct 2003
Posts: 16

Rep: Reputation: 0
POSIX Thread Termination confusion


Hi,

I have a question about POSIX threads.

Here is my program:

/******************************************************************/
/* Alert: link to the pthreads library by appending
-lpthread to the compile statement */
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h> /* for sleep */
#include <stdlib.h> /* for malloc */

#define NUM_THREADS 5

void *sleeping(void *); /* thread routine */
pthread_t tid[NUM_THREADS]; /* array of thread IDs */

int main()
{
int *sleeptime, i, retval;
for ( i = 0; i < NUM_THREADS; i++) {
sleeptime = (int *)malloc(sizeof(int));
*sleeptime = (i+1)*2;
retval =pthread_create(&tid[i], NULL, sleeping, (void *)sleeptime);
if (retval != 0) {
perror("Error, could not create thread");
}
}
for ( i = 0; i < NUM_THREADS; i++)
{
printf("joining %d\n", i);
pthread_join(tid[i], NULL);
}
printf("main() reporting that all %d threads have terminated\n", i);
return (0);
} /* main */

void *sleeping(void *arg)
{
int sleep_time = *(int *)arg;
printf("thread %d sleeping %d seconds ...\n", pthread_self(), sleep_time);
sleep(sleep_time);
printf("\nthread %d awakening\n", pthread_self());
pthread_exit(NULL);
// return (NULL);
}
/******************************************************************/

Here's the output:
$ ./test
thread 168100320 sleeping 2 seconds ...
thread 168100472 sleeping 4 seconds ...
thread 168101688 sleeping 6 seconds ...
thread 168101840 sleeping 8 seconds ...
joining 0
thread 168101992 sleeping 10 seconds ...

thread 168100320 awakening

$

/******************************************************************/
Now here's my question:

Why doesn't every single thread prints the "awakening" statement? I notice once one thread terminates or exits, all other threads go on business as usual but all the printf statements stop working in other threads. The reason I need these statements is to know what's going on inside each thread.

I would really appreciate it if someone can shed some light on this.

Regards,
Michelle
 
Old 12-05-2004, 02:39 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
On my machine it works. In fact, you need to wait for the awakening text of the threads, but it's not that long. BTW printtf run from many different threads may lead to problems, text written in wrong order etc (but it shouldn't be the case this time).
 
Old 12-05-2004, 08:16 PM   #3
sleepymish
LQ Newbie
 
Registered: Oct 2003
Posts: 16

Original Poster
Rep: Reputation: 0
Thank Mara. I found out what my problem was. There was nothing wrong w/ my code, it was the platform I was running. I was running in Cygwin and the version I have has problems with printfs in multi-threaded applications. I downloaded the lastest one and the problem is fixed.

Thanks!
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
POSIX shared memory confusion MeMooMeM Programming 0 05-25-2005 04:32 PM
POSIX thread Programming msriram_linux Programming 4 11-21-2004 03:14 PM
POSIX thread Ivan Lee Programming 2 03-28-2003 03:54 AM
RT Posix thread permissions spaceape Programming 0 01-23-2003 08:50 AM
posix thread sulo Programming 1 02-28-2002 07:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration