是否可以C++从辅助线程中运行的函数在主线程中执行函数

Is it possible in C++ to execute function in main thread from function that is running in secondary thread?

本文关键字:函数 线程 执行 运行 是否 C++      更新时间:2023-10-16

例如,我有一个主线程,创建了很多类等等。我有一个网络部分,在单独的线程中等待客户端数据。这个"服务员"应该从在主线程中创建的类中运行一些函数,并且这个函数应该在主线程中执行。

我该怎么做?如果我以这种方式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)));
    }
}