C++11并发编程3------线程传参
/*
基本类型传值
*/
#include <iostream>
#include <thread> void func(int num)
{
num = ;
std::cout << "func: " << num << std::endl;
} int main()
{
int num = ;
std::thread my_job(func, num);
my_job.join(); std::cout << "main: " << num << std::endl;
return ;
}
/*
基本类型传引用
*/
#include <iostream>
#include <thread> void func(int &num)
{
num = ;
std::cout << "func: " << num << std::endl;
} int main()
{
int num = ;
std::thread my_job(func, std::ref(num));
my_job.join(); std::cout << "main: " << num << std::endl;
return ;
}
/*
类类型传值
*/
#include <iostream>
#include <thread> class A
{
public:
int num_;
A(int num) : num_(num)
{
std::cout << "A(int num)" << std::endl;
}
A(const A &a) : num_(a.num_)
{
std::cout << "A(const A &a)" << std::endl;
}
~A()
{
std::cout << "~A()" << std::endl;
}
}; void func(A a)
{
std::cout << "a.num_ = " << a.num_ << std::endl;
a.num_ = ;
} int main()
{
A a();
std::thread my_job(func, a);
my_job.join(); std::cout << "main : a.num_ = " << a.num_ << std::endl;
return ;
}
类类型传值执行结果:多次调用拷贝构造函数,影响效率,所以不推荐这种做法

/*
类类型传引用
*/
#include <iostream>
#include <thread> class A
{
public:
int num_;
A(int num) : num_(num)
{
std::cout << "A(int num)" << std::endl;
}
A(const A &a) : num_(a.num_)
{
std::cout << "A(const A &a)" << std::endl;
}
~A()
{
std::cout << "~A()" << std::endl;
}
}; void func(A &a)
{
std::cout << "a.num_ = " << a.num_ << std::endl;
a.num_ = ;
} int main()
{
A a();
std::thread my_job(func, std::ref(a));
my_job.join(); std::cout << "main : a.num_ = " << a.num_ << std::endl;
return ;
}


如果希望子线程里面不修改对象的内容,形参可加const修饰。
/*
传智能指针
*/
#include <iostream>
#include <thread> void func(std::unique_ptr<int> int_ptr)
{
std::cout << *int_ptr << std::endl;
} int main()
{
std::unique_ptr<int> int_ptr(new int()); std::thread my_job(func, std::move(int_ptr));
my_job.join(); return ;
}
C++11并发编程3------线程传参的更多相关文章
- c++11中关于`std::thread`线程传参的思考
关于std::thread线程传参的思考 最重要要记住的一点是:参数要拷贝到线程独立内存中,不管是普通类型.还是引用类型. 对于传递参数是引用类型,需要注意: 1.当指向动态变量的指针(char *) ...
- Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- Java并发编程:线程池的使用(转)
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- Java并发编程:线程池的使用(转载)
转载自:https://www.cnblogs.com/dolphin0520/p/3932921.html Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实 ...
- Java并发编程:线程池的使用(转载)
文章出处:http://www.cnblogs.com/dolphin0520/p/3932921.html Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实 ...
- [转]Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- 【转】Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- 13、Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- C++11 并发编程库
C++11 并发编程 C++11 新标准中引入了几个头文件来支持多线程编程,他们分别是: <atomic>:该头文主要声明了两个类, std::atomic 和 std::atomic_f ...
随机推荐
- 多进程pipe
pipe模块可以实现进程之间数据传递 栗子1:3个进程,一个主进程,2个子进程,三个管道,三个进程通过3个管道连接,主进程发一个信息,通过2个子进程转发,最后回到主进程输出 import multip ...
- [转]工作量证明(PoW)权益证明(PoS)和委任权益证明(DPoS)区别
原文链接 Both in the glossary and in some of our previous posts we've touched on mining and the two main ...
- MAC Address-Table Move Update Feature
MAC Address-Table Move Update The MAC address-table move update feature allows the switch to provide ...
- eclipse链接mySQL数据库常见错误
1错误: 解决: 2,用户名输入错误 解决:查看自己的正确用户名https://zhidao.baidu.com/question/248308313.html 3. 解决: 链接示例:https:/ ...
- python manage.py shell
启动python有两种方式:python manage.py shell和python. 这两个命令 都会启动交互解释器,但是manage.py shell命令有一个重要的不同: 在启动解释器之前,它 ...
- 洛谷 P2239 螺旋矩阵(模拟 && 数学)
嗯... 题目链接:https://www.luogu.org/problem/P2239 这道题首先不能暴力建图,没有简单方法,只有进行进行找规律. AC代码: #include<cstdio ...
- VMware15下载、安装、激活
1.VMware15下载 链接:https://pan.baidu.com/s/1bI8LReRY-5k81O3rrNgg-A 提取码:6c03 2.VMware15安装 3.VMware15激活
- kongdashboard
apiVersion: v1kind: Servicemetadata: name: kong-dashboard namespace: kongspec: type: NodePort po ...
- WORKDIR 指定工作目录 每一个 RUN 都是启动一个容器、执行命令、然后提交存储层文件变更
WORKDIR 指定工作目录 格式为 WORKDIR <工作目录路径>. 使用 WORKDIR 指令可以来指定工作目录(或者称为当前目录),以后各层的当前目录就被改为指定的目录,如该目录不 ...
- Executor、Executors、ExecutorService多线程操作
Executor:一个接口,其定义了一个接收Runnable对象的方法executor,其方法签名为executor(Runnable command),该方法接收一个Runable实例,它用来执行一 ...