---
title: "Multithreading advantages and C program"
subject: "Embedded System"
chapter: 6
key: "semester_6/embedded_system:multithreading-advantages-c"
source: https://www.bctengineer.com/q/semester_6/embedded_system/multithreading-advantages-c
site: BCT Engineering
---

# Multithreading advantages and C program

Embedded System · Chapter 6: RTOS

- Asked in: 2070 Magh, 6 marks
- Video: [Introduction to Threads in C](https://www.youtube.com/watch?v=tKgi1flgD2o) — Web App Development
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/multithreading-advantages-c
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-6

## Question

What are the advantages of multithreading program? Write a simple multithreading program in C.

## Short answer

Multithreading improves responsiveness, resource sharing, economy, CPU utilization, scalability, and simplifies program structure. A C program creates POSIX threads with pthread_create to run a print function concurrently and uses pthread_join to wait for completion.

## Answer

#### Advantages of multithreading
- **Responsiveness:** the program keeps working even if one thread is blocked or slow.
- **Resource sharing:** threads share code, data and files of the process, so communication is simple and fast.
- **Economy:** thread creation and context switching take less time and memory than for processes.
- **Better CPU utilisation:** the CPU is busy with another thread while one waits for I/O.
- **Scalability:** threads run in parallel on multi-core processors.
- **Simpler program structure:** each activity gets its own thread.

#### C program using POSIX threads
```c
#include <stdio.h>
#include <pthread.h>

/* function executed by each thread */
void *print_message(void *arg)
{
    char *name = (char *)arg;
    for (int i = 1; i <= 3; i++)
        printf("%s: count %d\n", name, i);
    return NULL;
}

int main(void)
{
    pthread_t t1, t2;                       /* thread identifiers */

    /* create two threads running the same function */
    pthread_create(&t1, NULL, print_message, "Thread-1");
    pthread_create(&t2, NULL, print_message, "Thread-2");

    /* wait for both threads to finish */
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    printf("Main: both threads finished\n");
    return 0;
}
```
Compile with `gcc prog.c -o prog -lpthread`. **Output** (order of the two threads may vary): Thread-1: count 1, 2, 3 and Thread-2: count 1, 2, 3, then "Main: both threads finished". `pthread_create` starts a thread and `pthread_join` makes main wait for it.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why is multithreading more economical than multiple processes?
   A. Threads need separate address spaces
   B. Threads avoid using the scheduler
   C. Threads need more memory but run faster
   D. Thread creation and switching need less time and memory
   Answer: D. Threads share the process resources, so creating and switching them is cheap. (Key point: Economy)

2. What does pthread_create do in the example?
   A. Waits for a thread to finish
   B. Terminates a thread
   C. Starts a new thread running the given function
   D. Creates a new process
   Answer: C. pthread_create starts a thread executing print_message with the supplied argument. (Key point: pthread_create)

3. What is the purpose of pthread_join(t1, NULL) in main?
   A. Starts thread t1
   B. Makes main wait until thread t1 finishes
   C. Cancels thread t1
   D. Locks shared data for t1
   Answer: B. Without join, main could exit and end the program before threads complete. (Key point: pthread_join)

4. What can be said about the output order of the two threads?
   A. The interleaving of Thread-1 and Thread-2 lines may vary, but main's final message comes last
   B. Thread-1 always prints entirely before Thread-2
   C. Main's message prints first
   D. The program prints nothing without a mutex
   Answer: A. Thread scheduling is not fixed, but join ensures the final main message follows both threads. (Key point: Output order)

## Related in other subjects

- [Simple program illustrating OOP concept](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoop-simple-program) — Object Oriented Programming, chapter 1
