gpt4 book ai didi

c++ - 如何将类中的函数放入线程中? (使用 Boost 的 C++)

转载 作者:太空宇宙 更新时间:2023-11-04 15:27:55 25 4
gpt4 key购买 nike

我有一个包含一些函数的类,例如 printf("hello main thread");printf("hello thread created inside class");。每个理论上都可以使用类变量。如何将其中一个函数放入线程中? (C++ 使用 Boost 库)

最佳答案

看看 boost::bind

class Class
{
public:
void method(const char*);
};

// instance is an instance of Class
boost::thread(boost::bind(&Class::method, &instance, "hello main thread"));

应该这样做。

但是,请注意 boost::thread 有一个构造函数已经这样做了:参见 this link .

所以你基本上可以这样做:

boost::thread(&Class::method, &instance, "hello main thread");

关于c++ - 如何将类中的函数放入线程中? (使用 Boost 的 C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4122858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com