Asynchronous function call for C++

前端 未结 8 725
面向向阳花
面向向阳花 2021-02-09 17:38

I need a hint how to implement asynchronous function calls in C/C++ ( or names of frameworks/API calls for windows and/or linux )

The use case is following: A parent thr

相关标签:
8条回答
  • 2021-02-09 18:09

    Rather than using different frameworks, and noting that you've mentioned pthread_join() in your question, you can still achieve the desired effect with POSIX C. For that, you can use signals to acknowledge a thread of a parallel task completion. In this method, you install a signal handler for a user-defined signal (SIGUSR1, for example), create a set of working threads with different tasks and let them signalize the parent when they are complete.

    The following program illustrates the attempt. In the example, I use SIGUSR1 to inform the parent thread of the completion of some processing. The parent thread is kept busy doing some I/O until interrupted by the child thread. Note that, for clarity, no error-handling code of any sort has been put.

    #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 implementation of signals, which is quite different from what POSIX defines. If you are concerned with portability or compliance, the above solution should be further revised.

    0 讨论(0)
  • 2021-02-09 18:13

    C++0x provides std::async for this. Here's an existing implementation, a discussion, and Wikipedia.

    0 讨论(0)
  • 2021-02-09 18:14

    I suggest you check out http://www.threadingbuildingblocks.org/.

    The Task class (http://cache-www.intel.com/cd/00/00/30/11/301114_301114.pdf#page=95) may be a starting point.

    0 讨论(0)
  • 2021-02-09 18:14

    Use boost::thread as the standard cross-platform solution. If you need something more involved, there's Intel's Thread Building Blocks.

    0 讨论(0)
  • 2021-02-09 18:20

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

    Connect to QThread::finished() or QFutureWatcher::finished() signal to be notified when the work is done.

    0 讨论(0)
  • 2021-02-09 18:21

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

    I've used several different GUI toolkits and all of them allow you to post a message from a different thread. So have the worker thread post a GUI message when it is complete, the handler will then run in the main thread.

    In Windows, use PostMessage.

    In Linux, use XtAppAddSignal and XtNoticeSignal

    0 讨论(0)
提交回复
热议问题