Threading in C/C++ programming language using pthreads

Utpal Kumar   2 minute read      

Using multiple threads in C for concurrent process flow

In this post, we will write a simple C script for performing a task concurrently using the POSIX pthread api in linux/unix like operating systems. For more details on concurrency, see my previous post about parallel computing in Python.

Simple script to make use of the pthread api

#include <pthread.h> //pthread
#include <unistd.h> //sleep
#include <stdio.h> // printf
#include <string.h> //strcpy

struct sData {char text[100];};


void *myfunc(void *voidData) {
    struct sData *data = voidData;
    for (int i=0; i<8; i++) {
        printf ("(i = %d) data text is %s \n", i, data->text);
        sleep(1);
    }
    return NULL;
}

void anotherfunc() {
    for (int i=0; i<3; i++) {
        sleep(1); //sleep for 1s
        printf("(i = %d) this is another func! \n", i);
    }
}

int main() {
    struct sData sd;
    int tret;
    pthread_t tid; // declare a new variable of type pthread_t tid to identify the thread in the system
    strcpy(sd.text, "earthinversion");
    tret = pthread_create(&tid, NULL, myfunc, &sd); // args: pointer to thread id, attributes, function name, arguments to this function
    anotherfunc();
    pthread_join(tid, NULL); 

    return 0;
}

In the main() function above, we define a data sd, and integer tret to store the output of the thread creation. We store text value in the sd.text, which will be argument for the myfunc. Then we create a variable tid of the data type pthread to identify the thread in the system. To create a thread, we need four arguments - pointer to the thread id, attributes (like detached state, scheduling policy, stack address, etc. Here, we specify it to be NULL, hence the default attributes will be used), name of the function and the argument to the function. Here, the function takes only one argument. If multiple arguments need to be given, then we need to use struct.

We then execute function anotherfunc() in the main thread. Since, the function anotherfunc() will finish before myfunc, we need to wait for the myfunc. To do this, we call the pthread_join. The second argument of the pthread_join is the return value of the thread. In this case, we declare it to be NULL.

Compilation

To compile the codes, we need to use the compiler with pthread library.

gcc threading.c -o threading

When you execute the threading, you should get similar return:

(i = 0) data text is earthinversion 
(i = 1) data text is earthinversion 
(i = 0) this is another func! 
(i = 2) data text is earthinversion 
(i = 1) this is another func! 
(i = 3) data text is earthinversion 
(i = 2) this is another func! 
(i = 4) data text is earthinversion 
(i = 5) data text is earthinversion 
(i = 6) data text is earthinversion 
(i = 7) data text is earthinversion

Further reads

  1. Thread functions in C/C++

Disclaimer of liability

The information provided by the Earth Inversion is made available for educational purposes only.

Whilst we endeavor to keep the information up-to-date and correct. Earth Inversion makes no representations or warranties of any kind, express or implied about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services or related graphics content on the website for any purpose.

UNDER NO CIRCUMSTANCE SHALL WE HAVE ANY LIABILITY TO YOU FOR ANY LOSS OR DAMAGE OF ANY KIND INCURRED AS A RESULT OF THE USE OF THE SITE OR RELIANCE ON ANY INFORMATION PROVIDED ON THE SITE. ANY RELIANCE YOU PLACED ON SUCH MATERIAL IS THEREFORE STRICTLY AT YOUR OWN RISK.


Leave a comment