是否可以C++从辅助线程中运行的函数在主线程中执行函数
Is it possible in C++ to execute function in main thread from function that is running in secondary thread?
例如,我有一个主线程,创建了很多类等等。我有一个网络部分,在单独的线程中等待客户端数据。这个"服务员"应该从在主线程中创建的类中运行一些函数,并且这个函数应该在主线程中执行。
我该怎么做?如果我以这种方式SomeClass::SomeMethod(some_args);
从服务员调用所需的方法,当然,它们在辅助线程中执行。
有这样的东西会很好: SomeClass::Invoke(function_pointer);
这样,function_pointer指向的函数将在主线程中执行?我需要针对Windows操作系统的建议。
如果这是 Windows Win32 应用程序,则使用应用程序的消息处理队列是一种常用方法。在应用的主窗口中,您等待自定义用户消息,通常如下所示:
(in header file)
#define WM_MYCUSTOMMESSAGE (WM_USER + 1)
(WndProc for you main window)
LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_MYCUSTOMMESSAGE:
... Process something
break;
}
}
(On seconday thread)
SendMessage(hWnd, WM_MYCUSOMMESSAGE, wParam, lParam); // Send and wait for the result
PostMessage(hWnd, WM_MYCUSTOMMESSAGE, wParam, lParam); // Send the message and continue this thread.
[编辑]对于控制台应用程序,请尝试使用 Windows 事件。因此,使用以下方法创建一个命名事件:
(On primary thread)
HANDLE myEvent = CreateEvent(NULL, FALSE, FALSE, "MyEvent");
... later as part of a message processing loop
while(true)
{
WaitForSingleObject( myEvent, 0 ); // Block until event is triggers in secondary thread
... process messages here
... I recommend storing "messages" in a synchronized queue
}
(On secondary thread)
SetEvent(myEvent); // Triggers the event on the main thread.
即使使用调用,该函数仍将在调用它的线程中执行,因此没有用。
您可以在循环内,在辅助线程中释放的主线程中进行繁忙的等待或互斥锁,并且在释放时,它会根据三元变量调用某个方法。
//thread1
runThread2();
while (true)
{
mutex.acquire();
mutex.lock();
switch(command)
{
case command_noop:
sleep(1000);
break;
case command1:
foo1();
break;
case command2:
foo2();
break;
//and so on...
}
mutex.release();
}
//thread2:
mutex.lock();
//commands
command = 1;
mutex.release();
mutex.acquire();
//rest of commands
在Embarcadero C++ Builder中,有函数TThread::
Queue和TThread::Sync,可用于在主线程中执行函数。这适用于任何线程,它不必是TThread
。
#include <vcl.h>
#include <functional>
namespace {
class wrapper : public TCppInterfacedObject<TThreadProcedure> {
public:
wrapper(std::function<void(void)> f) : f_(f) {}
void __fastcall Invoke() {
f_();
}
private:
std::function<void(void)> f_;
};
const unsigned int main_thread = GetCurrentThreadId();
} // namespace
// Execute the function asynchronously in main thread
void queue_to_main_thread(std::function<void(void)> f)
{
if (GetCurrentThreadId() == main_thread) {
f();
}
else {
TThread::Queue(NULL, _di_TThreadProcedure(new wrapper(f)));
}
}
// Execute the function synchronously in main thread
void synchronize_to_main_thread(std::function<void(void)> f)
{
if (GetCurrentThreadId() == main_thread) {
f();
}
else {
TThread::Synchronize(NULL, _di_TThreadProcedure(new wrapper(f)));
}
}
相关文章:
- 如何将元素添加到数组的线程安全函数?
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- C++使用params创建线程函数会导致转换错误
- 为什么std::async使用同一个线程运行函数
- 使用 std::string () const 函数启动线程或未来
- C++线程中,没有重载函数接受 X 参数
- 如何从线程中的不同模块调用函数?
- 如何在调用析构函数时优雅地停止/销毁带有阻塞调用C++线程?
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 如何将类的成员函数传递给线程
- 是否可以创建一个从不同类调用函数的线程?
- 如何制作一个只能在一个线程上同时执行的函数?
- 启动类函数作为失去引用的线程
- 为什么即使调用了析构函数,C++11 中的分离线程也可以执行
- 非静态成员失败的线程调用函数
- 线程调用的函数对对象删除是否安全?
- 将类成员函数作为线程调用到另一个类成员函数时发出警告消息
- 类成员在构造函数(线程)之后更改地址
- C++无限依次运行两个函数(线程)
- c++将各种参数传递给父类构造函数(线程c++11)