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

时间:2012-02-12 08:31:36

标签: c++ multithreading

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

我怎么能这样做?如果我以这种方式从服务员那里调用所需的方法SomeClass::SomeMethod(some_args);,当然,它们在辅助线程中执行。

这样的事情会很好: SomeClass::Invoke(function_pointer);所以,function_pointer指向的函数将在主线程中执行?我需要一个关于Windows操作系统的建议。

3 个答案:

答案 0 :(得分:6)

如果这是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.

答案 1 :(得分:2)

即使使用调用,该函数仍会在调用它的线程中执行,因此没用。

在辅助线程中释放的主线程中,您可以在循环内部进行忙等待或互斥,并且在释放时,它会根据三元变量调用某些方法。

//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

答案 2 :(得分:0)

在Embarcadero C ++ Builder中,有函数TThread::QueueTThread::Synchronize,可用于执行主线程中的函数。这可以在任何线程中使用,而不必是conn = op.get_bind() inspector = Inspector.from_engine(conn) tables = inspector.get_table_names()

if table_name not in tables:
   op.create_table()