Skip to content
System Programming
Create a thread

Creating a thread

The sample demonstrates how to create a thread using pthread_create() function and wait for its completion.

#include <pthread.h>
#include <iostream>
#include <sys/types.h>
 
// the function that is going to run in a separate thread
// as per the requirement of the library, it should accept a single argument
// of type void* and return void* from the function
void* threadFunction(void* arg){
	
    // we will make the thread to work infinitely 
    while(true){
 
        // always print + symbol
        std::cout << "+";
    }
 
    // nothing to return
    return NULL;
}
 
 
int main(){
 
    // printing a message from the main thread
    std::cout << "Hello from main thread... " << std::endl;
 
    // the variable will hold the thread identifier we are going to create
    pthread_t tid;
 
    // create thread to run threadFunction routine, the identifier of the thread
    // will be stored in the variable tid that is passed by reference in this example
    int threadCreated = pthread_create(&tid, NULL, threadFunction, NULL); 
 
    // check if created successfully
    if(threadCreated != 0){
        std::cerr << "Could not create new thread" << std::endl;
        return threadCreated;
    }
 
    // after thread is created, we will infinitely print - symbol from the main thread
    while(true){
        std::cout << "-";
    }
 
    // we will never reach here as there is an infinite loop, however, in general we 
    // should wait for the thread's completion.
 
    // return value of thread
    void* retval;
 
    // join the thread and get its return value
    int joinResult = pthread_join(tid, &retval);
 
    return 0;
}

The file can be compiled and executed as follows:

g++ thread.cpp -pthread -o thread
./thread

The program is supposed to print + and - signs without any particular order as follows:

Hello from the main thread...
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-++-++-+---+-+-+-+++---+-+-+-++-+-+-++-+---+-+--+---+--+-++-+-+--+-++-+-+-+-+-++-+-+-+-+-+-++-+-+---+-+-+-+-+-+-+-+-+---+-+-+-+--+--+-+-++-+-+--+-+-+--+-+-+---+---+--+-+-+-++-++-+--+-+-+-+-+-+--+--+-++-++-++-+---+-+-+---+-+-+--+-+-+-++-+-+-+++-+-+-++-++-+-+++-++-+-++-+-+++-+++++++-+-+-------------------------------------------------------------------------------------------------------+-+-+--+-+-+++-+-++++-+-+-+-+-+-+-++++-+++++-++++-+-+-+-+-+--++-+-+---------------------------------------------------------------------------------------------------------------------+--+-+-+-+----+-+-+-+----+--+-++-++-+++-+-+-+--+-+++-++-+-+++-++-+--++-+++-++-+--+-+-+-++-+-++-+-+-+--+-++-+-+-+-----+-+-+-+-+---+--+-++-++-+---+-+-++-+--+-++-+++-+-+--+-+-+-++-+-+-+-+-+-+-+-+-++-+--+-++-+-+-+-+-+-++-+-+-+-+-+-+-+--------------------------------------------------+-++-+-+-+-+-++++-+-+-+-+-+-+--+-+++-+--+-+++-+++-+++++-++++-++++-+++++-+-+-+-+-+-++-+++-++++-+-+-++-+-+-+++-+-+-+-++++-++++-+-+-+-+-+-+--+--+-++-+++-+--+-+++-+-+-+---+-+-+-+-+++-+++-+-+-+-+-+++-++++-+-+-+----+-+-+-+-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-+-+-+-+-++++-+-+----+-+-+++-+-+-+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---+--+-+-+-+-+--+-+-+++-++++-+++-+-+-+-+-+-++-+-+++-++++-++++-++++-+-+-+-++-+-+-++++-++-+-+-+-++-++++-+---+-+-+-+-+-+-+-++++-++++-+-++-+-+-+-+-+-+++-+-+-+--+-+++++-+++-++++-++-+-+--+-++-+++-+----+-+-+-+-+-+-+-+-+-+++++-+-+-+-+-+-+-+-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++