C++11/14 线程调用类对象和线程传参的方法

Nita ·
更新时间:2024-09-20
· 780 次阅读

线程调用类对象

在前面的示例中,我们为线程任务使用了通常的函数。实际上,我们可以使用任何可调用对象或者lambda函数,如下调用类对象的例子:

#include <iostream> #include <thread> class MyFunctor { public: void operator()() { std::cout << "functor\n"; } }; int main() { MyFunctor fnctor; std::thread t(fnctor); std::cout << "main thread\n"; t.join(); return 0; }

 在这里,我们创建了一个函数对象,并将其分配给线程任务,我们可能会用这种方法尝试此实例:

// MyFunctor fnctor; std::thread t(MyFunctor());

但是编译不通过,所以,如果我们想让它工作,我们应该这样做:

// MyFunctor fnctor; std::thread t((MyFunctor()));

就是说我们必须添加 ()包含 MyFunctor().

为什么?我这边不去深究,它跟C++函数声明规定相关。

线程传参

下面是将参数传递给线程的示例。 在这个例子中,我们传递一个字符串:

#include <iostream> #include <thread> #include <string> void thread_function(std::string s) { std::cout << "thread function "; std::cout << "message is = " << s << std::endl; } int main() { std::string s = "Kathy Perry"; std::thread t(&thread_function, s); std::cout << "main thread message = " << s << std::endl; t.join(); return 0; }

从下面的输出中,我们知道字符串已经成功地传递给了线程函数。

thread function message is = Kathy Perry main thread message = Kathy Perry

如果我们想以引用方式传递,我们可能会这样做:

void thread_function(std::string &s) { std::cout << "thread function "; std::cout << "message is = " << s << std::endl; s = "Justin Beaver"; }

为确保字符串真的是以引用方式传递,我们在线程函数尾部修改了字符串的内容。但是输出并没有改变:

thread function message is = Kathy Perry main thread message = Kathy Perry

实际上,字符串是以值传递的而不是引用传递。为了以引用方式传递,我们应该使用std::ref稍微修改下代码:

std::thread t(&thread;_function, std::ref(s));

然后,修改后的输出为:

thread function message is = Kathy Perry main thread message = Justin Beaver

有另一种免复制、非内存共享的方法在线程间传递参数。 我们可以使用 move():

std::thread t(&thread_function, std::move(s));

当字符串被从main函数移动到线程函数后,main函数就不再有这个变量了,输出为空值:

thread function message is = Kathy Perry main thread message =

线程复制和移动 copy / move

如下代码期望拷贝线程是编译不通过的:

#include <iostream> #include <thread> void thread_function() { std::cout << "thread function\n"; } int main() { std::thread t(&thread;_function); std::cout << "main thread\n"; std::thread t2 = t; t2.join(); return 0; }

但是我们可以通过move把其所有权转移,见如下代码:

// t5.cpp #include <iostream> #include <thread> void thread_function() { std::cout << "thread function\n"; } int main() { std::thread t(&thread;_function); std::cout << "main thread\n"; std::thread t2 = move(t); t2.join(); return 0; }

程序输出:

$ g++ t5.cpp -o t5 -std=c++11 -pthread
$ ./t5
main thread
thread function 

线程ID

我们可以通过 this_thread::get_id()获取线程的id信息:

int main() { std::string s = "Kathy Perry"; std::thread t(&thread_function, std::move(s)); std::cout << "main thread message = " << s << std::endl; std::cout << "main thread id = " << std::this_thread::get_id() << std::endl; std::cout << "child thread id = " << t.get_id() << std::endl; t.join(); return 0; }

输出:

thread function message is = Kathy Perry
main thread message =
main thread id = 1208
child thread id = 5224

创建多少线程呢?

线程库提供了线程数量的建议函数hardware_concurrency():

int main() { std::cout << "Number of threads = " << std::thread::hardware_concurrency() << std::endl; return 0; }

输出:

Number of threads = 2

Lambda函数

既然我们谈的C++,那么让我们来了解下Lambda。

我们可以用lambda函数(匿名函数)这样替换线程函数:

int main() { std::thread t([]() { std::cout << "thread function\n"; } ); std::cout << "main thread\n"; t.join(); // main thread waits for t to finish return 0; }

注意,我们正在编写内联代码,并将其传递到另一个线程构造函数中。

Lambda表达式是用括号括起来的一系列语句, 前缀用[], 调用lambda编译接口告诉编译器我们正在声明一个lambda函数, 在我们的例子中,没有传递参数。我们本质上可以用 {} 作为一个任务 , 并把它分配给我们的线程。

您可能感兴趣的文章:C#如何调用原生C++ COM对象详解C++中各种可调用对象深入讲解



方法 对象 c++11 类对象 c+ C++ 线程 调用类 调用

需要 登录 后方可回复, 如果你还没有账号请 注册新账号