/* Threads example S. Murrell, 11-11-99 first downloading threads.h and threads.c, and this example test.c compile and run them like this: cc -c threads.c cc -c test.c cc -pthread threads.o test.o ./a.out When you write your own thread-using program in C++, you must use CC (capitals, not cc), but cc and CC should not be mixed, so you will need to recompile the library too: CC -c threads.c CC -c hw4.cpp CC -pthread threads.o test.o ./a.out these instructions are designed for rabbit. Other computers will probably differ. */ #include #include "threads.h" semaphore s; int total=0; void activity(int c) { int i; for (i=10; i>=0; i-=1) { usleep(random_in_range(100000,2000000)); grab(s); total+=1; release(s); printf("<%c%d>\n",c,i); } } void main(void) { int t1, t2; srandomdev(); s=create_semaphore(); t1=start_thread(activity, 'X'); t2=start_thread(activity, 'Y'); printf("main waiting\n"); usleep(random_in_range(2500000,5000000)); printf("main awake!\n"); wait_for_exit(t1); wait_for_exit(t2); printf("end, total=%d\n",total); }