Asynchronous function call for C ++

I need a hint on how to implement asynchronous function calls in C / C ++ (or framework names / API calls for windows and / or Linux)

The use case is as follows: the parent thread calls the function. The function creates a child thread and returns, so the call is not blocked, and the parent thread may continue to do some work.

For example, pthread_join is not suitable for getting the result, so the result must be stored on the heap, and the parent must be notified. I want something like a callback function in the parent thread that will be executed after the child thread is ready with the job.

This is surprising, but I can not find a single example in google.

thanks for the help

+6
c ++ c asynchronous
source share
8 answers

C ++ 0x provides std::async for this. There is an existing implementation , discussion and Wikipedia .

+8
source share

Instead of using different frameworks and noting that you mentioned pthread_join() in your question, you can still achieve the desired effect using POSIX C. To do this, you can use signals to confirm the flow of the parallel task. In this method, you set the signal handler for the defined the user of the signal (for example, t21>), create a set of workflows with various tasks and let them signal their parents when they are completed.

The following program illustrates the attempt. In this example, I use SIGUSR1 to inform the parent thread that some processing has completed. The parent thread remains busy doing some I / O until it is interrupted by the child thread. Please note that for clarity, the error code is not processed.

 #include <pthread.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* 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; } 

The example uses the LinuxThreads signal implementation, which is very different from what POSIX defines. If you are interested in portability or compliance, the above solution should be further reviewed.

+7
source share
+4
source share

Boost.thread

I believe this works well. Select the function call as a new thread and just don't stop it from joining. It will take care of it when it ends anyway, I believe.

You can configure it so that the child sends a signal to the parent when he finishes using Boost.Signals . This signal will be associated with the parent callback function.

+3
source share

Use boost :: thread as a standard cross-platform solution. If you need something more active, there are Intel stream assembly blocks.

+1
source share

You will need to configure the infrastructure to notify the caller that the work is completed. This can be done using the Observer pattern type. A message type infrastructure may also be used.

The real question is what your parent is going to do while he is waiting. Do you want him to poll the results? Or get a notification?

0
source share

Use the framework of the Qt and Thread classes or the Qt Concurrency .

Connect to the QThread :: finished () or QFutureWatcher :: finished () signal to receive a completion notification.

0
source share

I see in your answer to Starkey that the main thread is the UI thread.

I used several different GUI tools, and all of them allow you to post a message from another thread. So that the worker thread sends a GUI message when it is completed, the handler will then start in the main thread.

On Windows, use PostMessage .

On Linux, use XtAppAddSignal and XtNoticeSignal

0
source share

All Articles