C - Thread                                        Home : www.sharetechnote.com

 

 

 

 

Thread

 

 

 

How to Create and Run a Thread ?

 

 

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <unistd.h>

 

 

void *SampleThread(void *vargp)

{

    int i = 0;

    

    printf("SampleThread is running ... \n");

    for(i = 0; i < 15; i++) {

        sleep(1);

        printf("timer running inside thread = %d\n", i);

    }

    printf("SampleThread is exiting ... \n");

    return NULL;

}

  

int main()

{

    int i = 0;

    

    pthread_t tid;

    pthread_create(&tid, NULL, SampleThread, NULL);

    

    for(i = 0; i < 5; i++) {

        sleep(2);

        printf("timer running outside thread = %d\n", i);

    }

    

    // this is to make it sure that the program (Application) waiting until the tid thread

    // completes. without this routine, the application move to the next step right away

    // exit(0) in this example.

    pthread_join(tid, NULL);

    

    exit(0);

}

 

Result :-----------------------------------------------

// What I am trying to show you is that printf() outside of thread is running even while printf() within the thread is running. The part in red is from inside of Thread and the part in black is from outside of Thread.

 

SampleThread is running ...

timer running inside thread = 0

timer running outside thread = 0

timer running inside thread = 1

timer running inside thread = 2

timer running outside thread = 1

timer running inside thread = 3

timer running inside thread = 4

timer running outside thread = 2

timer running inside thread = 5

timer running inside thread = 6

timer running outside thread = 3

timer running inside thread = 7

timer running inside thread = 8

timer running outside thread = 4

timer running inside thread = 9

timer running inside thread = 10

timer running inside thread = 11

timer running inside thread = 12

timer running inside thread = 13

timer running inside thread = 14

SampleThread is exiting ...

 

 

 

Running Two Threads

 

 

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <unistd.h>

 

void *SampleThread1(void *vargp)

{

    int i = 0;

    

    printf("SampleThread(1) is running ... \n");

    for(i = 0; i < 10; i++) {

        sleep(1);

        printf("timer running inside SampleThread(1) = %d\n", i);

    }

    printf("SampleThread(1) is exiting ... \n");

    return NULL;

};

 

 

void *SampleThread2(void *vargp)

{

    int i = 0;

    

    printf("SampleThread(2) is running ... \n");

    for(i = 0; i < 15; i++) {

        sleep(1);

        printf("timer running inside SampleThread(2) = %d\n", i);

    }

    printf("SampleThread(2) is exiting ... \n");

    return NULL;

};

 

  

int main()

{

    int i = 0;

    

    pthread_t tid1, tid2;

    pthread_create(&tid1, NULL, SampleThread1, NULL);

    pthread_create(&tid2, NULL, SampleThread2, NULL);

    

    for(i = 0; i < 7; i++) {

        sleep(2);

        printf("timer running outside thread = %d\n", i);

    }

    printf("timer outside Thread is ended ..\n");

    

    pthread_join(tid1, NULL);

    pthread_join(tid2, NULL);

    

    exit(0);

}

 

 

Result :-----------------------------------------------

 

SampleThread(1) is running ...

SampleThread(2) is running ...

timer running inside SampleThread(1) = 0

timer running inside SampleThread(2) = 0

timer running outside thread = 0

timer running inside SampleThread(2) = 1

timer running inside SampleThread(1) = 1

timer running inside SampleThread(2) = 2

timer running inside SampleThread(1) = 2

timer running outside thread = 1

timer running inside SampleThread(2) = 3

timer running inside SampleThread(1) = 3

timer running inside SampleThread(2) = 4

timer running inside SampleThread(1) = 4

timer running outside thread = 2

timer running inside SampleThread(2) = 5

timer running inside SampleThread(1) = 5

timer running inside SampleThread(2) = 6

timer running inside SampleThread(1) = 6

timer running outside thread = 3

timer running inside SampleThread(2) = 7

timer running inside SampleThread(1) = 7

timer running inside SampleThread(2) = 8

timer running inside SampleThread(1) = 8

timer running outside thread = 4

timer running inside SampleThread(2) = 9

timer running inside SampleThread(1) = 9

SampleThread(1) is exiting ...

timer running inside SampleThread(2) = 10

timer running outside thread = 5

timer running inside SampleThread(2) = 11

timer running inside SampleThread(2) = 12

timer running outside thread = 6

timer outside Thread is ended ..

timer running inside SampleThread(2) = 13

timer running inside SampleThread(2) = 14

SampleThread(2) is exiting ...