C ++的异步函数调用

我需要一个提示如何在C / C ++中实现异步函数调用(或者用于windows和/或linux的框架/ API调用的名称)

用例如下:父线程调用函数。 该函数创建一个子线程并返回,因此调用是非阻塞的,父线程可以继续做一些工作。

例如,获取结果的pthread_join不合适,因此必须将结果存储在堆中,并且必须通知父级。 我想要的是像父线程中的回调函数,它将在子线程准备好作业后执行。

这是令人惊讶的,但我在谷歌找不到一个例子。

感谢帮助

C ++ 0x为此提供了std::async 。 这是现有的实现 , 讨论和维基百科 。

您可以使用POSIX C获得所需的效果,而不是使用不同的框架,并注意到您在问题中提到了pthread_join() 。为此,您可以使用信号来确认并行任务完成的线程。 在此方法中,您为用户定义的信号(例如, SIGUSR1 )安装信号处理程序,创建一组具有不同任务的工作线程,并让它们在完成时发出父信号。

以下程序说明了这种尝试。 在示例中,我使用SIGUSR1通知父线程完成某些处理。 父线程一直忙着做一些I / O,直到子线程中断为止。 请注意,为清楚起见,没有放置任何类型的error handling代码。

 #include  #include  #include  #include  #include  /* Variable to hold the value of some calculation. */ int value = 0; /* Variable used by the parent thread to inform itself of task completion. */ volatile sig_atomic_t value_received = 0; /* Signal handler to handle the SIGUSR1 signal (the signal used to acknowledge the parent of task completion). */ void handler(int signum) { value_received = 1; } /* Working thread routine. First parameter is a pthread_t (cast to void*) identifying the parent. */ void *thread(void *parent) { /* Do something lengthy here, such as a long calculation. */ value = 1; sleep(5); /* Simulate lengthy operation. */ /* After processing, inform the parent thread that we have ended. */ pthread_kill((pthread_t)parent, SIGUSR1); return NULL; } int main(void) { struct sigaction action; pthread_t child; /* Install signal handler to receive the child thread notification. */ action.sa_handler = handler; sigemptyset(&action.sa_mask); action.sa_flags = 0; sigaction(SIGUSR1, &action, NULL); /* Create child thread that will perform some task. */ pthread_create(&child, NULL, thread, (void*)pthread_self()); /* Detach thread from execution. No need to join the thread later. */ pthread_detach(child); /* Do some other processing while the ongoing task is running in parallel. */ while (!value_received) { char buffer[0x100]; /* Echo some input until something happens. */ if (!fgets(buffer, sizeof buffer, stdin)) break; printf("You typed: %s", buffer); } /* Something happened (signal received or EOF in stdin). In the latter, just sleep a little while. */ if (feof(stdin)) while (!value_received) sleep(1); /* At this point, child thread has already ended the execution. */ printf("Value received: %i\n", value); return EXIT_SUCCESS; } 

该示例使用LinuxThreads信号实现,这与POSIX定义的完全不同。 如果您担心可移植性或合规性,则应进一步修改上述解决方案。

我建议你看看http://www.threadingbuildingblocks.org/ 。

Task类( http://cache-www.intel.com/cd/00/00/30/11/301114_301114.pdf#page=95 )可能是一个起点。

Boost.Thread

我相信这工作做得很好。 将函数调用作为新线程调高,只是不要加入它。 无论如何,当它完成时我会相信它。

您可以对其进行设置,以便孩子在完成使用Boost.Signals时向父母发送信号。 该信号将链接到父母的回调函数。

使用boost :: thread作为标准的跨平台解决方案。 如果你需要更多的东西,那就是Intel的Thread Building Blocks。

您必须设置某种基础结构来通知调用者工作已完成。 这可以使用Observer模式类型完成。 也可以使用消息类型基础结构。

真正的问题是你的父母在等待时会做什么。 你想让它进行民意调查吗? 或者收到通知?

使用Qt框架和Thread类或Qt Concurrency框架。

连接到QThread :: finished()或QFutureWatcher :: finished()信号,以便在工作完成时得到通知。

我在你对Starkey的回复中看到主线程是一个UI线程。

我使用了几个不同的GUI工具包,所有这些工具包都允许您从不同的线程发布消息。 因此,工作线程在完成后发布GUI消息,然后处理程序将在主线程中运行。

在Windows中,使用PostMessage

在Linux中,使用XtAppAddSignalXtNoticeSignal