boost C++如何知道某个boost线程已经执行了?
How to know that a certain boost thread has been executed in boost C++?
我在我的源代码中使用 boost C++ 库创建了一个独立线程,而不是主线程:
boost::thread t(&initSynthesis);
我想知道何时执行此 initSyntesis() 函数(或线程 t),以便我可以使按钮在最初隐藏的 windows 表单上可见:
button1->show();
但问题是我正在从 class 形式的 constructor
创建线程 t,而 initSynthesis
函数不是此 class 的一部分,所以执行此功能时我无法显示按钮。如何解决这个问题?
您可以将 button1
传递给 initSynthesis
,然后在 initSynthesis
中您可以调用 button1->show();
。
请注意 button1
应按引用传递,而不是按值传递。
void initSynthesis(System::Windows::Forms::Button^% button1)
{
button1->show();
//Do something else
}
//Thread will call 'initSynthesis' with 'button1'
boost::thread t{ &initSynthesis, button1 };
根据您对其他答案的评论,您似乎是在 C++/CLI 中使用增强线程。 高度适合使用.Net thread。
使用 .Net 线程,您问题的答案与其他答案非常相似,只是您不需要显式传递按钮。 .Net 中的线程方法允许作为实例方法,无需跳过任何额外的环节,因此您可以像正常一样访问实例字段。
public ref class MyWindow
{
Button^ button1;
void foo()
{
Thread^ t = gcnew Thread(gcnew ThreadStart(this, &MyWindow::InitSynthesis));
t->Start();
}
void InitSynthesis()
{
// Do work
// OK, work's done, show the "Next" button.
this->button1->Show();
}
}
我在我的源代码中使用 boost C++ 库创建了一个独立线程,而不是主线程:
boost::thread t(&initSynthesis);
我想知道何时执行此 initSyntesis() 函数(或线程 t),以便我可以使按钮在最初隐藏的 windows 表单上可见:
button1->show();
但问题是我正在从 class 形式的 constructor
创建线程 t,而 initSynthesis
函数不是此 class 的一部分,所以执行此功能时我无法显示按钮。如何解决这个问题?
您可以将 button1
传递给 initSynthesis
,然后在 initSynthesis
中您可以调用 button1->show();
。
请注意 button1
应按引用传递,而不是按值传递。
void initSynthesis(System::Windows::Forms::Button^% button1)
{
button1->show();
//Do something else
}
//Thread will call 'initSynthesis' with 'button1'
boost::thread t{ &initSynthesis, button1 };
根据您对其他答案的评论,您似乎是在 C++/CLI 中使用增强线程。 高度适合使用.Net thread。
使用 .Net 线程,您问题的答案与其他答案非常相似,只是您不需要显式传递按钮。 .Net 中的线程方法允许作为实例方法,无需跳过任何额外的环节,因此您可以像正常一样访问实例字段。
public ref class MyWindow
{
Button^ button1;
void foo()
{
Thread^ t = gcnew Thread(gcnew ThreadStart(this, &MyWindow::InitSynthesis));
t->Start();
}
void InitSynthesis()
{
// Do work
// OK, work's done, show the "Next" button.
this->button1->Show();
}
}