current position:Home>C + + concurrent programming (3)
C + + concurrent programming (3)
2022-05-15 05:17:26【Robot Laboratory of Hefei University of Technology】
C++ Concurrent programming (3) and C++ Concurrent programming (2) Corresponding to Chapter 2 of the book ,C++ Concurrent programming (3) This is the closing part of Chapter 2
Transfer thread ownership
For a specific execution thread , Its ownership can be in multiple thread Transfer between instances , There are several uses
- stay thread Transfer the ownership of thread between instances
- thread Object as the return value
- thread Object as a function parameter
- towards STL Loading in container thread object
The sample code is as follows :
#include <iostream>
#include <thread>
#include <string.h>
#include <vector>
using namespace std;
const int thread_num = 10;
void smile()
{
cout << "Smile" << endl;
}
void cry()
{
cout << "cry" << endl;
}
thread test()
{
thread t_(smile);
cout << "test t_ id:" << t_.get_id() << endl;
return t_;
}
void test02(thread t_)
{
if (t_.joinable())
{
cout << "test02 t_ joinable" << endl;
cout << "test02 t_ id:" << t_.get_id() << endl;
t_.join();
}
else
{
cout << "test02 t_ unjoinable" << endl;
cout << "test02 t_ id:" << t_.get_id() << endl;
}
}
int main()
{
thread t;
// Accept... From within a function thread object
t = test();
cout << "main t id:" << t.get_id() << endl;
if(t.joinable())
{
cout << "main t joinable" << endl;
t.join();
}
thread t1 = thread(cry);
cout << "main t1 id:" << t1.get_id() << endl;
//t = move(t1); error !!!
// stay thread Transfer the ownership of thread between objects
thread t2 = move(t1);
cout << "main t2 id:" << t2.get_id() << endl;
// Pass... Inside the function thread object
test02(move(t));
t2.join();
// In a container thread object
vector<thread> v;
for (int i = 0; i < thread_num; ++i)
{
if (i % 2 == 0)
{
v.emplace_back(smile);
}
else
{
v.emplace_back(cry);
}
}
int k = 0;
for(auto& v_t:v)
{
cout << "v_t " << k << " id:" << v_t.get_id() << endl;
v_t.join();
k++;
}
cout << "main end" << endl;
return 0;
}
Operation output :
[email protected]-VirtualBox:~/Cplus_learning/build$ ../bin/thread_03
test t_ id:140260519634688
main t id:140260519634688
main t joinable
Smile
main t1 id:140260519634688
main t2 id:140260519634688
test02 t_ unjoinable
test02 t_ id:thread::id of a non-executing thread
cry
v_t 0 id:140260519634688
Smile
cry
Smile
Smile
cry
cry
cry
Smile
cry
Smile
v_t 1 id:140260511241984
v_t 2 id:140260502849280
v_t 3 id:140260494456576
v_t 4 id:140260411438848
v_t 5 id:140260403046144
v_t 6 id:140260394653440
v_t 7 id:140260386260736
v_t 8 id:140260377868032
v_t 9 id:140260369475328
main end
If to the associated thread thread Object transfers the ownership of the thread , be std::terminate() Function will be called , Terminate the whole program
So long as std::thread Object is controlling a thread , You can't simply assign a new value to it
copyright notice
author[Robot Laboratory of Hefei University of Technology],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/131/202205111245214979.html
The sidebar is recommended
- Online FAQ positioning FAQ what is the cause of the high load problem?
- What is the function of getstatic, a common tool for online FAQs?
- Android 11 new soft keyboard occlusion / animation solution
- Common tools for online FAQs include?
- How does SAP commerce cloud configure new applications for storefront
- In the CMS GC process, what is the reason why the business thread puts objects into the old generation (the characteristics of concurrent collection)?
- How good and accurate is the recommendation?
- Online FAQ positioning FAQs what are the causes of continuous GC problems?
- Does the data reflect the real viewing experience?
- What are the reasons for fullgc (throw oom if FGC recovery is invalid)?
guess what you like
Algorithm improvement - basic algorithm (turtle speed multiplication)
[C + +] sword finger offer 10 - I. Fibonacci sequence
Online FAQ positioning FAQ nosuchmethodexception what is the cause of the problem?
IOS enables native im development
What is the common function of SM?
"Automated testing" a new generation of Web front-end automated testing framework - playwright, get started quickly!
Online FAQ positioning FAQ what is the cause of the high load problem?
What is the function of watch, a common tool for online FAQs?
Timeliness in recommender systems, Zhang Fuguo et al. ESWA 2017
Alibaba's open source Java diagnostic tool uses what methods to diagnose.
Random recommended
- What is the function of dashboard, a common tool for online FAQs?
- What is the role of JAD, a common tool for online FAQs?
- Online FAQ positioning FAQ what are the causes of high CPU utilization?
- 07 - explore the underlying principles of IOS | several OC objects [instance object, class object, metaclass], ISA pointer of object, superclass, method call of object and the underlying essence of class
- Extreme fox gitlab settled in Alibaba cloud computing nest to jointly improve the development experience on the cloud
- How does artificial intelligence help natural science
- Elementui upload file
- Modern CSS solution: CSS mathematical functions
- Create a general efficiency improvement solution for front desk + middle desk based on vue3 (network disk link)
- Brush 100 front-end high-quality interview real questions in 2 weeks, and the source code is complete
- Vue has reduced its workload by half since using components
- I built a front-end mock tool
- About uboot -- Ping virtual machine Ubuntu operation
- Video transcoder editready for Mac
- [taro] taro gets the detailed attributes of the element (solved)
- Picture and text difference comparison tool: kaleidoscope for Mac
- Background of spatiotemporal artificial intelligence
- The top 10 of oceanbase database competition was born, and the integration of industry and education accelerated the training of database talents
- China brand Day | Youxuan software: strengthen its own brand and fight for China's database industry
- New feature release of gaussdb (for redis): enhanced prefix scanning and multi rent isolation
- CICC purchases the original maintenance service of gbase database in 2022
- Java implementation sequence table
- Simple implementation of linked list in Java
- C + + parameterless constructor (difference between stack and heap)
- Vue NPM startup error - solution
- With the introduction of Alibaba cloud database into Shandong medical insurance information platform, the settlement response speed increased by nearly 10 times
- Yixinhuachen was selected into the atlas of relevant manufacturers in the primary market of China's big data industry
- 2021-06-05 Java Foundation (day four): two ways of locking
- Android bangs screen and water drop screen are the best overall adaptation scheme
- Don't take detours in Android learning
- Android realizes Gaode map track playback
- 2021 preparing for the 1000 Java interview questions that gold, silver and four must brush
- The database function queries the MySQL database at the correct time
- Linux changes the SSH connection mode, changes the public key to the user name and password, and logs in
- Websocket + springboot message active push
- Java common classes and methods
- Go connect to the database (simple query learning)
- HTTP - understand HTTP protocol (I)
- Spring boot quickly switches the configuration of development environment and production environment (application. YML)
- Java gets the date of the previous day