Complete Communications Engineering

How Can I Start a Linux Thread using C? This can be done using the create_thread function from the pthread library.  This is a Linux system call that will create a thread in the current process.  Threads execute concurrently, either in OS-managed time slices or on separate processors.  The create_thread function takes an argument that tells which function the thread should run.  The function is passed as a function pointer.  The create_thread function returns a handle to the thread through another argument.  The handle type is pthread_t and it can be used to get information about the thread and control its behavior.  The following example code demonstrates using threads in a Linux C program:

#include <stdio.h>

#include <pthread.h>

#include <unistd.h>

#include <time.h>

 

void *lee_mow_lawn (void *param)

{

    printf(“Lee will mow the lawn.  It will take 25 minutes.\n”);

    usleep(25000000); /* In this universe, seconds are minutes */

    printf(“Lee has finished mowing the lawn.\n”);

    return NULL;

}

 

void *sam_trim_hedges (void *param)

{

    printf(“Sam will trim the hedges.  It will take 20 minutes.\n”);

    usleep(20000000); /* In this universe, seconds are minutes */

    printf(“Sam has finished trimming the hedges.\n”);

    return NULL;

}

 

void *alex_pull_weeds (void *param)

{

    printf(“Alex will pull weeds.  It will take 15 minutes.\n”);

    usleep(15000000); /* In this universe, seconds are minutes */

    printf(“Alex has finished pulling weeds.\n”);

    return NULL;

}

 

int main()

{

    pthread_t lee;

    pthread_t sam;

    pthread_t alex;

    time_t tstart, tend;

   

    tstart = time(NULL); /* In this universe, seconds are minutes */

 

    pthread_create(&lee, NULL, lee_mow_lawn, NULL);

    pthread_create(&sam, NULL, sam_trim_hedges, NULL);

    pthread_create(&alex, NULL, alex_pull_weeds, NULL);

   

    pthread_join(lee, NULL);

    pthread_join(sam, NULL);

    pthread_join(alex, NULL);

 

    tend = time(NULL); /* In this universe, seconds are minutes */

 

    printf(“All work was completed in %d minutes\n”, (int)(tend tstart));

    return 0;

}

The code first includes some standard headers that are required to define the functions used by the program, including the create_thread function.  After the includes, three thread functions are defined.  These functions will all run concurrently in separate threads.  The main function first declares three pthread_t handles, one for each thread, then defines a start and end time that will be used to time the threads.  The start time is recorded before the threads are started, and the end time is recorded after all the threads have finished.  Next, the three threads are created with three calls to pthread_create.  Each call receives one of the thread functions as its argument.  After the threads are created, the function pthread_join is used to wait for each thread to finish.  This function will not return until after the given thread has exited.  When all of the threads have exited, the end time is recorded and the final message is printed.  The expected output from this program is:

Lee will mow the lawn.  It will take 25 minutes.

Sam will trim the hedges.  It will take 20 minutes.

Alex will pull weeds.  It will take 15 minutes.

Alex has finished pulling weeds.

Sam has finished trimming the hedges.

Lee has finished mowing the lawn.

All work was completed in 25 minutes

Note that the total time taken is the same as the longest time taken by one of the threads.  This is expected for threads that run concurrently.