Multithreading In C

Multithreading In C

What is a Thread? 

A thread is like a mini-sequence of actions inside a bigger task. Threads are sometimes called lightweight processes because they share some similarities with processes.

What are the differences between process and thread? 

Threads aren’t independent like processes; they share code, data, and OS resources. However, each thread has its own program counter, registers, and stack space, much like processes.

Why Multithreading?

Threads are a helpful way to make applications work better together. Think of them like different tasks happening at the same time. For example, in a web browser, each open tab can be a separate task or thread. Programs like Microsoft Word also use threads to do different jobs, like formatting text or handling inputs.

Threads are faster than processes for a few reasons:

1) Creating threads is quick.

2) Switching between threads is fast.

3) Stopping threads is easy.

4) Threads can talk to each other quickly.

Can we write multithreading Muprograms in C?

Multithreading won’t be supported by the language standard. POSIX Threads (or Pthreads) is considered as a POSIX standard for threads. Moreover, the execution of pthread will be available with gcc compiler.

C Program to illustrate the use of pthread basic functions

The program given below can only compile with the C compilers along with the pthread library.

C
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h>  //Header file for sleep(). man 3 sleep for details. 
#include <pthread.h> 
  
// A normal C function that is executed as a thread  
// when its name is specified in pthread_create() 
void *myThreadFun(void *vargp) 
{ 
    sleep(1); 
    printf("Printing Skillvertex from Thread \n"); 
    return NULL; 
} 
   
int main() 
{ 
    pthread_t thread_id; 
    printf("Before Thread\n"); 
    pthread_create(&thread_id, NULL, myThreadFun, NULL); 
    pthread_join(thread_id, NULL); 
    printf("After Thread\n"); 
    exit(0); 
}

In the main() function, we create a variable called thread_id, which is of type pthread_t—an integer used to identify the thread. After declaring thread_id, we use the pthread_create() function to make a new thread.

The pthread_create() function takes 4 arguments:

  1. The first argument is a pointer to thread_id, and this function sets its value.
  2. The second argument specifies attributes. If it’s NULL, default attributes will be used.
  3. The third argument is the name of the function to be executed for the new thread.
  4. The fourth argument is used to pass arguments to the function, in this case, myThreadFun.

The pthread_join() function is like wait() for processes. It makes the calling thread wait until the specified thread, identified by the first argument, finishes its work.

How to compile above program? 

Inorder to compile a multithreaded program with the help of gcc, we have to connect with the threads library. The command given below is used to compile the program.

gfg@ubuntu:~/$ gcc multithread.c -lpthread
gfg@ubuntu:~/$ ./a.out
Before Thread
Printing GeeksQuiz from Thread 
After Thread
gfg@ubuntu:~/$ 

A C Program to show multiple threads with the global and static variable

We know that the global and static variables can share data segments. Global and static variables will be stored in the data segment.The example given below will illustrate the same.

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <pthread.h> 
  
// Let us create a global variable to change it in threads 
int g = 0; 
  
// The function to be executed by all threads 
void *myThreadFun(void *vargp) 
{ 
    // Store the value argument passed to this thread 
    int *myid = (int *)vargp; 
  
    // Let us create a static variable to observe its changes 
    static int s = 0; 
  
    // Change static and global variables 
    ++s; ++g; 
  
    // Print the argument, static and global variables 
    printf("Thread ID: %d, Static: %d, Global: %d\n", *myid, ++s, ++g); 
} 
int main() 
{ 
    int i; 
    pthread_t tid; 
  
    // Let us create three threads 
    for (i = 0; i < 3; i++) 
        pthread_create(&tid, NULL, myThreadFun, (void *)&tid); 
  
    pthread_exit(NULL); 
    return 0; 
}
  
gfg@ubuntu:~/$ gcc multithread.c -lpthread
gfg@ubuntu:~/$ ./a.out
Thread ID: 3, Static: 2, Global: 2
Thread ID: 3, Static: 4, Global: 4
Thread ID: 3, Static: 6, Global: 6
gfg@ubuntu:~/$ 

Please note that the above example is simple and used to illustrate how threads work. Accessing a global variable in a thread is usually not recommended. What if thread 2 has priority over thread 1 and thread 1 needs to change the variable? In real-world scenarios, if multiple threads need to access a global variable, it’s better to use a mutex for safe access.

FAQ- Multithreading In C

Q1. What is multithreading in C language?

Ans. Multithreading in C means using multiple threads within a single process, where each thread serves a distinct function. It allows concurrent operation, enabling multiple tasks to be executed simultaneously. Additionally, multithreading helps minimize CPU resource consumption.

Q2. Does C allow multithreading?

Ans. The Microsoft C/C++ compiler (MSVC) offers support for building multithreaded applications. It’s beneficial to use more than one thread when your application has to execute resource-intensive operations that might otherwise make the user interface unresponsive.

Q3. What is multithreading also called?

Ans.Concurrency is frequently used interchangeably with multithreading. Multitasking enables multiple threads to run simultaneously.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment