On Linux, you can create and manage threads in C/C++ using the POSIX thread (pthread) library. Unlike other operating systems, there is little difference between a thread and a process in Linux. That's why Linux often refers to its threads as light-weight processes.

Using the pthread library, you can create threads, wait for them to terminate, and terminate them explicitly.

The History of Thread Usage on Linux

Before Linux version 2.6, the main thread implementation was LinuxThreads. This implementation had significant limits in terms of performance and synchronization operations. A limit on the maximum number of threads that could run restricted them to in the 1000s.

In 2003, a team led by developers from IBM and RedHat succeeded in making the Native POSIX Thread Library (NPTL) project available. It was first introduced in RedHat Enterprise version 3 to resolve performance issues with the Java Virtual Machine on Linux. Today, the GNU C library contains implementations of both threading mechanisms.

Neither of these is an implementation of green threads, which a Virtual Machine would manage and run in purely user mode. When you use the pthread library, the kernel creates a thread each time a program starts.

You can find thread-specific information for any running process in the files under /proc/<PID>/task. This is the standard location for process information under the procfs Linux standard. For single-thread applications, it will appear that there is a task record with the same value as the PID under this directory.

Working Logic of Threads

Threads are like processes currently running on the operating system. In single-processor systems (e.g. microcontrollers), the operating system kernel simulates threads. This allows transactions to run simultaneously through slicing.

A single-core operating system can only really run one process at a time. However, in multi-core or multi-processor systems, these processes can run simultaneously.

Thread Creation in C

You can use the pthread_create function to create a new thread. The pthread.h header file includes its signature definition along with other thread-related functions. Threads use the same address space and file descriptors as the main program.

The pthread library also includes the necessary support for mutex and conditional operations required for synchronization operations.

When you’re using the functions of the pthread library, you must ensure the compiler links the pthread library into your executable. If necessary, you can instruct the compiler to link to the library using the -l option:

        gcc -o test test_thread.c -lpthread
    

The pthread_create function has the following signature:

        int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)

It returns 0 if the procedure is successful. If there is a problem, it returns a non-zero error code. In the above function signature:

  • The thread parameter is of type pthread_t. The created thread will always be accessible with this reference.
  • The attr parameter lets you specify custom behavior. You can use a series of thread-specific functions starting with pthread_attr_ to set up this value. Possible customizations are the scheduling policy, stack size, and detach policy.
  • start_routine specifies the function that the thread will run.
  • arg represents a generic data structure passed to the function by the thread.

Here’s an example application:

        #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
 
void *worker(void *data)
{
    char *name = (char*)data;
 
    for (int i = 0; i < 120; i++)
    {
        usleep(50000);
        printf("Hi from thread name = %s\n", name);
    }
 
    printf("Thread %s done!\n", name);
    return NULL;
}
 
int main(void)
{
    pthread_t th1, th2;
    pthread_create(&th1, NULL, worker, "X");
    pthread_create(&th2, NULL, worker, "Y");
    sleep(5);
    printf("Exiting from main program\n");
    return 0;
}
Output from program showing two threads running simultaneously

Thread Types

When a thread returns from the main() function in an application, all threads terminate and the system frees up all resources the program used. Likewise, when exiting any thread with a command like an exit(), your program will terminate all threads.

With the pthread_join function, you can wait for a thread to terminate instead. The thread using this function will block until the expected thread terminates. The resources they use from the system are not return even in cases such as the termination of joinable threads, unscheduled by the CPU, or even failing to join with ptread_join.

Sometimes there are situations where joining with pthread_join does not make sense; if it’s impossible to predict when the thread will end, for example. In this case, you can ensure that the system returns all resources automatically at the point where the thread returns.

To achieve this, you should start the relevant threads with the DETACHED status. When starting a thread, DETACH status can be set via a thread attribute values or with the pthread_detach function:

        int pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int pthread_detach(pthread_t thread);

Here’s an example use of pthread_join(). Replace the main function in the first program with the following:

        int main(void)
{
    pthread_t th1, th2;
    pthread_create(&th1, NULL, worker, "X");
    pthread_create(&th2, NULL, worker, "Y");
    sleep(5);
    printf("exiting from main program\n");
    pthread_join(th1, NULL);
    pthread_join(th2, NULL);
    return 0;
}

When you compile and run the program, your output will be:

        Hi from thread Y
Hi from thread X
Hi from thread Y
...
Hi from thread Y
exiting from main program
Hi from thread X
...
Hi from thread X
Thread X done!
Hi from thread Y
Thread Y done!

Thread Termination

You can cancel a thread with a call to pthread_cancel, passing the corresponding pthread_t id:

        int pthread_cancel(pthread_t thread);

You can see this in action in the following code. Again, only the main function is different:

        int main(void)
{
    pthread_t th1, th2;
    pthread_create(&th1, NULL, worker, "X");
    pthread_create(&th2, NULL, worker, "Y");
    sleep(1);
    printf("====> Cancelling Thread Y!!\n");
    pthread_cancel(th2);
    usleep(100000);
    printf("====> Cancelling Thread X!\n");
    pthread_cancel(th1);
    printf("exiting from main program\n");
    return 0;
}
Output from a program showing threads running and then being cancelled

Why Are Threads Created?

Operating systems always try to run threads on one or more CPUs, either from a self-created list or from a user-created thread list. Some threads cannot run because they are waiting for an input/output signal from the hardware. They may also be waiting voluntarily, waiting for a response from another thread, or have another thread blocking them.

You can adjust the resources you allocate to threads that you create using pthread. This can be a custom scheduling policy, or you can choose scheduling algorithms such as FIFO or Round-robin if desired.