DEV Community

Cover image for Basics of multithreading in C
Nathanael Demacon
Nathanael Demacon

Posted on • Edited on

Basics of multithreading in C

C is a language that runs on one thread by default, which means that the code will only run one instruction at a time. In some cases you'll need to do multiple instructions at a time, a graphical interface for instance, will not stop when it performs an action related to a button's click. That's what we call multithreading, not to be confused with asynchronous operations that can be performed in only one thread that does multiple tasks.

What is a thread?

A thread is a task that runs linked to a process. A process we can have multiple threads and threads can run other threads and so on.

By default a process runs on a single thread. Each thread is new tasks that can be run indefinitely and in parallel to the other threads.

Creating a thread

Like said in the title, this post will talk about multithreading in C, so we will do C!

On POSIX operating systems, there is a library named pthread.h, which does exactly what it says, create threads! To use it under compilers, you'll need to link it with -lpthread argument (ex: gcc -lpthread main.c).



#include <pthread.h>
#include <unistd.h>
#include <stdio.h>

void
wait(void)
{
    sleep(2);

    printf("Done.\n");
}

int
main(void)
{
    pthread_t thread;
    int err;

    err = pthread_create(&thread, NULL, wait, NULL);

    if (err)
    {
        printf("An error occured: %d", err);
        return 1;
    }

    printf("Waiting for the thread to end...\n");

    pthread_join(thread, NULL);

    printf("Thread ended.\n");    

    return 0;
}


Enter fullscreen mode Exit fullscreen mode

The output will be:



Waiting for the thread to end...
Done. (~2 seconds after)
Thread ended.


Enter fullscreen mode Exit fullscreen mode

The main thread continued and printed a message while the created thread was operating and it's only one line to call a function in a new thread, pretty easy for C uh? Maybe you're a little lost, I will explain the code.

First of all, we include the pthread.h library, like said above, it contains all the functions needed to perform multithreading tasks. Then we include unistd.h which is containing the sleep() function. And then the stdio.h for printf().

pthread_create function is called to create the thread. It requires a pthread_t, which is the thread descriptor, a pointer to a void function and some other parameters that I will not describe here, the function's arguments for instance.

pthread_join is used to link the current thread process to another thread. It literally makes the program stops in order to wait for the end of the selected thread.


Mutex

Doing multiple operations on one target at the same time is very dangerous, the best example I can give is for databases. If three threads want to write a single file at the same time, it would be a problem because an hard drive can't go as fast as a CPU does. In this case we should lock the other threads in order to not overload the hard drive with tasks that can corrupt the file. Mutex can be used to lock the other threads.

In practice, we use mutex to tell if the task is locked or unlocked.



#include <pthread.h>
#include <unistd.h>
#include <stdio.h>

pthread_mutex_t lock;
int j;

void
do_process()
{
    pthread_mutex_lock(&lock);
    int i = 0;

    j++;

    while(i < 5)
    {
        printf("%d", j);
        sleep(1);

        i++;
    }

    printf("...Done\n");

    pthread_mutex_unlock(&lock);
}

int
main(void)
{
    int err;
    pthread_t t1, t2;

    if (pthread_mutex_init(&lock, NULL) != 0)
    {
        printf("Mutex initialization failed.\n");
        return 1;
    }

    j = 0;

    pthread_create(&t1, NULL, do_process, NULL);
    pthread_create(&t2, NULL, do_process, NULL);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    return 0;
}


Enter fullscreen mode Exit fullscreen mode

The output will be:



111111...Done
222222...Done


Enter fullscreen mode Exit fullscreen mode

So, as you can see, the thread t2 was done totally after t1, so the mutex worked! It's not the result of the pthread_join calls as they only change something for the current thread (here the main thread), you can try to join t2 before t1, it will perform the same thing.

Semaphores

Pretty much the same thing as the mutex, the semaphores are used to be controlled by any thread. The notion of ownership is not present in the case of a semaphore, so they can be locked and unlocked by any part of the program.


That was pretty easy don't you think? C functions are very powerful to perform such actions, a great power carries a great responsibility.

SuperMan

Top comments (17)

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

pthread is outdated since availability of C11 which introduced standard threading in C. The header files is <threads.h> with functions like thrd_create.

The standard functions for threading, conditions, and signalling, provide guarantees that pthreads cannot.

Collapse
 
quantumsheep profile image
Nathanael Demacon

I have my answers, threads.h is better to use even if it's not POSIX (stackoverflow.com/a/9377007)

Collapse
 
quantumsheep profile image
Nathanael Demacon

pthread.h is POSIX compliant, threads.h isn't.

But sure you can use it, it's implemented in linux and freeBSD kernels.

Collapse
 
noah11012 profile image
Noah11012

But threads.h is C11 compliant so by now ALL compilers have support for C11 at least for the three major ones:

MSVC
GCC
CLANG

Thread Thread
 
quantumsheep profile image
Nathanael Demacon

Yup, seems nice, their is just a lack of documentation, I wanted to know what did it really does.

Thread Thread
 
noah11012 profile image
Noah11012

You can find documentation here: en.cppreference.com/w/c/thread

Thread Thread
 
jl2210 profile image
JL2210

Well, it's the C libraries for two of those platforms that are supposed to implement threads.h (even though I'm pretty sure glibc doesn't), although I'm not sure what Windows does.

Collapse
 
jl2210 profile image
JL2210

It's not the kernels, it's the C libraries. You can have a kernel installed and not be able to do anything without a C library.

Collapse
 
phlash profile image
Phil Ashby

Thanks Nathanael! Nice starter article on pthreads :)

For the curious, Lawrence Livermore National Laboratory have this article with more background and examples of why you might use the various features available in pthreads: computing.llnl.gov/tutorials/pthre...

It's also a good idea to make sure you are using thread-safe library functions, here's a nice SO question and answer: stackoverflow.com/questions/125957...

Enjoy your full control of the CPU!

Collapse
 
quantumsheep profile image
Nathanael Demacon

Glab that you enjoyed it!

I'm starting to understand a lot about pure Computer Science since I got some courses about the theory of operating systems. It's so fascinating to learn how things really works beyond the compilers and why things are like that in programming languages!

Collapse
 
akahay159 profile image
Akshay Hiremath

Can you suggest that courses pls.

Thread Thread
 
quantumsheep profile image
Nathanael Demacon

For the theory of operating systems, I see this subject at my school (engineering school), but for programming languages I heard that Engineering a Compiler is very great when starting in this domain.

The Dragon Book is very good but much more advanced.

Collapse
 
ondrejs profile image
Ondrej

Thank god for every C-related article here (for us, who have interest in low-level programming). Thanks, Nathanael!

Collapse
 
quantumsheep profile image
Nathanael Demacon

C can be a very scary language at first but it's so captivating, I'm happy that you enjoyed reading this article 😄

Collapse
 
shreyosghosh profile image
Shreyos Ghosh • Edited

Hey, recently I worked with the C++ standard thread library and made a series about it, where I've talked about the mutexes and locks. I hope you'll find it useful. dev.to/shreyosghosh/series/20850

Collapse
 
jl2210 profile image
JL2210

Why can't you use a for loop? for( i = 0; i < 5; i++ )

Collapse
 
quantumsheep profile image
Nathanael Demacon

You can and should do a for loop. It's been 1 year since I did this article, I think it's time to rewrite it with my current knowledge :)