C++ 11 feature: C++ Multithreading Tutorial: std::lock
std::lock is used to lock multiple mutex at the same time. Syntax: styntax: std::lock(m1, m2, m3, m4); It will be able to lock all the objects in a non deadlock way by using deadlock avoidance algorithm. It will make series of lock, try_lock, and unlock to achieve this. It is a blocking call. The order […]
C++ 11 feature: C++ Multithreading Tutorial: std::async and std::future
In the previous chapter we studied about promise and future. In this chapter we shall see how to achieve the same using “async” and “future”. “async” is present in namespace std. “std::scync” is used to get asynchronous execution of the tasks. It means, you start a thread, and comeback to the calling function start executing […]
C++ 11 feature: C++ Multithreading Tutorial: std::promise And std::future
First we shall see some points related to Promise and Future. Promise: It is an asynchronous provider. std:async is another asynchronous provider. That we shall see in next chapter. Future: It is an asynchronous return object. It is used to get a value from promise. It will wait for notification from promise. In general: A […]
C++ 11 feature: C++ Multithreading Tutorial: Conditional Variables in C++ threading
Condition Variable is a class in C++ A conditional variable is used to block and object until it is “notified” to makeup. To use conditional variables, we need to use below header: #include <condition_variable> // std::condition_variable Conditional variable uses unique_lock to lock a thread when one of its wait functions is called. Then that thread […]
C++ 11 feature: C++ Multithreading Tutorial: Unique Lock in C++ threading
In the previous chapter we saw lock_guard. In this chapter we shall learn unique lock. Unique lock is a class that is available form C++ 11. Unique lock is a wrapper over a mutex. It will own the mutex that is passed to unique lock. A unique lock has below features: 1. A unique lock […]
C++ 11 feature: C++ Multithreading Tutorial: Lock Guard in C++ threading
In this chapter we shall learn about lock guard. 1. lock_guard is a class in C++ 2. lock_guard provides RAII style mechanism for acquiring mutex for a scoped block. 3. lock_guard acquires mutex the moment you create an object of lock_guard. 4. When the control leaves the scope, it will automatically unlocks and lock_guard object […]
C++ 11 feature: C++ Multithreading Tutorial: Recursive mutex in C++ threading
In the previous chapter we learn about timed_mutex, in this chapter we shall learn about recursive_mutex. Need for recursive mutex? Suppose there is a recursive function that acquires lock before calling itself as shown below: recursive_function() { . . . m.lock(); recursive_function(); m.unlock(); } Now the thread T1 will enter the function and locks itself. […]
C++ 11 feature: C++ Multithreading Tutorial: Timed mutex in C++ threading
In the previous chapters we learnt about Mutex, Race condition and critical section. In this chapter we shall learn about timed mutex. Generally in a mutex, when one thread tries to acquires a lock [T2], that has already been locked by another thread [T1], current thread [T2] will wait for a period of time till […]
C++ 11 feature: C++ Multithreading Tutorial: Mutex Try Lock in C++ threading
In the previous chapter we learn about mutex. In this chapter we shall learn about try_lock member function in mutex. As explained in the previous statement, try_lock is a member function of mutex class. There are different types of try_lock functions available. They are: There are 9 different try_lock member functions available: 1. std::try_lock 2. […]
C++ 11 feature: C++ Multithreading Tutorial: Mutex in C++ threading
In this chapter we shall learn about below topics; Mutex stands for Mutual Exclusion. What is mutual exclusion? To answer this question, we need to understand what is Race Condition? A race condition is a situation where two or more threads or process trying to modify the data at the same time. Race condition occurs […]