0% found this document useful (0 votes)
41 views

Multi-Threading in C

Threads are lightweight processes that can achieve parallelism within a single process. They share resources with other threads but are not fully independent like processes. Threads operate faster than processes for creation, context switching, termination, and communication. In C, threads are created using pthread_create() which takes arguments like the thread ID, start routine, and arguments. By default, threads are joinable meaning another thread must call pthread_join() to wait for it to finish and release resources. Alternatively, threads can be detached to automatically release resources without needing to be joined.

Uploaded by

hamzand02
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Multi-Threading in C

Threads are lightweight processes that can achieve parallelism within a single process. They share resources with other threads but are not fully independent like processes. Threads operate faster than processes for creation, context switching, termination, and communication. In C, threads are created using pthread_create() which takes arguments like the thread ID, start routine, and arguments. By default, threads are joinable meaning another thread must call pthread_join() to wait for it to finish and release resources. Alternatively, threads can be detached to automatically release resources without needing to be joined.

Uploaded by

hamzand02
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Multi-threading in

C CSC326 - Operating Systems


Fall 2023
“Lightweight processes”, threads have some
of the properties of processes

Single sequence stream within in a process


What are
Threads are not independent of one other
like processes
threads?

Threads share with others some resources

2
Why use threads?

• To improve an application through


achieving parallelism
E.g. When you open multiple tabs in a
browser
• Threads operate faster than
processes:
Faster: creation, context switching between
threads, termination, communication 3
C Syntax - Header files

4
C Syntax - Thread
creation

pthread_t: an integer used to identify the thread in the system.

int pthread_create (pthread_t * thread, // pointer to thread_id


const pthread_attr_t * attr, // NULL because we use default thread attributes
void * (*start_routine)(void*), // name of function to be executed by thread
void *arg); // to pass arguments to thread, use pointer to struct if multiple args
5
C Syntax – Joining threads

• Equivalent of wait() for processes


• Blocks the calling thread, until the thread with identifier (first argument) terminates
int pthread_join ( pthread_t tid, // thread suspended until the thread identified by tid terminates, either
by calling pthread_exit() or by being cancelled
void **thread_return); // If thread_return is not NULL, the return value of tid is stored
in the location pointed to by thread_return

6
Terminate the calling thread by calling
pthread_exit()

Thread Exiting
void pthread_exit (void *retval); //
Return value of pthread_exit(), we
usually set it to NULL
7
How to compile the C program?

gcc –o exercise.out exercise.c -lpthread

8
Joinable vs. Detachable
Threads
 By default, a thread runs in joinable mode
• Joinable thread will not release any resource, even after the end of thread
function, until some other thread calls pthread_join() with its ID
• pthread_join() is a blocking call, it will block the calling thread until the
other
thread ends.

9
Joinable vs. Detachable
Threads
 Detached thread automatically releases it allocated resources on exit
• No other thread needs to join it
• To make a thread detached, we need to call int pthread_detach(pthread_t
thread);
• Or we can create directly a detachable thread:

10

You might also like