C Programming Cookbook by B. M. Harwani
Author:B. M. Harwani
Language: eng
Format: epub
Tags: COM000000 - COMPUTERS / General, COM082000 - COMPUTERS / Bioinformatics, COM006000 - COMPUTERS / Buyer
Publisher: Packt Publishing
Published: 2019-03-29T09:01:40+00:00
When two or more threads operate on the same data, that is, when they share the same resources, certain check measures must be applied so that only one thread is allowed to manipulate the shared resource at a time; other threads' access must be blocked. One of the methods that helps to avoid ambiguity when a resource is shared among threads is mutual exclusion.
Mutual exclusion
To avoid ambiguity when two or more threads access the same resource, mutual exclusion implements serializing access to the shared resources. When one thread is using a resource, no other thread is allowed to access the same resource. All of the other threads are blocked from accessing the same resource until the resource is free again.
A mutex is basically a lock that is associated with the shared resource. To read or modify the shared resource, a thread must first acquire the lock for that resource. Once a thread acquires a lock (or mutex) for that resource, it can go ahead with processing that resource. All of the other threads that wish to work on it will be compelled to wait until the resource is unlocked. When the thread finishes its processing on the shared resource, it unlocks the mutex, enabling the other waiting threads to acquire a mutex for that resource. Aside from mutex, a semaphore is also used in process synchronization.
A semaphore is a concept that is used to avoid two or more processes from accessing a common resource in a concurrent system. It is basically a variable that is manipulated to only allow one process to have access to a common resource and implement process synchronization. A semaphore uses the signaling mechanism, that is, it invokes wait and signal functions, respectively, to inform that the common resource has been acquired or released. A mutex, on the other hand, uses the locking mechanism—the process has to acquire the lock on the mutex object before working on the common resource.
Although mutex helps to manage shared resources among threads, there is a problem. An application of mutex in the wrong order may lead to a deadlock. A deadlock occurs in a situation when a thread that has lock X tries to acquire lock Y to complete its processing, while another thread that has lock Y tries to acquire lock X to finish its execution. In such a situation, a deadlock will occur, as both of the threads will keep waiting indefinitely for the other thread to release its lock. As no thread will be able to finish its execution, no thread will be able to free up its locks, either. One solution to avoid a deadlock is to let threads acquire locks in a specific order.
The following functions are used to create and manage threads:
pthread_join: This function makes the thread wait for the completion of all its spawned threads. If it is not used, the thread will exit as soon as it completes its task, ignoring the states of its spawned threads. In other words, pthread_join blocks the calling thread until the thread specified in this function terminates.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Whiskies Galore by Ian Buxton(41831)
Introduction to Aircraft Design (Cambridge Aerospace Series) by John P. Fielding(33054)
Rewire Your Anxious Brain by Catherine M. Pittman(18539)
Craft Beer for the Homebrewer by Michael Agnew(18132)
Cat's cradle by Kurt Vonnegut(15166)
Sapiens: A Brief History of Humankind by Yuval Noah Harari(14230)
Leonardo da Vinci by Walter Isaacson(13163)
The Tidewater Tales by John Barth(12603)
Thinking, Fast and Slow by Kahneman Daniel(12047)
Underground: A Human History of the Worlds Beneath Our Feet by Will Hunt(12007)
The Radium Girls by Kate Moore(11909)
The Art of Thinking Clearly by Rolf Dobelli(10190)
A Journey Through Charms and Defence Against the Dark Arts (Harry Potter: A Journey Throughâ¦) by Pottermore Publishing(9215)
Mindhunter: Inside the FBI's Elite Serial Crime Unit by John E. Douglas & Mark Olshaker(9177)
Tools of Titans by Timothy Ferriss(8200)
Wonder by R. J. Palacio(7984)
Turbulence by E. J. Noyes(7928)
Change Your Questions, Change Your Life by Marilee Adams(7621)
Nudge - Improving Decisions about Health, Wealth, and Happiness by Thaler Sunstein(7574)