Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Posix/1.cpp

Go to the documentation of this file.
00001 /* cc thisfile.c -lthread -lpthread */
00002 #define _REENTRANT    /* basic 3-lines for threads */
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 *);   /* thread routine */
00012  
00013 int i, err;
00014 thread_t tid[NUM_THREADS];      /* array of thread IDs */
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':  /* POSIX */
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':  /* Solaris */
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 

Generated on Sun Oct 14 18:46:40 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001