问问题
1514 次
1 回答
4
只是一个快速演示如何std::thread
快速让你所有的麻烦消失(通过无缝集成std::bind
风格调用):
#include <string>
#include <thread>
#include <memory>
void some_function(int i, std::string bla)
{
}
struct some_class
{
void some_member_function(double x) const
{
}
};
int main()
{
std::thread t1(&some_function, 42, "the answer");
std::thread t2;
{
some_class instance;
t2 = std::thread(&some_class::some_member_function,
std::ref(instance),
3.14159);
t2.join(); // need to join before `instance` goes out of scope
}
{
auto managed = std::make_shared<some_class>();
t2 = std::thread([managed]()
{
managed->some_member_function(3.14159);
});
// `managed` lives on
}
if (t1.joinable()) t1.join();
if (t2.joinable()) t2.join();
}
于 2013-05-22T21:52:30.400 回答