Fluent::Programmer

    Home Blog About
  • Home
  • Blog
  • About

Beautiful code #9 - Restrict access to memory location for threads using mutex, cv, bool flag in multi threading (C++) ๐Ÿ‘‹

  • Fluent Programmer
  •   3 min read

Quick summary โ†ฌ  To restrict access to a memory location in C++ to only certain threads, you can use a combination of synchronization primitives such as mutexes and condition variables. Here’s an example that demonstrates how to achieve this:

Beautiful code #9

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex mtx;
std::condition_variable cv;
bool isAllowed = false;

void restrictedAccessThread(int threadId) {
    std::unique_lock<std::mutex> lock(mtx);
    
    // Wait until access is allowed
    cv.wait(lock, [] { return isAllowed; });
    
    // Access the restricted memory location
    std::cout << "Thread " << threadId << " is accessing the restricted memory location." << std::endl;
    
    // Release the lock
    lock.unlock();
    
    // Perform other operations with the memory location
    
    // Notify other threads that access is allowed
    cv.notify_all();
}

int main() {
    std::thread t1(restrictedAccessThread, 1);
    std::thread t2(restrictedAccessThread, 2);
    
    // Allow access to the memory location
    {
        std::lock_guard<std::mutex> lock(mtx);
        isAllowed = true;
    }
    
    // Notify waiting threads that access is now allowed
    cv.notify_all();
    
    // Perform other operations
    
    // Wait for threads to finish
    t1.join();
    t2.join();
    
    return 0;
}

Beautiful code #9 explanation

  1. In the above example, we have two threads (t1 and t2) that will attempt to access a restricted memory location. The access is controlled using a mutex (mtx) and a condition variable (cv).

  2. The restrictedAccessThread function represents the code executed by each thread. It first acquires a lock on the mutex and then waits on the condition variable until isAllowed becomes true. This ensures that the thread remains blocked until it is explicitly allowed to access the memory location.

  3. Once access is granted (i.e., isAllowed becomes true), the thread proceeds to access the restricted memory location. After completing its operations, it releases the lock and notifies other waiting threads that access is now allowed and first come thread will get the access to the restricted memory location.

  4. In the main function, we initially set isAllowed to true and notify all waiting threads using cv.notify_all(). This allows the threads to proceed with their operations. You can modify the control flow and synchronization logic as per your specific requirements.

  5. By using a combination of mutexes, condition variables, and boolean flags, you can selectively control and restrict access to a memory location in C++, allowing only certain threads to access it while ensuring synchronization and avoiding data races.

You will also like โ€” More Articles

https://fluentprogrammer.com/beautiful-code-1-using-define-templates-r-value-reference/ https://fluentprogrammer.com/beautiful-code-2-enable_if_t-template-inside-a-template/ https://fluentprogrammer.com/beautiful-code-3-no_unique_address_cpp_20-feature/ http://fluentprogrammer.com/beautiful-code-4-any_of-none_of-all_of/ https://fluentprogrammer.com/beautiful-code-5-for_each-optional/

You may like this

About The Author

Fluentprogrammer doesn't need coffee to program. They run on pure caffeine and lines of code.

Email Newsletter

Table of Contents

  • Beautiful code #9
  • Beautiful code #9 explanation
  • You will also like โ€” More Articles
  • You may like this
  • C++
  • Beautiful code series

Unhealthy love with dark corners of C++

Founded by an engineer to help engineers. 2021โ€“2023.

  • About us
  • Privacy policy