Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Silly Darwin pthreads question



Hello, all ...

Does pthread_exit() behave differently on Darwin / Mac OS X than it does on, say, LinuxPPC, with regards to main()? As an example, consider the following:

*****************************

#include <pthread.h>
#include <stdio.h>
#include <strstream.h>

void * Hello(void *pThreadID)
{
printf("Hello %d\n", pThreadID);
pthread_exit(0);
}

int main(int argc, char *argv[])
{
pthread_t threads[5] = {0};

for (int iIndex = 0; iIndex < 5; iIndex ++)
{
printf("Creating thread %i\n", iIndex);

int iOK = pthread_create(&threads[iIndex], NULL, Hello, (void *)iIndex);

if (iOK != 0)
{
printf("error\n");
break;
}
}

pthread_exit(0);
}

***************

Now, the pthread documentation i've read indicates that, if main() exits with pthread_exit(), that it waits for all spawned threads to exit before doing the exit itself. On LinuxPPC, this seems to be the case, i.e. always see the "Hello 4" message before exiting. On Mac OS X 10.1.3, though, the last thread (Hello 4) often doesn't seem to get a chance to run before main() exits, as evidenced by the last message displayed sometimes being "Hello 3" ... no "Hello 4" anywhere.

Anyone care to explain this? I'm certainly not a pthread pro, and i'm still pretty new to Darwin, so i'm not adverse to the premise that John Knows Not What He Does ... still, this seems like a simple enough thing, i'd expect this to work equally across any platform which does pthreads ...

Regards,

John

Falling You - exploring the beauty of voice and sound
http://www.mp3.com/fallingyou
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.