Multi-Threading in C
Multi-Threading in C
2
Why use threads?
4
C Syntax - Thread
creation
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?
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