MUTLITHREADING IN C
Thread:A thread is a single sequence stream withing a process as threads have some of the propeerties of the processes, also sometimes referred as lightweight processes.
How a process differs from threads:
Threads as not independent of one other like processes are as a result threads shares with other threads their code section , data section and many OS resuources like open files and signals .Though like process , a thread has its own program counter (PC) , a register set and the stack space.
Use of Multithreading:
threads are popular way to improve application through parallelism like in a browser , multiple tabs can perform different searches .
Thread operates faster than a process due to following reasons:
· threads creation is much faster.
· context switching between threads is much faster
Functions to create a multithreaded programs:
pthread_create:
· function to create a thread , takes 4 arguments ie. first argument is thread_id which is set be this function
· second argument specifies attributes, if value is NULL so default attributes are used.
· third argument used to pass the name of function to be executed by the thread to be created
· forth argument is used to pass the arguments to be passed to the thread function
pthread_join :
equivalant of the wait() call for the processes , pthread_join blockes the calling thread until the thread with identifier equal to the first argument terminates
pthread_exit:
used to exit from the thread function in pace of pthread_exit we can even use return to exit from the thread function.
Compiling the multithreaded program:
to compile a multithreaded program in gcc we need to link it with the thread pthread library ie -lpthread.