![]() The buffer also rotates in a circular fashion as kept track of by the left and right integer index fields of the Buffer class. The buffer is a circular bounded buffer, meaning that it has a fixed size as defined by the macro BUFFER_CAPACITY. placing integers in the buffer) and “consuming” (i.e. The main part of the problem is the Buffer data structure, which has two functions for “producing” (i.e. Let’s go over the header file for the Buffer class. Note that the thread calling notify() doesn’t need to acquire the mutex of the waiting thread due to the notify() function itself. Once the thread that is waiting is notified by another thread, it reacquires the mutex. When a calling thread calls wait() it must first acquire the mutex as input to wait().ĭuring wait(), the thread is placed on the condition variable’s queue of threads and the mutex is released. The calling thread remains blocked until another thread calls notify() on the same condition variable. blocked) until it is notified to resume by another thread.Ī unique lock is used to lock the calling thread that calls wait(). Condition VariablesĪs a solution to the previous problem, we will introduce condition variables, which allow inter thread communication.Ĭondition variables work by making a thread wait (i.e. Note that with mutexes, all accesses to a shared data structure between threads must be synchronized with mutexes. We can call a sleep function in intervals but ultimately this solution is still inefficient because the threads can’t talk to each other. However, the solution is inefficient because of the while loop, which keeps the CPU busy unnecessarily while it loops. Mutexes alone are enough to solve the data race problem discussed previously if we use the while loops to check for the buffer being empty or full and use mutexes to make the code thread safe. Threads wanting to access a thread safe block of code call mutex.lock() and if another thread is currently accessing that block, the thread is added to a queue from which threads are removed one at a time and run when the running thread calls mutex.unlock(). Typically mutexes are implemented with a queue of threads. This is done by calling mutex.lock() at the start of the block and mutex.unlock() at the end. We can make transactions thread safe using mutexes by locking a particular block of code that we want to execute such that only one thread is accessing that block at a time. Mutexes are a synchronization primitive that prevent data races when multiple threads access the same data structure. We will go over two synchronization primitives, mutexes and condition variables, that can be used in one possible solution to this problem. If the buffer size update is not thread safe, producers may try to add data to the buffer when it is already full and consumers may try to remove data from the buffer when the buffer is empty. However, this solution will result in data races.ĭata races easily occur if no synchronization techniques are used because the size of the buffer needs to be updated atomically with the production and consumption of data in the buffer. The heart of the problem lies in coordinating the producers to only add data if there is space in the buffer and the consumers to only remove data from the buffer if there exists data in the buffer.Īn initial solution to the data race problem may use while loops to check the size.įor example, the producer may loop while the buffer is full and when there is space, exit the while loop and add data to the buffer. buffer) that many consumers are reading from at the same time (i.e. ![]() In the Producer Consumer problem, many producers are adding data to a data structure (i.e. ![]() Post Outlineīackground Producer Consumer Problem Setup This post is based off of the blog post here by Baptiste Wicht. The solution is written in C++ and uses mutexes and condition variables. We will go over a solution to the Producer Consumer problem in concurrency with multiple producers and consumers in a buffer of bounded size.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |