michael9000
Posts: 17
Joined: Sat Oct 26, 2019 1:04 am

multi threading in c

Mon Nov 30, 2020 2:04 am

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

DarkElvenAngel
Posts: 1270
Joined: Tue Mar 20, 2018 9:53 pm

Re: multi threading in c

Mon Nov 30, 2020 2:23 am

C programming and multiple threads are no problem at all.

LdB
Posts: 1664
Joined: Wed Dec 07, 2016 2:29 pm

Re: multi threading in c

Mon Nov 30, 2020 2:27 am


User avatar
jahboater
Posts: 6530
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: multi threading in c

Mon Nov 30, 2020 2:30 am

Do you still need pthreads?
See section 7.26 in the ISO standard.
Pi4 8GB and Pi4 4GB running Raspberry Pi OS 64-bit

knute
Posts: 649
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: multi threading in c

Mon Nov 30, 2020 2:50 am

Threads are pretty simple in C++. The compiler command line is in the code.

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;
}
pi:~/bin$ mt
ababaabaabaabaabaabaabaabaabaabaabaaa

LdB
Posts: 1664
Joined: Wed Dec 07, 2016 2:29 pm

Re: multi threading in c

Mon Nov 30, 2020 3:04 am

jahboater wrote:
Mon Nov 30, 2020 2:30 am
Do you still need pthreads?
See section 7.26 in the ISO standard.
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.

illecitnom
Posts: 35
Joined: Tue Nov 10, 2020 9:12 am
Location: Sardinia, Italy

Re: multi threading in c

Mon Nov 30, 2020 2:29 pm

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

michael9000
Posts: 17
Joined: Sat Oct 26, 2019 1:04 am

Re: multi threading in c

Mon Nov 30, 2020 10:11 pm

Thankyou all for your answers. I will go with C.

Michael

Return to “C/C++”