NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

About Threads


Threads are used to divide the program work overload into multiple processor or cores to execute the code faster.In linux the thread API for C/C++ is the Posix library.It allows to create a new concurrent process flow. A process may have multiple threads, it is faster because thread creation doesn't require any new virtual memory space instead it shares the same global memory (data and heap segments) but each one of them h as their own stack.


Thread Creation

According to my understanding of the code written in C for creating a new Thread is that in main method it uses a system call to load the dependencies by including <pthread.h> library. In this library there is a method named pthread_create() which takes several arguments including the thread ID which is of type int reperesented by pthread_t in the header file. A function to call the subroutine.

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);


here the pthread_attr_t reperesents the attributes which are used at thread creation time and if the argument is NULL then the default attribute are specified for thread creation.

the pthread_create() method returns an integer number if it returns 0 then the thread creation is successful otherwise and error code will be return.

In our case the thread creation was successful so the function return the status code of 0 and two threads were created with default attributes.


Thread Synchronization:

According to the documentation provided in the lab manual. Thread in C provides three sorts of synchronization mechanisms
-> Mutual Exclusion Lock (mutex)
-> Joins
-> Condition Variables


Mutexes or Mutual Exclusion Lock is a mechanism in which one thread block the access to the variables inside the thread attributes provided at the run time during thread creation to be accessed by any other thread during the creation process. They are useful to prevent any data corruption or inconsistancies like same memory read or write operations by multiple thread over the same address space changing the content of the blocks.

in the code for MUTEX

the method pthread_mutex_lock(); is used to acquire a lock on the specified thread associated mutex variable during thread creation and if the mutex is already locked by the another thread it is block till the pthread_mutex_unlock(); is called.Hence, the counter initially started from 0 for thread one and the mutex lock is being applied and when the thread 2 tried to attach the mutex lock it was blocked since the join function wait for the thread to complete its execution and hence the counter was incremented to one and the lock was removed and then the second thread applied mutex lock and the counter value was incremented to 2 and the lock was removed.



Join is used to wait for the thread to complete their execution in the order they were created. Later thread wait for the prior to complete its execution and then it start its execution.The pthread_join(); method wait for the termination of the thread that was parsed and when it completes its execution an identifier was returned and then the other thread start its execution.


Third mechanism is using condition variables. These are variables that are used to trigger some event like terminating the thread or halting it or waiting till some condition is true.
They are associated with mutex so that the condition might not get corrupted such that one thread waits for the condition and the other signal it before the first one resulting in a deadlock situation.
pthread_cond_init is used to initialize the condition.
pthread_cond_destroy is used to destroy the condition.
pthread_cond_wait waits for the condition variable to be signaled and unlock the mutex.
pthread_cond_signal restarts the thread waiting on the condition.
pthread_cond_broadcast wake up the threads blocked by specific condition variables.


     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.