/* threadex.c */ /* This version is used to test Dekkers's algorithm for N threads */ /* To compile (on a Solaris == SunOS 5.x machine ) */ /* cc threadex.c -lthread */ /* To run */ /* a.out 1000000 */ #include #include #include #define MAX_THREADS 16 /* Maximum possible number of threads */ #define NUM_THREADS 8 /* Number of threads to us in this run */ int stack[MAX_THREADS][4096]; /* Stacks for the new threads */ thread_t td[MAX_THREADS]; /* Thread descriptors for the new threads */ volatile int shared = 0; /* The shared variable being updated */ int loopcount = 100000; /* Default value for increment count */ #if 1 void dek_lock( int tid) {} void dek_unlock( int tid) {} #endif /* We will run instances of this function as separate threads */ void xthread( int id) /* Logical thread id.. values are 0, 1, 2,... */ { int i; int local; printf("Made it in %d \n", id); for (i = 0; i < loopcount; i++) { dek_lock(id); local = shared; local += 1; thr_yield(); shared = local; dek_unlock(id); } thr_exit((void *)0); } void main( int argc, char **argv) { int tid; int pri; int stat; thread_t gone; /* Place to return status of exiting threads */ int tstatus; /* Override loop count if user supplied one */ if (argc > 1) sscanf(argv[1], "%d", &loopcount); /* Test to see if thread support is alive and well */ stat = thr_setconcurrency(NUM_THREADS); printf("Set Concurrency Stat = %d \n", stat); stat = thr_getconcurrency(); printf("Concurrency = %d \n", stat); /* Create the threads */ stat = thr_min_stack(); printf("Min stack size = %d \n", stat); for (tid = 0; tid < NUM_THREADS; tid++) { stat = thr_create(stack[tid], sizeof(stack[0]), xthread, tid, THR_NEW_LWP, &td[tid]); printf("Create status = %d \n", tid); } /* Wait for all to terminate */ for (tid = 0; tid < NUM_THREADS; tid++) { stat = thr_join(td[tid], &gone, &tstatus); printf("Thread %d done \n", tid); } printf("Made it out \n"); printf("Value of shared is %d \n", shared); }