A preliminary study on multithreading in C++11 Concurrency Guide
- 2020-07-21 09:20:12
- OfStack
It's been almost two years since the launch of C++11 in 2011, and I haven't paid much attention to it until the last few months. I'll be writing about C++11 in the next few posts, just to keep track of what I've learned.
I believe that Linux programmers have used Pthread, but with C++11 std::thread, you can write multithreaded program in the language level, the direct benefit is that the portability of multithreaded program has been greatly improved, so as an C++ programmer, familiar with C++11 multithreaded programming way is still very beneficial.
If you are not familiar with C++11, I suggest you check out the new features of C++11 on Wikipedia, C++11 in Chinese, C++11 in English, and Bjarne Stroustrup, the father of C++, C++11 on FAQ. I have also collected some information about C++11 for your reference:
Data transfer
http://www.open-std.org/jtc1/sc22/wg21/
x C + + 0 / + + 11 C Support in GCC: http: / / gcc gnu. org/projects/cxx0x html
What is C + + 0 x: https: / / www2 research. att. com / ~ bs what - is - 2009. pdf
Overview of the New C + + : http: / / www artima. com/shop/overview_of_the_new_cpp
Overview of the New C + + (C + + 0 x). pdf: http: / / ishare iask. sina. com. cn f / 20120005. html & # 63; from = like
A Brief Look at C + + 0 x: http: / / www artima. com/cppsource/cpp0x html
Summary of C + + 11 Feature Availability in gcc and MSVC: http: / / www aristeia. com / 11 / C C + + + + 11 FeatureAvailability. htm
C + + 11: Come Closer: http: / / www codeproject. com/Articles / 344282 / - Come - Closer Cplusplus - 11
C + + 11 threads locks and condition variables: http: / / www codeproject. com/Articles / 598695 / Cplusplus11 - threads - locks - and - condition - variables
Move Semantics and Perfect Forwarding in C + + 11: http: / / www codeproject. com/Articles / 397492 / Move - Semantics - and Perfect - Forwarding in -- Cplusplus
http://solarianprogrammer.com/categories/C++11/
C + + 11 Concurrency: http: / / www baptiste - wicht. com / 2012/03 / cpp11 concurrency - part1 - start - threads /
http://www.hpl.hp.com/personal/Hans_Boehm/misc_slides/sfacm-cleaned.pdf
http://en.cppreference.com/w/cpp/thread
http://isocpp.org/blog/2012/12/c11-a-cheat-sheet-alex-sinyakov
The Biggest Changes in C + + 11: http: / / blog smartbear. com/c - plus - plus/the - biggest - changes in - c11 - and - why - you - should - care /
Ten C + + 11 Features Every C + + Developer Should Use: http: / / www codeproject. com/Articles / 570638 / Ten - Cplusplus11 - Features - Every - Cplusplus - Developer
C + + 11 � A Glance [part 1 of n] : http: / / www codeproject. com/Articles / 312029 / Cplusplus11 - A - Glance - part - 1 - of - n
C + + 11 � A Glance [part 2 of n] : http: / / www codeproject. com/Articles / 314415 / Cplusplus11 - A - Glance part - 2 - of - n
C + + 11 (and modern C + + style) and fast iterative development: http: / / mindhacks cn 2012/08/27 / modern - cpp - practices /
Lambda Functions in C + + 11 - the Definitive Guide: http: / / www cprogramming. com / 11 / c c + + + + 11 - lambda - closures. html
Better in C++ 11-ES327en, enum classes (strongly typed enumerations) http: / / www. cprogramming. com / 11 / c c + + + + 11 - nullptr - strongly - typed - enum - class. html
Rvalue - references - and - move - semantics - in - c + + 11: http: / / www cprogramming. com/c + + 11 / rvalue - references - and - move - semantics in c + + 11. html
http://www.gotw.ca/publications/index.htm
http://www.devx.com/SpecialReports/Door/38865
Multi - threading in C + + 0 x: http: / / accu org/index php/journals / 1584
C + + 0 X feature summary cheat sheat: http: / / www iesensor. com blog / 2011/05/31 / c x 0 - feature - summary - cheat - sheat /
Multithreading in C + + 0 x part 1: Starting Threads: http: / / www justsoftwaresolutions. co. uk/threading/multithreading - in c + + 0 x - part - 1 - starting - threads. html
http://en.cppreference.com/w/cpp/thread
http://www.cplusplus.com/reference/multithreading/
All right, let's get down to business; -)
Header file related to C++11 multithreading
The C++11 new standard introduces four header files to support multithreaded programming. They are < atomic > , < thread > , < mutex > , < condition_variable > and < future > .
< atomic > : The header declares two classes, std::atomic and std::atomic_flag, plus a set of C-style atomic types and functions for C-compatible atomic operations. < thread > : The header file primarily declares the std::thread class, and the std::this_thread namespace is also in the header file. < mutex > : The header file mainly declares the classes related to the mutex (mutex), including the std::mutex series classes, std::lock_guard, std::unique_lock, and other types and functions. < condition_variable >: The header file primarily declares the classes associated with the condition variable, including std::condition_variable and std::condition_variable_any. < future > : The header file declares the std::promise, std::package_task two Provider classes, std::future and std::shared_future () functions in this header file.
std::thread "Hello world"
Here is a simple example of using the std::thread class:
#include <stdio.h>
#include <stdlib.h>
#include <iostream> // std::cout
#include <thread> // std::thread
void thread_task() {
std::cout << "hello thread" << std::endl;
}
/*
* === FUNCTION =========================================================
* Name: main
* Description: program entry routine.
* ========================================================================
*/
int main(int argc, const char *argv[])
{
std::thread t(thread_task);
t.join();
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
Makefile is as follows:
all:Thread
CC=g++
CPPFLAGS=-Wall -std=c++11 -ggdb
LDFLAGS=-pthread
Thread:Thread.o
$(CC) $(LDFLAGS) -o $@ $^
Thread.o:Thread.cc
$(CC) $(CPPFLAGS) -o $@ -c $^
.PHONY:
clean
clean:
rm Thread.o Thread
Note that in the environment of Linux GCC4.6, -pthread needs to be added at compile time, otherwise it will appear during execution:
$ ./Thread
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)
The reason is that GCC does not load the pthread library by default, and it is said that the -ES543en option may not be added at compile time in subsequent versions.