Multithreading in C Tutorial- Part 1

What is a Thread?

Thread is a smallest sequence of instructions that can be executed independently by the scheduler.Process is nothing but multiple threads of execution running asynchronously.

We will start with creating a thread in C,the API used to create a thread is pthread_create.The arguments to this function are

First argument :      pthread_t *thread ,(Thread id)
Second Argument: const pthread_attr_t *attr ,(Attributes if any)
Third Argument: void *(*start_routine)(void *) ,(the function which should be called)
Fourth Argument: void *arg,(Arguments to the function)

We will see a simple example code here:

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


void *ThreadFn(void *arg)
{
sleep(1);
printf("In Thread Fn\n");
return NULL;
}

int main(int argc, char *argv[])
{

pthread_t tid;
printf("Before Thread\n");
pthread_create(&tid, NULL, ThreadFn, NULL);
/*
  *pthread_join blocks the calling thread until the
* thread with identifier equals to the first argument
* terminates
  */
pthread_join(tid, NULL);
printf("After Thread\n");
return 0;
}

In order to compile this application you have to pass -lpthread as an argument to gcc

gcc 1.c -0 1 -lpthread

O/P: Before Thread
        In Thread Fn
        After Thread

Example of Passing an argument to the thread:

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

struct test{
int a;
int b;
};

void *myThreadFn(void *arg)
{
struct test *ptr = (struct test *)arg;
sleep(1);
printf("In Thread Fn with ptr->a:%d and ptr->b:%d\n",ptr->a, ptr->b);
return NULL;
}



int main(int argc, char *argv[])
{
struct test hello = {1,2};
pthread_t tid;
printf("Before Thread\n");
pthread_create(&tid, NULL, myThreadFn,(void *) &hello);
/*
  *pthread_join blocks the calling thread until the
* thread with identifier equals to the first argument
* terminates
  */
pthread_join(tid, NULL);
printf("After Thread\n");
return 0;
}

O/P:
 Before Thread
In Thread Fn with ptr->a:1 and ptr->b:2
After Thread

Example of Getting the return value from pthread_join:

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


void *thread_fn(void *arg)
{

char *str = (char *)arg;
printf("Thread Fn:%s\n", str);
return (void *)strlen(str);
}


int main(int argc, char *argv[])
{

pthread_t tid;
void *res;
int ret;

ret = pthread_create(&tid, NULL, thread_fn, "Hello World");
if (ret != 0)
printf("Error in creating Thread\n");

pthread_join(tid, &res);
printf("Thread returned :%ld\n", (long)res);

return 0;

}

O/P:
 Thread Fn:Hello World
Thread returned :11

Example Code to check whether created thread is running in the same process or different process

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



void* threadFn(void *arg)
{
printf("%s:%d\n",__func__, getpid());
pthread_exit(NULL);
}



int main(int argc, char *argv[])
{

pthread_t tid;
int ret = pthread_create(&tid, NULL, threadFn, NULL);
if (ret != 0){
perror("Error Creating Thread\n");
exit(1);
}
pthread_join(tid, NULL);
printf("%s:%d\n",__func__, getpid());
pthread_exit(NULL);

}

O/P:
threadFn:11651
main:11651

You cannot call pthread_join on currently running thread for example you cannot do pthread_join(pthread_self()),it will cause a deadlock and the function will return EDEADLK.


Tip:
To know which implementation of POSIX is supported in your system.In terminal type the following:
getconf GNU_LIBPTHREAD_VERSION

Comments

Popular posts from this blog

bb.utils.contains yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig

PR, PN and PV Variable in Yocto