Category: 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 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
How to Create a Thread in Java using Runnable Interface
In this article we will discuss how to create a thread in Java using Runnable Interface. Why do we need Runnable Interface We create threads to execute certain tasks in parallel. Every task is different from another Task. Tasks are usually encapsulated in its own class. Therefore, extending our Task class from Thread is not only an incorrect …
How to Create a Thread in Java using Runnable InterfaceRead More
C++11 : Start thread by member function with arguments
In this article we will discuss how to start a thread by a member function of class. Starting thread with non static member function Suppose we have a class Task, which has non static member function execute() i.e. Advertisements class Task { public: void execute(std::string command); }; Now we want to start a thread which …
C++11 : Start thread by member function with argumentsRead More
How to put a thread to sleep in c++11 ? | sleep_for | sleep_until
In this article we will discuss how to put a c++11 thread to sleep. c++11 provides 2 functions for putting a thread to sleep i.e. Advertisements std::this_thread::sleep_for std::this_thread::sleep_untill Sleep for a Duration C++11 provides a function std::this_thread::sleep_for to block the current thread for specified duration i.e. template <class Rep, class Period> void sleep_for (const chrono::duration<Rep,Period>& …
How to put a thread to sleep in c++11 ? | sleep_for | sleep_untilRead More