[编程基础] C++多线程入门3-小心地将参数传递给线程

原始C++标准仅支持单线程编程。新的C++标准(称为c++11或c++0x)于2011年发布。在c++11中,引入了新的线程库。因此运行本文程序需要C++至少符合c++11标准。

3 小心地将参数传递给线程

要将参数传递给线程的关联可调用对象或函数,只需将其他参数传递给std::thread构造函数。默认情况下,所有参数都复制到新线程的内部存储中。让我们看一个例子:

3.1 在c++11中将简单参数传递给std::thread

#include <iostream>
#include <string>
#include <thread>

void threadCallback(int x, std::string str)
{
	std::cout << "Passed Number = " << x << std::endl;
	std::cout << "Passed String = " << str << std::endl;
}
int main()
{
	int x = 10;
	std::string str = "Sample String";
	std::thread threadObj(threadCallback, x, str);
	threadObj.join();
	return 0;
}

输出为:

Passed Number = 10
Passed String = Sample String

3.2 如何不将参数传递给c++11中的线程

不要将变量的地址从本地堆栈传递到线程的回调函数。因为线程1中的局部变量可能超出了作用域,但是线程2仍然试图通过它的地址访问它。在这种情况下,访问无效地址可能会导致意外行为。例如:

#include <iostream>
#include <thread>

void newThreadCallback(int * p)
{
	std::cout << "Inside Thread:"":p = " << p << std::endl;
	std::chrono::milliseconds dura(1000);
	std::this_thread::sleep_for(dura);
	*p = 19;
}
void startNewThread()
{
	int i = 10;
	std::cout << "Inside Main Thread:"":i = " << i << std::endl;
	std::thread t(newThreadCallback, &i);
	t.detach();
	std::cout << "Inside Main Thread:"":i = " << i << std::endl;
}
int main()
{
	startNewThread();
	// 表示一段时间,这里是2000 毫秒
	std::chrono::milliseconds dura(2000);
	// 当前线程休眠一段时间
	std::this_thread::sleep_for(dura);
	return 0;
}

输出为:

Inside Main Thread::i = 10
Inside Thread::p = Inside Main Thread::i = 10
000000D9DD5BF4A4
程序崩溃

同样,将指向堆上内存的指针传递给线程时也要小心。因为在新线程试图访问该存储器之前,某些线程可能会删除该存储器。在这种情况下,访问无效地址可能导致意外行为。例如:

#include <iostream>
#include <thread>

void newThreadCallback(int * p)
{
	std::cout << "Inside Thread :  "" : p = " << p << std::endl;
	std::chrono::milliseconds dura(1000);
	std::this_thread::sleep_for(dura);
	*p = 19;
}
void startNewThread()
{
	int * p = new int();
	*p = 10;
	std::cout << "Inside Main Thread :  "" : *p = " << *p << std::endl;
	std::thread t(newThreadCallback, p);
	t.detach();
	delete p;
	p = NULL;
}
int main()
{
	startNewThread();
	std::chrono::milliseconds dura(2000);
	std::this_thread::sleep_for(dura);
	return 0;
}

输出为:

Inside Main Thread :?  : *p = 10
Inside Thread :?  : p = 0000024AC61ECEA0

3.3 如何在c++11中传递对std::thread的引用

由于参数被复制到新的线程堆栈,因此,如果您需要以通用方式传递引用,即

#include <iostream>
#include <thread>

void threadCallback(int const & x)
{
	int & y = const_cast<int &>(x);
	y++;
	std::cout << "Inside Thread x = " << x << std::endl;
}

int main()
{
	int x = 9;
	std::cout << "In Main Thread:Before Thread Start x = " << x << std::endl;
	std::thread threadObj(threadCallback, x);
	threadObj.join();
	std::cout << "In Main Thread:After Thread Joins x = " << x << std::endl;
	return 0;
}

输出为:

In Main Thread:Before Thread Start x = 9
Inside Thread x = 10
In Main Thread:After Thread Joins x = 9

即使threadCallback接受参数作为参考,但所做的更改仍在线程外部不可见。这是因为线程函数threadCallback中的x引用了在新线程的堆栈上复制的临时值。如何解决呢?使用std::ref()即可。std::ref 用于包装按引用传递的值。

#include <iostream>
#include <thread>
void threadCallback(int const & x)
{
	int & y = const_cast<int &>(x);
	y++;
	std::cout << "Inside Thread x = " << x << std::endl;
}
int main()
{
	int x = 9;
	std::cout << "In Main Thread : Before Thread Start x = " << x << std::endl;
	std::thread threadObj(threadCallback, std::ref(x));
	threadObj.join();
	std::cout << "In Main Thread : After Thread Joins x = " << x << std::endl;
	return 0;
}

输出为:

In Main Thread : Before Thread Start x = 9
Inside Thread x = 10
In Main Thread : After Thread Joins x = 10

3.4 将指向类成员函数的指针分配为线程函数

将指针传递给成员函数作为回调函数,并将指针传递给Object作为第二个参数。例如:

#include <iostream>
#include <thread>
class DummyClass {
public:
    DummyClass()
    {}
    DummyClass(const DummyClass & obj)
    {}
    void sampleMemberFunction(int x)
    {
        std::cout<<"Inside sampleMemberFunction "<<x<<std::endl;
    }
};
int main() {
 
    DummyClass dummyObj;
    int x = 10;
    std::thread threadObj(&DummyClass::sampleMemberFunction,&dummyObj, x);
    threadObj.join();
    return 0;
}

输出为:

Inside sampleMemberFunction 10

3.5 参考

https://thispointer.com//c11-multithreading-part-4-data-sharing-and-race-conditions/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值