How to Create Thread in Linux
We know about thread in Linux. In this tutorial, I will explain how to create thread in Linux with the help of a programming example. Thread must be executed in process. It is much similar to a process. We can say that thread is light weight process and shares the same address space, data and attribute of process. Thread is single sequence stream within the process. It is like some mathematical or some small function of code which is run within the process. Refer to the tutorial Thread in Linux.
Fig. 1: Overview of Thread in LINUX
The POSIX thread libraries are available which include different functions regarding the thread. It is a standard library named <pthread.h>.
You can create a new thread by pthread_t data type.
System Call pthread_create ( )
System call pthread_create ( ) creates a new thread in the current process.
Synopsis of function is:
int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
Execution of new thread starts by invoking start_routine ( ) and arg is passing argument of routine. New thread name is pointed by thread and the attr argument points to a pthread_attr_t structure whose contents are used to determine attributes for the new thread. The thread is created with default attributes if attr is NULL. If pthread_create ( ) is successfully called, it stores the ID of new thread in buffer pointed to thread.
The system call pthread_create() returns zero if new thread is successfully created and it returns error number if not created due to any one of the following errors:
EAGAIN – Requirement of resources are not fully satisfied to create another thread.
EAGAIN – This error will occur when the number of thread creation limits reaches at threshold level.
EINVAL – It indicates the attr is not valid for creating new thread
EPERM – No permission to set the scheduling policy and parameters specified in attr.
Let’s explain how to create thread by system call thread_create ( ) and how to work upon it. Here I have described a simple demo of creation of thread in c programming. If you are unaware about the basics of C language, refer to the tutorial how to make first C programming in Linux.
void *eventToPrint( void *str );
int result1 ,result2;
char *msg = “Welcome User”;
pthread_t thread1, thread2;
result1 = pthread_create (&thread1, NULL, eventToPrint,(void*) msg);
if (result1 == 0)
printf(“n Thread 1 created successfullyn”);
perror(“Sorry….Thread 1 is not created!!!n”);
result2 = pthread_create (&thread1, NULL, eventToPrint, “Hello Engineersgarage”);
if (result2 == 0)
printf (“n Thread 2 created successfullyn”);
perror(“Sorry….Thread 2 is not created!!!n”);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
void *eventToPrint( void *str )
display = (char *) str;
printf(“%s n”, display);
Save the file in directory and compile it. After the successful compilation, run the executable file from command terminal. You may refer to the tutorial how to make first C programming in Linux if you not aware of compilation and execution process.
Here I have created two new threads for display message. First thread displays “Welcome User” and second thread displays “Hello Enigneersgarage” on screen. Before calling pthread_ create ( ), I have assigned address to thread by thread_t data type. When pthread_create ( ) is calling it will be passed by first argument pthread_t *thread and it will store the ID in address space.
The function eventToPrint ( ) displays the thread message on screen. Message is displayed by routine function eventToPrint ( ) which is passed as sub routine argument in system call. I declared the message of first thread as outside of system call and passed by reference. Message of second thread is directly defined in argument arg with system call pthread_create ( ).
If first thread is created successfully, it returns zero and displays the following message in screen:
Thread 1 created successfully
It displays the error number with following message if creation of thread is failed:
Sorry….Thread 1 is not created!!!
Same thing happens when second thread is created.
The system call pthread_join ( ) waits for completion of the task of thread. If you do not join the thread, it terminates as soon as it is completed. If pthread_join ( ) is cancelled to join thread, it returns zero and current thread remains joined. I attached both the threads by pthread_join ( ) waiting for one thread when another thread is being executed.
Output of above code will be printed on screen as follows:
Thread 1 created successfully
Thread 2 created successfully
Filed Under: Tutorials