#include <thread>
#include <iostream>
class Wrapper {
public:
void member1() {
std::cout << "i am member1" << std::endl;
}
void member2(const char *arg1, unsigned arg2) {
std::cout << "i am member2 and my first arg is (" << arg1 << ") and second arg is (" << arg2 << ")" << std::endl;
}
std::thread member1Thread() {
return std::thread(&Wrapper::member1, this);
}
std::thread member2Thread(const char *arg1, unsigned arg2) {
return std::thread(&Wrapper::member2, this, arg1, arg2);
}
};
int main() {
Wrapper *w = new Wrapper();
std::thread tw1 = w->member1Thread();
tw1.join();
std::thread tw2 = w->member2Thread("hello", 100);
tw2.join();
return 0;
}
300
一些用户已经给出了答案并对其进行了很好的解释 .
I would like to add few more things related to thread.
如何使用仿函数和线程 . 请参考以下示例 .
线程将在传递对象时创建自己的对象副本 .
#include<thread>
#include<Windows.h>
#include<iostream>
using namespace std;
class CB
{
public:
CB()
{
cout << "this=" << this << endl;
}
void operator()();
};
void CB::operator()()
{
cout << "this=" << this << endl;
for (int i = 0; i < 5; i++)
{
cout << "CB()=" << i << endl;
Sleep(1000);
}
}
void main()
{
CB obj; // please note the address of obj.
thread t(obj); // here obj will be passed by value
//i.e. thread will make it own local copy of it.
// we can confirm it by matching the address of
//object printed in the constructor
// and address of the obj printed in the function
t.join();
}
5 回答
编辑:计算你的编辑,你必须这样做:
UPDATE: 我想解释一些问题,其中一些也在评论中进行了讨论 .
上述语法是根据INVOKE定义(第20.8.2.1节)定义的:
我要指出的另一个一般事实是,默认情况下,线程构造函数将复制传递给它的所有参数 . 原因是参数可能需要比调用线程更长,复制参数保证了这一点 . 相反,如果您想真正传递引用,可以使用
std::ref
创建的std::reference_wrapper
.通过这样做,您承诺在线程对它们进行操作时,您将保证参数仍然存在 .
请注意,上面提到的所有内容也可以应用于
std::async
和std::bind
.由于您使用的是C 11,因此lambda-expression是一个很好的清洁解决方案 .
由于
this->
可以省略,它可以缩短为:要不就
这是一个完整的例子
用g编译产生以下结果
@ hop5和@RnMss建议使用C 11 lambdas,但是如果你处理指针,你可以直接使用它们:
输出
然后从this answer重写样本:
一些用户已经给出了答案并对其进行了很好的解释 .
I would like to add few more things related to thread.
如何使用仿函数和线程 . 请参考以下示例 .
线程将在传递对象时创建自己的对象副本 .
实现同样目标的另一种方法是:
但是如果你想通过引用传递对象,那么使用以下语法: