// CarbonThreadManager.c
   
#include <pthread.h>
#include <mach/mach.h>
#include <CoreServices/CoreServices.h>
   
#define MAXTHREADS 8
   
static ThreadID mainThread;
static ThreadID newThreads[MAXTHREADS] = { 0 };
   
voidPtr
threadFunction(void *threadParam)
{
    int i = (int)threadParam; 
   
    printf("thread #%d: CTM %#08lx, pthread %p, Mach %#08x\n",
           i, newThreads[i], pthread_self(), mach_thread_self());
   
    if (i == MAXTHREADS)
        YieldToThread(mainThread);
    else
        YieldToThread(newThreads[i + 1]);
   
    /* NOTREACHED */
    printf("Whoa!\n");
   
    return threadParam;
}
   
int
main()
{
    int   i;
    OSErr err = noErr;
   
    // main thread's ID
    err = GetCurrentThread(&mainThread);
   
    for (i = 0; i < MAXTHREADS; i++) {
   
        err = NewThread(
                  kCooperativeThread, // type of thread
                  threadFunction,     // thread entry function
                  (void *)i,          // function parameter
                  (Size)0,            // default stack size
                  kNewSuspend,        // options
                  NULL,               // not interested
                  &(newThreads[i]));  // newly created thread
   
        if (err || (newThreads[i] == kNoThreadID)) {
            printf("*** NewThread failed\n");
            goto out;
        }
   
        // set state of thread to "ready"
        err = SetThreadState(newThreads[i], kReadyThreadState, kNoThreadID);
    }
   
    printf("main: created %d new threads\n", i);
   
    printf("main: relinquishing control to next thread\n");
    err = YieldToThread(newThreads[0]);
   
    printf("main: back\n");
   
out:
   
    // destroy all threads
    for (i = 0; i < MAXTHREADS; i++)
        if (newThreads[i])
            DisposeThread(newThreads[i], NULL, false);
   
    exit(err);
}

haidragondeMacBook-Pro:7-37 haidragon$ ls
CarbonThreadManager	CarbonThreadManager.c
haidragondeMacBook-Pro:7-37 haidragon$ ./CarbonThreadManager
main: created 8 new threads
main: relinquishing control to next thread
thread #0: CTM 0x700008e69000, pthread 0x700008e69000, Mach 0x002903
thread #1: CTM 0x700008eec000, pthread 0x700008eec000, Mach 0x001903
thread #2: CTM 0x700008f6f000, pthread 0x700008f6f000, Mach 0x001a03
thread #3: CTM 0x700008ff2000, pthread 0x700008ff2000, Mach 0x001b03
thread #4: CTM 0x700009075000, pthread 0x700009075000, Mach 0x002503
thread #5: CTM 0x7000090f8000, pthread 0x7000090f8000, Mach 0x002403
thread #6: CTM 0x70000917b000, pthread 0x70000917b000, Mach 0x001e03
thread #7: CTM 0x7000091fe000, pthread 0x7000091fe000, Mach 0x001f03
main: back
haidragondeMacBook-Pro:7-37 haidragon$ gcc -Wall -o CarbonThreadManager CarbonThreadManager.c  -framework Carbon