Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c language
concurrency
threading
multitasking

How to create a simple thread in C

Educative Answers Team
svg viewer

A program can be quickly executed by introducing concurrency using threads. Each thread can execute part of the same task (e.g., summing elements of an array), or it can allocate the same task for different clients in a client-server architecture.

Syntax

1. Header file

Include the header file pthread.h.

#include <pthread.h>

2. The ID of a thread

Each thread has an object of type pthread_t associated with it that tells its ID. The same pthread_t object cannot be used by multiple threads simultaneously. For multiple threads, an array can be created where each element is an ID for a separate thread:

pthread_t id[2];

3. Creating a thread

A thread is created and starts using the function pthread_create(). It takes four parameters:

Name Type Description
ID pthread_t * Reference (or pointer) to the ID of the thread.
Attributes pthread_attr_t * Used to set the attributes of a thread(e.g., the stack size, scheduling policy, etc.) Passing NULL suffices for most applications.
Starting routine void * The name of the function that the thread starts to execute. If the function’s return type is void *, then its name is simply written; otherwise, it has to be type-cast to void *.
Arguments void * This is the argument that the starting routine takes. If it takes multiple arguments, a struct is used.

The return type of a starting routine and its argument is usually set to void *.

pthread_create(&id[0], NULL, printNumber, &arg);

4. Exiting a thread

pthread_exit() is used to exit a thread. This function is usually written at the end of the starting routine. If a value is returned by a thread upon ending, its reference is passed as an argument. Since a thread’s local variables are destroyed when they exit, only references to global or dynamic variables are returned.

Starting routine example

// Global variable:
int i = 1;

// Starting routine:
void* foo(void* p){
    int i = *(int*) p;
    printf("Received value: %i", i);
    
    // Return reference to global variable:
    pthread_exit(&i);
}

5. Waiting for a thread

A parent thread is made to wait for a child thread using pthread_join(). The two parameters of this function are:

Name Type Description
Thread ID pthread_t The ID of the thread that the parent thread waits for.
Reference to return value void ** The value returned by the exiting thread is caught by this pointer.
int* ptr;
pthread_join(id, &ptr);

Demo

1 of 13

Code

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

// Global variable:
int i = 2;

void* foo(void* p){
  // Print value received as argument:
  printf("Value recevied as argument in starting routine: ");
  printf("%i\n", * (int*)p);

  // Return reference to global variable:
  pthread_exit(&i);
}

int main(void){
  // Declare variable for thread's ID:
  pthread_t id;

  int j = 1;
  pthread_create(&id, NULL, foo, &j);
    
  int* ptr;

  // Wait for foo() and retrieve value in ptr;
  pthread_join(id, (void**)&ptr);
  printf("Value recevied by parent from child: ");
  printf("%i\n", *ptr);
}

RELATED TAGS

c language
concurrency
threading
multitasking
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring