DEV Community

Cover image for Basics of multithreading in C

Basics of multithreading in C

Nathanael Demacon on January 09, 2019

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...
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 :)