Hi
I am looking to build an app to expand my coding skills that will likely include multithreading in the form of a 3d renderer.
I expect to have to learn a new language to achieve this. Currently I only know python.
C looks interesting but I'm unclear how well c lends itself to multithreading.
So, my question, is c a practical language to write multithreaded code?
Thanks
Michael
-
- Posts: 17
- Joined: Sat Oct 26, 2019 1:04 am
-
- Posts: 1270
- Joined: Tue Mar 20, 2018 9:53 pm
Re: multi threading in c
C programming and multiple threads are no problem at all.
Re: multi threading in c
Yes under linux you use pthreads
https://www.tutorialspoint.com/cplusplu ... eading.htm
https://www.tutorialspoint.com/cplusplu ... eading.htm
Re: multi threading in c
Do you still need pthreads?
See section 7.26 in the ISO standard.
See section 7.26 in the ISO standard.
Pi4 8GB and Pi4 4GB running Raspberry Pi OS 64-bit
Re: multi threading in c
Threads are pretty simple in C++. The compiler command line is in the code.
pi:~/bin$ mt
ababaabaabaabaabaabaabaabaabaabaabaaa
Code: Select all
#include <iostream>
#include <unistd.h>
#include <thread>
// g++ mt.c -o mt -lpthread
using namespace std;
void a() {
for (int i=0; i<25; i++) {
cout << "a";
cout.flush();
sleep(1);
}
}
void b() {
for (int i=0; i<12; i++) {
cout << "b";
cout.flush();
sleep(2);
}
}
int main(int argc,char *argv[]) {
thread a_thread(a);
thread b_thread(b);
a_thread.join();
b_thread.join();
cout << endl;
}
ababaabaabaabaabaabaabaabaabaabaabaaa
Re: multi threading in c
I still avoid C11 threads too many problems and different implementations. All supposed to being fixed in C2x lets see

Rather than me give you biased personal views you can just search for the official defect list at the standards committee and work out how big a problem it is to what you want to do.
-
- Posts: 35
- Joined: Tue Nov 10, 2020 9:12 am
- Location: Sardinia, Italy
Re: multi threading in c
If your purpose is exploiting multi-core capabilities of today CPUs, why don't you get the easiest way, i.e. C + OpenMP?
OpenMP comes with the C/C++ (but also Fortran) compiler and is as easy as
#include <omp.h>
int i;
# pragma omp parallel num_threads (4) // run with 4 threads
{
... block of code to be distributed among cores
}
Then compile with -openmp option (or equivalent, may vary for different compilers)
gcc my_test.c -openmp -O3 -o my_test
If you compile without the -openmp option the openmp pragmas are simply ignored and the program is executed single-thread
PS: the choice of C sounds good to me
OpenMP comes with the C/C++ (but also Fortran) compiler and is as easy as
#include <omp.h>
int i;
# pragma omp parallel num_threads (4) // run with 4 threads
{
... block of code to be distributed among cores
}
Then compile with -openmp option (or equivalent, may vary for different compilers)
gcc my_test.c -openmp -O3 -o my_test
If you compile without the -openmp option the openmp pragmas are simply ignored and the program is executed single-thread
PS: the choice of C sounds good to me
-
- Posts: 17
- Joined: Sat Oct 26, 2019 1:04 am
Re: multi threading in c
Thankyou all for your answers. I will go with C.
Michael
Michael