DEV Community

sofaki000
sofaki000

Posted on

Getting started with pthreads

  1. Installing pthreads library
  2. Compiling pthread programs
  3. Pthreads API

Installation

Installing pthreads library on linux:

sudo apt-get install libpthread-stubs0-dev


Compilation

You can compile threaded programms with gnu compiler with the following command:

1)

gcc -pthread file.c

or

2)

gcc -pthread file.c -o thread -lpthread

where:
-lpthread command will tell the compiler to execute the program with pthread.h library.
gcc is the compiler command.
file.c is the name of c program source file.
-o is option to make object file.
thread is the name of object file.


Pthreads API

All identifiers in the threads library begin with pthread_.

Some frequently used methods are:

1) pthread_ = Threads themselves and miscellaneous subroutines
2) pthread_attr_ = Thread attributes objects
3) pthread_mutex_ = Mutexes
4) pthread_mutexattr_ = Mutex attributes objects.

Also the pthread.h header file should be included in each source file using the Pthreads library.

Top comments (0)