Tag: Multithreading
C++11 Multithreading – Part 1 : Three Different ways to Create Threads
In this article we will discuss how to create threads in C++11 using std::thread. Introduction to C++11 Thread Library Original C++ Standard supported only single thread programming. The new C++ Standard (referred to as C++11 or C++0x) was published in 2011. In C++11 a new thread library is introduced. Advertisements Compilers Required: Â Linux: gcc 4.8.1 …
C++11 Multithreading – Part 1 : Three Different ways to Create ThreadsRead More
C++11 Multithreading – Part 2: Joining and Detaching Threads
In this article we will discuss about joining and detaching of std::thread. Joining Threads with std::thread::join() Once a thread is started then another thread can wait for this new thread to finish. For this another need need to call join() function on the std::thread object i.e. Advertisements std::thread th(funcPtr); // Some Code th.join(); Let’s see …
C++11 Multithreading – Part 2: Joining and Detaching ThreadsRead More
C++11 Multithreading – Part 3: Carefully Pass Arguments to Threads
To Pass arguments to thread’s associated callable object or function just pass additional arguments to the std::thread constructor. By default all arguments are copied into the internal storage of new thread. Lets look at an example Advertisements Passing simple arguments to a std::thread in C++11 #include <iostream> #include <string> #include <thread> void threadCallback(int x, std::string …
C++11 Multithreading – Part 3: Carefully Pass Arguments to ThreadsRead More
C++11 Multithreading – Part 4: Data Sharing and Race Conditions
In multithreading environment data sharing between threads is very easy. But this easy sharing of data can cause problems in application. One such problem is Race Condition. What is a Race Condition? Race condition is a kind of a bug that occurs in multithreaded applications. Advertisements When two or more threads perform a set of …
C++11 Multithreading – Part 4: Data Sharing and Race ConditionsRead More
C++11 Multithreading – Part 5: Using mutex to fix Race Conditions
In this article we will discuss how to use mutex locks to protect shared data in multithreaded environment and avoid race conditions. To fix race conditions in multi-threaded environment we need mutex i.e. each thread needs to lock a mutex before modifying or reading the shared data and after modifying the data each thread should …
C++11 Multithreading – Part 5: Using mutex to fix Race ConditionsRead More
C++11 Multithreading – Part 6: Need of Event Handling
In this article we will discuss the need of Event Handling in Multi-threading. Sometimes a thread needs to wait for an event to happen like a condition to become true or a task to be completed by another thread. Advertisements For example, Suppose we are building a network based application. This application does following tasks, …
C++11 Multithreading – Part 6: Need of Event HandlingRead More
C++11 Multithreading – Part 8: std::future , std::promise and Returning values from Thread
A std::future object can be used with asych, std::packaged_task and std::promise. In this article will mainly focus on using std::future with std::promise object. Many times we encounter a situation where we want a thread to return a result. Advertisements Now question is how to do this? Lets take an example, Frequently Asked: C++11 Multithreading – …
C++11 Multithreading – Part 7: Condition Variables Explained
In this article we will discuss the usage of Condition Variable in C++11 Multi-threading with example. Condition Variables Condition Variable is a kind of Event used for signaling between two or more threads. One or more thread can wait on it to get signaled, while an another thread can signal this. Advertisements Header file required for condition …
C++11 Multithreading – Part 7: Condition Variables ExplainedRead More
C++11 Multithreading – Part 9: std::async Tutorial & Example
In this article we will discuss how to execute tasks asynchronously with std::async in C++11. std::async is introduced in c++11. what is std::async() std::async() is a function template that accepts a callback(i.e. function or function object) as an argument and potentially executes them asynchronously. Advertisements template <class Fn, class… Args> future<typename result_of<Fn(Args…)>::type>Â async (launch policy, Fn&& …
C++11 Multithreading – Part 9: std::async Tutorial & ExampleRead More
C++11 Multithreading – Part 10: packaged_task Example and Tutorial
In this example we will discuss c++11 std::packaged_task feature and its uses. std::packaged_task<> Details std::packaged_task<> is a class template and represents a asynchronous task. It encapsulates, Advertisements A callable entity i.e either function, lambda function or function object. A shared state that stores the value returned or thrown exception by associated callback. Need of std::packaged_task<> Suppose …
C++11 Multithreading – Part 10: packaged_task Example and TutorialRead More