Skip to content
Advertisement

how to implement POSIX threads ( pthread.h ) on fedora 9

I need to use pthreads but it seems that I do not have it in my fedora and I cannot found how to install it. Thanks

Advertisement

Answer

Fedora 9 uses Linux Kernel version 2.6 and this version is fully compatible with libc 2.3.2. This libc contains the pthread.h header.

Check this implementation example.

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %ldn", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %dn", rc);
         exit(-1);
      }
   }

   /* Last thing that main() should do */
   pthread_exit(NULL);
}

And compile with:

gcc program.c -o program -lpthread
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement