Header file required for pthreads:
#include <pthread.h>
Types of threads
1. Kernel Level Threads
2. User Level Threads.
POSIX thread Library API:
int pthread_create( pthread_t *pthread, const pthread_attr_t *attr, void *( *start_routine) (void *), void *arg );
Function prototype for a thread:
int pthread_create( pthread_t *pthread, const pthread_attr_t *attr, void *( *start_routine) (void *), void *arg );
void * start_routine( void * arg )
{
//statements
}
How to Synchronization between threads
Now let’s see how to create a thread:
Below are the thread attributes
Below are the resources that are shared along with the process.
Below are the resources that are unique to a thread.
Example 1: Simple thread creation:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void * thread1()
{
printf("Hello from www.ProDeveloperTutorial.com\n");
}
int main()
{
int status;
pthread_t tid1;
pthread_create(&tid1,NULL,thread1,NULL);
return 0;
}
Example 2: Passing parameter to a thread
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>
//for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com
void *myFun(void *arg)
{
int *p; // Pointer to an integer variable
int x;
p = (int *) arg; // Casting "(void *)" type to "(int *)"
x = *p;
printf("\n The input parameter is %d", x );
return(NULL);
}
int main(int argc, char *argv[])
{
pthread_t tid;
int param;
param = 12345;
pthread_create(&tid, NULL, myFun, & param);
pthread_join(tid, NULL);
}
Output:
The input parameter is 12345
Joining and Detaching a thread:
Below is the API used to join a thread:
pthread_join (threadid,status):
pthread_join is used to wait till the child thread to terminate.
Below is the API used to detaching a thread:
pthread_detach (threadid) pthread_attr_setdetachstate (attr,detachstate) pthread_attr_getdetachstate (attr,detachstate)
pthread_detach is used to delete the thread resource once the execution has been completed.
Example 3: Simple thread creation with join system call
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>
//for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com
void *myFun(void *arg)
{
printf("\nHello World !\n");
return(NULL);
}
int main(int argc, char *argv[])
{
pthread_t tid;
if ( pthread_create(&tid, NULL, myFun, NULL) )
{
printf("\nCannot create thread");
exit(1);
}
printf("\nMain now waits for thread tid to finish....") ;
pthread_join(tid, NULL);
exit(0);
}
Output:
Main now waits for thread tid to finish….
Hello World !
Example 4: Simple thread creation with detach system call
By default when you create a thread without any attributes, it will create a join able threads. It means that, the resources acquired by that thread needs to be destroyed by other threads with pthread_join().
But if you make a thread as detached, then the resources are automatically freed upon termination.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>
//for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com
void *myFun(void *arg)
{
pthread_detach(pthread_self());
sleep(1);
printf("Thread Fn\n");
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t tid;
int ret = pthread_create(&tid, NULL, myFun, NULL);
if (ret != 0)
{
perror("Thread Creation Error\n");
exit(1);
}
printf("After thread created in Main\n");
pthread_exit(NULL);
}
Output:
After thread created in Main
Thread Fn