C++ Thread synchronization via lock_guard

Introduction

We can forget to unlock the mutex manually.

A better approach is to use the std::lock_guard function instead.

It locks the mutex, and once it goes out of scope, it automatically unlocks the mutex.

Example:

#include <iostream> 
#include <thread> 
#include <string> 
#include <mutex> 

std::mutex m; // will guard std::cout 

void myfunction(const std::string& param) 
{ 
    for (int i = 0; i < 10; i++) 
    { /*  w  ww .java2  s  .com*/
        std::lock_guard<std::mutex> lg(m); 
        std::cout << "Executing function from a " << param << '\n'; 
    } // lock_guard goes out of scope here and unlocks the mutex 
} 

int main() 
{ 
    std::thread t1{ myfunction, "Thread 1" }; 
    std::thread t2{ myfunction, "Thread 2" }; 

    t1.join(); 
    t2.join(); 
} 



PreviousNext

Related