Multi-threading in C

Multithreading is a powerful concept in computer programming that allows multiple threads to run simultaneously within a single program. Multithreading is particularly useful in applications where multiple tasks need to be performed concurrently, such as network communication, graphical user interfaces, and computational tasks. In this blog post, we will explore the basics of multithreading in the C programming language.

What is a Thread?

A thread is a lightweight process that runs within a program. Threads share the same memory space as the parent process, but each thread has its own stack and execution context. Threads can communicate with each other through shared memory or message passing. Threads are often used to perform tasks in parallel, allowing programs to take advantage of multi-core processors.

Creating a Thread

In C, threads are created using the pthread_create function, which takes four arguments: a pointer to a pthread_t variable that will hold the thread identifier, a pointer to a pthread_attr_t variable that specifies thread attributes (NULL can be used for default attributes), a pointer to the function that the thread will run, and an argument to pass to the thread function (if any).

Here’s an example of creating a thread:

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

void *thread_func(void *arg) {
    printf("Hello from thread!\n");
    return NULL;
}

int main() {
    pthread_t thread_id;
    pthread_create(&amp;thread_id, NULL, thread_func, NULL);
    pthread_join(thread_id, NULL);
    return 0;
}


In this example, we define a function called thread_func that will be run by the new thread. This function simply prints a message to the console. In main, we create a new thread by calling pthread_create and passing in the thread ID variable, the default thread attributes, the thread_func function, and a null argument. We then call pthread_join to wait for the thread to finish before exiting the program.

Synchronization

One of the challenges of multithreading is synchronizing access to shared resources. Without proper synchronization, multiple threads can access the same resource simultaneously, leading to unpredictable behavior. In C, synchronization can be achieved using mutexes and condition variables.

A mutex is a synchronization primitive that allows threads to coordinate access to a shared resource. A mutex is typically used to protect a critical section of code, which is a section of code that must be executed atomically (i.e., without interruption from other threads).

Here’s an example of using a mutex to protect a critical section of code:

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int shared_resource = 0;

void *thread_func(void *arg) {
    pthread_mutex_lock(&amp;mutex);
    shared_resource++;
    printf("Thread %ld: shared_resource = %d\n", pthread_self(), shared_resource);
    pthread_mutex_unlock(&amp;mutex);
    return NULL;
}

int main() {
    pthread_t thread1, thread2;
    pthread_create(&amp;thread1, NULL, thread_func, NULL);
    pthread_create(&amp;thread2, NULL, thread_func, NULL);
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    return 0;
}

In this example, we define a global variable called shared_resource that will be accessed by both threads. We also define a mutex called mutex that will be used to synchronize access to shared_resource. In thread_func, we lock the mutex using pthread_mutex_lock, increment shared_resource, print the value of shared_resource, and then unlock the mutex using pthread_mutex_unlock.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *