/*
基本类型传值
*/
#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------线程传参的更多相关文章

  1. c++11中关于`std::thread`线程传参的思考

    关于std::thread线程传参的思考 最重要要记住的一点是:参数要拷贝到线程独立内存中,不管是普通类型.还是引用类型. 对于传递参数是引用类型,需要注意: 1.当指向动态变量的指针(char *) ...

  2. Java并发编程:线程池的使用

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  3. Java并发编程:线程池的使用(转)

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  4. Java并发编程:线程池的使用(转载)

    转载自:https://www.cnblogs.com/dolphin0520/p/3932921.html Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实 ...

  5. Java并发编程:线程池的使用(转载)

    文章出处:http://www.cnblogs.com/dolphin0520/p/3932921.html Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实 ...

  6. [转]Java并发编程:线程池的使用

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  7. 【转】Java并发编程:线程池的使用

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  8. 13、Java并发编程:线程池的使用

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  9. C++11 并发编程库

    C++11 并发编程 C++11 新标准中引入了几个头文件来支持多线程编程,他们分别是: <atomic>:该头文主要声明了两个类, std::atomic 和 std::atomic_f ...

随机推荐

  1. CSS 动画过程及间接实现样式延时

    /* 过度动画自动归位 */ @keyframes animation_button_scene { 0% { background: #9cacb4; } 10% { /* 样式过度2 */ } 6 ...

  2. C++(MFC)踩坑之旅 ------- 新建项目弹出“发生一个或多个错误”

    结束隔离,回公司上班,把在家办公的程序考回公司的电脑,结果出错了,每当我新建项目时,都会弹出"发生一个或多个错误",点确定后回到新建项目的设置上面,折腾了两天时间才解决,以下是我的 ...

  3. IIS反向代理配置教程(最终完整版本)

    IIS代理配置教程 插件下载:https://download.csdn.net/download/song_yan_/11996489 一.安装反向代理插件 1.rewrite插件安装 (1) 双击 ...

  4. IDEA部署项目,并结合Shell脚本运行Java程序

    一.概述 在实际开发中,我们写好的代码,往往打成war包或jar包,通过winscp或其他软件将其上传至服务器,然而这样非常大的一个弊端就是不利于开发,为什么这么说呢?假如我们刚刚将springboo ...

  5. python正则表达式中括号的作用,形如 "(\w+)\s+\w+"

    先看一个例子: import re string="abcdefg acbdgef abcdgfe cadbgfe" #带括号与不带括号的区别 regex=re.compile(& ...

  6. Flex:实例

    目的: 代码: <!--pages/index/index.wxml--> <view class="container"> <view class= ...

  7. php 基础 自动类型转换

    1.自动类型转换:表示运算的时候,Boolean,Null,String等类型,会先自动转为Integer或Float类型 null-->0 true-->1 false-->0 S ...

  8. Linux - Shell后台、前台,运行命令

    后台运行(终端能操纵) 只需要在后面加& gedit & 查看正在运行的jobs jobs 放到前台运行(终端不能操作) fg % fg %1 一个终端一个context.一个终端就是 ...

  9. ASP.NET(C#) Json序列化反序列化帮助类Jsonhelper

    原文地址:https://ken.io/note/csharp-asp.net-jsonhelper using System; using System.Collections.Generic; u ...

  10. UIView的API

    - (instancetype)initWithFrame:(CGRect)frame; 使用指定的框架矩形初始化并返回新分配的视图对象. - (instancetype)initWithCoder: ...