C++ 使用std::thread&;在成员函数内启动线程;绑定
我对下面的代码快照有一些疑问C++ 使用std::thread&;在成员函数内启动线程;绑定,c++,multithreading,c++11,C++,Multithreading,C++11,我对下面的代码快照有一些疑问 #include <iostream> #include <thread> class Task { public: void executeThread(void) { for(int i = 0; i < 5; i++) { std::cout << " :: " << i << std::endl;
#include <iostream>
#include <thread>
class Task
{
public:
void executeThread(void)
{
for(int i = 0; i < 5; i++)
{
std::cout << " :: " << i << std::endl;
}
}
void startThread(void);
};
void Task::startThread(void)
{
std::cout << "\nthis: " << this << std::endl;
#if 1
std::thread th(&Task::executeThread, this);
th.join(); // Without this join() or while(1) loop, thread will terminate
//while(1);
#elif 0
std::thread(&Task::executeThread, this); // Thread creation without thread object
#else
std::thread( std::bind(&Task::executeThread, this) );
while(1);
#endif
}
int main()
{
Task* taskPtr = new Task();
std::cout << "\ntaskPtr: " << taskPtr << std::endl;
taskPtr->startThread();
delete taskPtr;
return 0;
}
1) 关于pthread_create(),假设线程_1创建线程_2。据我所知,线程1可以退出而不加入,但线程2仍将继续运行。其中,如下面没有join()的示例所示,我无法运行线程,并且看到异常
2) 在少数几个例子中,我看到了没有线程对象的线程创建,如下所示。但当我做同样的事情时,代码就终止了
std::thread(&Task::executeThread, this);
I am compiling with below command.
g++ filename.cpp -std=c++11 -lpthread
但它仍然以例外终止。这是创建线程的正确方法还是C++的不同版本(在我的项目中,它们已经编译了,但对版本没有把握)。
3) 在我的几个项目代码示例中,我看到了下面创建线程的方法。但我无法使用下面的示例执行
std::thread( std::bind(&Task::executeThread, this) );
下面是我的代码快照
#include <iostream>
#include <thread>
class Task
{
public:
void executeThread(void)
{
for(int i = 0; i < 5; i++)
{
std::cout << " :: " << i << std::endl;
}
}
void startThread(void);
};
void Task::startThread(void)
{
std::cout << "\nthis: " << this << std::endl;
#if 1
std::thread th(&Task::executeThread, this);
th.join(); // Without this join() or while(1) loop, thread will terminate
//while(1);
#elif 0
std::thread(&Task::executeThread, this); // Thread creation without thread object
#else
std::thread( std::bind(&Task::executeThread, this) );
while(1);
#endif
}
int main()
{
Task* taskPtr = new Task();
std::cout << "\ntaskPtr: " << taskPtr << std::endl;
taskPtr->startThread();
delete taskPtr;
return 0;
}
#包括
#包括
课堂任务
{
公众:
void executeThread(void)
{
对于(int i=0;i<5;i++)
{
std::coutstd::thread(&Task::executeThread,this);
语句创建并销毁线程对象。当线程未连接或分离(如您的语句中)时,调用std::terminate
没有充分的理由在C++11中使用std::bind
,因为lambda在空间和速度方面都更好
构建多线程代码时,在编译和链接时需要指定-pthread
选项。链接器选项-lpthread
既不充分也不必要。std::thread(&Task::executeThread,this);
语句创建和销毁线程对象。当线程未连接或分离时(如在您的语句中),调用std::terminate
没有充分的理由在C++11中使用std::bind
,因为lambda在空间和速度方面都更好
构建多线程代码时,在编译和链接时需要指定-pthread
选项。链接器选项-lpthread
既不充分也不必要。根据设计,您需要连接生成的所有线程,或将它们分离。请参阅例如
另见
还要注意重要的注意事项
我也100%同意另一个答案中关于更喜欢lambdas绑定的评论
<>最后,不要在C++中线程上做pthRead取消的诱惑。参见E.G.P/P><设计>,您需要加入所有生成的线程,或者分离它们。
另见
还要注意重要的注意事项
我也100%同意另一个答案中关于更喜欢lambdas绑定的评论
<>最后,不要在C++中线程上做pthRead取消的诱惑。参见E.G.P/P>
在C++对象中,LI>< P>有一个生命周期。这与C++中处理C.中的句柄有点不同。如果在一个范围内在堆栈上创建对象,如果退出该范围,它将被破坏。这些规则有一些例外,如:代码> STD::移动< /Cord>,但作为经验,你拥有对象的生命周期。
这与上面的答案相同。当您调用std::thread(&Task::executeThread,This);
时,您实际上是在调用线程构造函数。这是线程生命周期和对象生命周期的开始。请注意,您在堆栈上创建了此对象。如果您离开作用域{..yourcode
将调用DTor。由于您在std::move
、join
或detatch
之前已执行此操作,因此将调用std::terminate()
,从而引发异常
您可以通过这种方式创建线程。如果您查看std::thread::thread(构造函数)的链接文档,有一个以相同方式创建对象foo的示例。您收到了哪些错误
相关文件:
a
b
c
我个人建议在C++中理解对象的生命周期。总之,所有对象在调用构造函数时都会启动它们的生命周期。当它们被杀死(如在范围之外)时调用它们的析构函数。编译器为你处理这个问题,所以如果你来自C,则会有新的概念。
在C++对象中,LI>< P>有一个生命周期。这与C++中处理C.中的句柄有点不同。如果在一个范围内在堆栈上创建对象,如果退出该范围,它将被破坏。这些规则有一些例外,如:代码> STD::移动< /Cord>,但作为经验,你拥有对象的生命周期。
这与上面的答案相同。当您调用std::thread(&Task::executeThread,This);
时,您实际上是在调用线程构造函数。这是线程生命周期和对象生命周期的开始。请注意,您在堆栈上创建了此对象。如果您离开作用域{..yourcode
将调用DTor。由于您在std::move
、join
或detatch
之前已执行此操作,因此将调用std::terminate()
,从而引发异常
您可以通过这种方式创建线程。如果您查看std::thread::thread(构造函数)的链接文档,有一个以相同方式创建对象foo的示例。您收到了哪些错误
相关文件:
a
b
c
我个人建议在C++中理解对象的生命周期。总之,所有对象在调用构造函数时都会开始使用它们的生命周期。它们的析构函数被调用。编译器会为您处理这个问题,因此如果您来自C,这是一个新概念。谢谢大家的输入。我在创建线程时错过了thread对象。因此,尽管编译,我还是会遇到异常。下面是我的更新代码。这三种情况都运行良好
#include <iostream>
#include <thread>
class Task
{
public:
void executeThread(std::string command)
{
for(int i = 0; i < 5; i++)
{
std::cout << command << " :: " << i << std::endl;
}
}
void startThread(void);
std::thread th2;
std::thread th3;
};
void Task::startThread(void)
{
std::cout << "\nthis: " << this << std::endl;
#if 0
std::thread th1(&Task::executeThread, this, "Thread1");
th1.join(); // Without join(), thread will terminate
#elif 0
th2 = std::thread(&Task::executeThread, this, "Thread2");
th2.join();
#else
th3 = std::thread( std::bind(&Task::executeThread, this, "Thread3") );
th3.join();
#endif
}
int main()
{
Task* taskPtr = new Task();
std::cout << "\ntaskPtr: " << taskPtr << std::endl;
taskPtr->startThread();
delete taskPtr;
return 0;
}
#包括
#包括
课堂任务
{
P