From the course: C++ Development: Advanced Concepts, Lambda Expressions, and Best Practices

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Mutex and lock

Mutex and lock

- [Instructor] The term mutex refers to mutually exclusive access to shared resources. A mutex is commonly used to avoid data corruption and race conditions when multiple threads of execution attempt to access the same data. A mutex will typically use locks to restrict access to one thread at a time. This is mutex.cpp from chapter six of the exercise files, and you'll see I declare a mutex object here called animal_mutex. And there's a class called Animal which will use the mutex. This is like the Animal class we've used in other exercises, with the difference that we now connect animals to their friends. And so we keep a list of friends of each animal in this standard list. And I use an alias called friend_t because I refer to that throughout the class, and this makes it more convenient. We have a member function for adding a friend. So this adds a friend to the animal, and it does it bilaterally. And so it actually…

Contents