Race conditions are avoided by creating critical sections in a program using a std::mutex.

computer science

Description

Background: Race conditions are avoided by creating critical sections in a program using a std::mutex. A std::lock_guard or std::unique_lock object is used to lock-andunlocking a std::mutex object to streamline creation and management of critical sections. Threads can enter and temporarily yield a critical section using std::condition_variable. Exercise: Provide brief answers to the following questions to improve your understanding of pertinent concepts: 


1. What is a critical section? Why is it used? How is it accomplished in a program? It is a fragment of code in which only one thread is allowed access to a shared resource at a time. This is done in order to avoid race conditions. A mutex is used to create a critical section. This is done by threads trying to lock the mutex via a system call to let other threads know that they are using it. If they are unsuccessful, the operating system has to wait until the mutex is 0. 


2. What are the 4 important rules that must be met in order to establish an effective Critical Section (CS)?

Instruction Files

Related Questions in computer science category