Threading in C

       

A thread is a single flow of control within a program.Thread is very much similar to a process.In fact thread is also called light-weight process.
What are the differences between process and thread?
Threads are not independent of one other like processes as a result threads shares with other threads their code section, data section and OS resources like open files and signals. But, like process, a thread has its own program counter (PC), a register set, and a stack space.
Why Multithreading?
Threads are popular way to improve application through parallelism. For example, in a browser, multiple tabs can be different threads. MS word uses multiple threads, one thread to format the text, other thread to process inputs, etc.
Threads operate faster than processes due to following reasons:
1) Thread creation is much faster.
2) Context switching between threads is much faster.
3) Threads can be terminated easily
4) Communication between threads is faster.
Can we write multithreading programs in C?
Unlike Java, multithreading is not supported by the language standard. POSIX Threads (or Pthreads) is a POSIX standard for threads. Implementation of pthread is available with gcc compiler. 
pthread_create : Create a new Thread
Syntax: int pthread_create(pthread_t *thread,const pthread_attr_t *attr,void *(*start  routine) (void*),void *arg)
   
Description:
    The pthread_create() function starts a new thread in the calling process.The new thread  stars execution by invoking start routine();
    arg is passed as the sole argument of start routine().

Return Value:
    On success pthread_create() return 0,On error it returns an error.

How to run programe of Thread:
                         gcc filename.c -o f1 -lpthread (for execution of programe)
                        ./ f1                 (to run programe)
                      where f1 is executable file name
Note:    we can also run this programe as
               gcc filename.c -lpthread(for execution)
            ./a.out(to run programe)
Example:
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
pthread_t tid,tid1;
    void* myfunction(void *arg)
    {   
    int ctr=0;
    for(ctr=1;ctr<=5;ctr++)
    {
    printf("Inside First Thread ctr=%d\n",ctr);
    sleep(5);
    }
    printf("Child thread completed ");
    return NULL;
    }
        void* myfunction1(void *arg)
        {   
        int ctr=0;
        for(ctr=1;ctr<=5;ctr++)
        {
        printf("Inside Second Thread ctr=%d\n",ctr);
        sleep(10);
        }
        printf("Child thread completed");
        return NULL;
        }
    int main()
    {
    int ctr,err,err1;
    err=pthread_create(&tid,NULL,&myfunction,NULL);
    if(err)
    {
    printf("Error in creating thread:%s",strerror(err));
    return 1;
    }
    err1=pthread_create(&tid1,NULL,&myfunction1,NULL);
    if(err1)
    {
    printf("Error in creating thread:%s",strerror(err1));
    return 1;
    }
    for(ctr=0;ctr<=5;ctr++)
    {
    printf("Insidse main thread :ctr=%d\n",ctr);
    }
    printf("Main Thread completed");
    }

Comments

Popular posts from this blog

Export data from mysql db to csv file using java

Secure Database Connectivity in node.js with mysql

Java Web Scraping