00001
00002 #define _REENTRANT
00003
00004 #include <j2k/Fred/Standard.hpp>
00005 #include <pthread.h>
00006 #include <thread.h>
00007
00008 #define NUM_THREADS 5
00009 #define SLEEP_TIME 10
00010
00011 void* sleeping(void *);
00012
00013 int i, err;
00014 thread_t tid[NUM_THREADS];
00015
00016 int main(int argc, char *argv[])
00017 {
00018 if (argc == 1) {
00019 printf("use 0 as arg1 to use pthread_create()\n");
00020 printf("or use 1 as arg1 to use thr_create()\n");
00021 return (1);
00022 }
00023
00024 switch (*argv[1]) {
00025 case '0':
00026 for ( i = 0; i < NUM_THREADS; i++) {
00027 err = pthread_create(&tid[i], NULL, sleeping, (void*)i);
00028 }
00029 for ( i = 0; i < NUM_THREADS; i++) {
00030 err = pthread_join(tid[i], NULL);
00031 }
00032 break;
00033
00034 case '1':
00035 for ( i = 0; i < NUM_THREADS; i++)
00036 thr_create(NULL, 0, sleeping, (void*)i, 0, &tid[i]);
00037
00038 while (thr_join(NULL, NULL, NULL) == 0);
00039 break;
00040 }
00041
00042 printf("main() reporting that all %d threads have terminated\n", i);
00043 return (0);
00044 }
00045
00046 void* sleeping(void *arg) {
00047 int t = (int)arg;
00048 for(;;)
00049 printf( "%d", t );
00050
00051 return (NULL);
00052 }
00053