Introduction
C + + 11 has been nearly two years since its release in 2011, has not been much attention, until the last few months to see some of the new features of C + + 11, as a record of what you learned it, and we encourage each other.
Believe that Linux programmers have used pthread, but with the C + + 11 Std::thread, you can write multithreaded programs at the language level, the direct benefit is that the portability of multithreaded programs has been greatly improved, so as a C + + programmer, familiar with C + + 11 of multithreaded programming is still very useful.
header files related to C + + 11 multi-Threading
The c++11 new standard introduces the following header files to support multithreaded programming, respectively <atomic>,<thread>,<mutex>,<condition_variable> and <future>.
- <atomic>: This header file mainly declares two classes, std::atomic and Std::atomic_flag, and also declares a set of C-style atomic types and C-compatible atomic manipulation functions.
- <thread>: The header file mainly declares the Std::thread class, and the Std::this_thread namespace is also in the header file.
- <mutex>: The header file primarily declares classes related to mutexes (mutexes), including Std::mutex series classes, Std::lock_guard,std::unique_lock, and other types and functions.
- <condition_variable>: the header file primarily declares classes related to condition variables, including std::condition_variable and Std::condition_variable_any.
- <future> : This header file mainly declares std::p romise,std::p ackage_task Two provider classes, and Std::future and Std::shared_ The future is two future classes, and there are other types and functions associated with it, and the Std::async () function is declared in this header file.
Std::thread "Hello Thread"
Here is one of the simplest examples of using the Std::thread class:
1#include <stdio.h>2#include <stdlib.h>3 4#include <iostream>//Std::cout5#include <thread>//Std::thread6 7 voidThread_task () {8Std::cout <<"Hello thread"<<Std::endl;9 }Ten One /* A * = = FUNCTION ========================================================= - * Name:main - * Description:program entry routine. the * ======================================================================== - */ - intMainintargcConst Char*argv[]) - { + std::thread T (thread_task); - T.join (); + A returnexit_success; at}/*----------End of function main----------*/
Makefile as follows:
ALL:THREADCC=g++cppflags=-wall-std=c++ -ggdbldflags=-pthreadthread: THREAD.O -o [email protected] $^thread.o:thread. CPP -o [email protected]-C $^. Phony: cleanclean: rm THREAD.O Thread
Note that in a Linux GCC4.6 environment, you need to add-pthread at compile time, otherwise the execution will appear:
$./'std::system_error'what (): Operation not permittedaborted ( Core dumped)
The reason is that GCC does not load the Pthread library by default, and it is said that the-pthread option can be added in subsequent releases without recompiling.
Turn from: c++11 Concurrency Guide One (c++11 multithreading approach)
"Go" c + + 11 concurrency Guide One (c + + 11 multi-Threading approach)