Threading in C Language with Linux (using GCC Complier)

Threading in C

In this chapter, you will learn about C Language Threading with GCC Linux with theory, syntax and examples.

Threads/ Processes are the mechanism by which you can run multiple code segments at a time, threads appear to run concurrently; the kernel schedules them asynchronously, interrupting each thread from time to time to give others chance to execute.

Identifying a Thread

Each thread identified by an ID, which is known as Thread ID. Thread ID is quite different from Process ID. A Thread ID is unique in the current process, while a Process ID is unique across the system.

Thread ID is represented by type pthread_t

Header file(s)

The header file which needs to be included to access thread functions

#include <pthread.h>

Creating a Thread ( pthread_create )

pthread_create is the function of pthread.h header file, which is used to create a thread. The syntax and parameters details are given as follows:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
    void *(*start_routine) (void *), void *arg);

pthread_t *thread

It is the pointer to a pthread_t variable which is used to store thread id of new created thread.

const pthread_attr_t *attr

It is the pointer to a thread attribute object which is used to set thread attributes, NULL can be used to create a thread with default arguments.

void *(*start_routine) (void *)

It is the pointer to a thread function; this function contains the code segment which is executed by the thread.

void *arg

It is the thread functions argument of the type void*, you can pass what is necessary for the function using this parameter.

int (return type)

If thread created successfully, return value will be 0 (Zero) otherwise pthread_create will return an error number of type integer.

Example of Threading in C Language

#include <stdio.h>
#include <pthread.h>
        
/*thread function definition*/
void* threadFunction(void* args)
{
    while(1)
    {
        printf("I am threadFunction.\n");
    }
}
int main()
{
    /*creating thread id*/
    pthread_t id;
    int ret;
    
    /*creating thread*/
    ret=pthread_create(&id,NULL,&threadFunction,NULL);
    if(ret==0){
        printf("Thread created successfully.\n");
    }
    else{
        printf("Thread not created.\n");
        return 0; /*return from main*/
    }
    
    while(1)
    {
        printf("I am main function.\n");      
    }
    
    return 0;
}

How to compile & execute thread program?

To compile
    $ gcc <file-name.c> -o <output-file-name> -lpthread
To run
    $ ./<output-file-name>

If file name is main.c while output file name is main

To compile
    $ gcc main.c -o main -lpthread
To run
    $ ./main

Output

Thread created successfully.
I am threadFunction.
I am threadFunction.
I am threadFunction.
I am threadFunction.
…
…
I am threadFunction.
I am main function.
I am main function.
I am main function.
I am main function.
…
…
I am main function.
I am threadFunction.

… and so on.


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.