This program creates a single extra
thread, shows that it is sharing variables with the original thread,
and gets the new thread to return a result to the original thread.
Multithreaded programs don't get much simpler than this.
Lets look the function which create the
thread. pthread_create creates a new thread,much as fork creates a
new process.
#include<pthread.h> int pthread_create(pthread_t *thread, pthread_attr_t *attr, void(*start_routine)(void *), void *arg);
The first argument is a pointer to
pthread_t . When a thread is created, an identifier is written to the
memory location to which this variable points.This identifier enables
you to refer to the thread . The next argument sets the thread
attributes. You do not usually need any special attributes, and you
can simply pass NULL as this argument. Later is the chapter you will
see how to use these attributes. The final two arguments tell the
thread the function that it is to start executing and the arguments
that are to be passed to this function.