当前位置:网站首页>Concurrency - create thread
Concurrency - create thread
2022-06-10 09:35:00 【Classmate Peng is her deskmate】
Create thread
Include header file #include<thread>
Create functions with threads
void threadBegin()
{
}
thread a(threadBegin);// Create a thread object You need to put a callable object inside the brackets to construct the object This function becomes the starting point of the thread
The problem of creating thread function parameters
void mprint(const int &a,char* b)
{
cout << a << endl;
cout << b << endl;
}
int main()
{
int a = 0;
char b[] = "abcd";
thread mobj(mprint,a,b);// In this way, the parameters are passed into
//mobj.detach();
mobj.join();
return 0;
}
Although it uses const int &a That is, the way of reference In fact, within the child thread a And in the main thread a Different addresses It doesn't really quote It's actually value passing
But if you pass through a pointer In a child thread b And the b It's an address So this is very dangerous
Problems with type conversion
Because it is not safe to pass the pointer directly So we convert the value stored in this pointer to string It will be better Will become value passing But there will still be problems Because of concurrency, we don't know
char b[] When is it converted to string Of May appear char b[] Has been recycled in the main function Proceed again string Type conversion of ( I don't understand here If this problem occurs What then? int a Value transfer can be realized Won't it appear int a Called back in the main function Then the value is passed to the child thread a Do you )
void mprint(const int &a,const string& b)
{
cout << a << endl;
cout << b << endl;
}
int main()
{
int a = 0;
char b[] = "abcd";
thread mobj(mprint,a,b);// The right way is thread mobj(mprint,a,string(b)); Create a temporary string
//mobj.detach();
mobj.join();
return 0;
}
prove string(b) It is feasible.
class A {
public:
double x, y;
A()
{
cout << " Constructors " << endl;
}
A(int i) {
// Type conversion constructors
cout << " Type conversion constructors " << endl;
x = i; y = 0;
}
A(const A& c) :x(c.x), y(c.y)
{
cout << " Called copy constructor " << endl;
}
~A()
{
cout << " It's deconstructed " << endl;
}
};
void start(const A& a)
{
cout << " Entering the sub thread " << endl;
}
int main()
{
int x = 1;
thread mobj(start, x);
mobj.detach();
return 0;
}

It is obvious that It is possible to execute after the main thread ends A a = x Type conversion of
So it is not completely implemented thread mobj(start,x) All the structures inside The main thread will continue to execute So there will be the front char b[] Has been recycled in the main function Proceed again string Type conversion of
When we change to A(x) when 
Obviously When we create a temporary A When the class object It must be executed first
Why ? I think so A(x) This sentence must be executed by the main thread So it must be possible to execute before the main thread ends also We can also see that the copy constructor is called explain At this point, the temporary created A The object is copied to... In the thread a
Create a thread with a class object
When creating threads with class objects Will copy a class object to the thread
So if the class object in the main thread is destroyed Class objects in child threads are not destroyed
class A
{
public:
A()
{
cout << "A Constructor for "<<endl;
}
A(const A& a)
{
cout << "A Copy constructor for " << endl;
}
void operator()()
{
cout << " I'm a class ";
}
~A()
{
cout << "A Destructor of " << endl;
}
};
int main()
{
A a;
thread mobj(a);
//mobj.detach();
mobj.join();
return 0;
}

The call is found A Copy constructor for Description is a copy of A Class object to child thread
lambda Expression creation
auto mylambda = []{
cout<<"lambda Execution start "<<endl;
cout<<"lambda end of execution "<<endl;
};
thread mobj(mylambda);
join()
Block main thread Let the main thread wait for the child thread to finish executing Then the child thread and the main thread merge
detach()
Separate the child thread from the main thread
void mprint()
{
cout << " First thread "<<endl;
cout << " First thread " << endl;
cout << " First thread " << endl;
cout << " First thread " << endl;
}
int main()
{
thread mobj(mprint);
mobj.detach();
cout << "main"<<endl;
cout << "main" << endl;
cout << "main" << endl;
cout << "main" << endl;
}
detach() The latter child thread loses contact with the main thread The child thread resides in the background and runs Child threads are C++ Runtime library takes over After the execution of the sub thread is completed, the runtime library will clean up the relevant resources ( The guardian thread )
That is, the main thread can execute concurrently with the sub thread And join Opposite
In the child thread detach No more join Come back Will report a mistake
joinable()
Determine whether you can use join perhaps detach
if(mobj.joinable() == true)
边栏推荐
- Partial processing method of jqgrid table:
- Principal Component Analysis
- Want to be iron man? It is said that many big men use it to get started
- makefile中$$的使用
- MySQL字符集utf8mb4与utf8的区别
- 牛客月赛50 D 生日(计算贡献)
- How to Spot-Check Regression Algorithms
- Talking about the methods of digital transformation | correctly understanding digital transformation
- ES6新特性class类
- printk学习之(二):调试
猜你喜欢

树莓派 IO 口驱动开发

利用requests库爬取网页获取数据

四种最简单的防反接电路

Niuke monthly race 50D birthday (calculated contribution)
![[Timm] an image model library based on pytorch](/img/b6/2fc7744a31fae1250f03b5d2fc8225.png)
[Timm] an image model library based on pytorch

Data governance in industrial digital transformation

P1387 maximum square (DP)

Deploy MySQL based on statefulset in kubernetes (Part 2)

中断上下文中的preempt count

论 T 级互动开发如何在我们手上发光发热
随机推荐
Huggingface使用过程中遇到的问题
A camera communication protocol verification CRC program
领导提拔你的原因,只有这点最真实,其他都是瞎扯!
How to Spot-Check Classification Algorithms
谈谈2022年数字化转型的趋势
Want to be iron man? It is said that many big men use it to get started
SOLR Advanced Query Application - - Grouper les requêtes par champ
Case sharing | digital intelligence upgrading: the transformation of Red Dragonfly (Part 2)
Share the 2022 China supply chain digital upgrading industry research report (PDF attached)
从0开始刷力扣
Scanpy downloading data is slow or fails to solve the problem
月赛50 F 鸡尾酒数(思维)
P1387 maximum square (DP)
Redis configuration optimization
BlockingQueue、SynchronousQueue、ArrayBlockingQueue、ReentrantLock、Semaphore、公平与非公平
Thinking about function declaration
Talking about the methods of digital transformation | correctly understanding digital transformation
R create folders and subfolders
基于深度学习的商品推荐系统(Web)
谈谈数字化转型方法|正确认识数字化转型