异步函数调用C ++ [英] Asynchronous function call for C++

查看:102
本文介绍了异步函数调用C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个暗示如何实现异步函数的C / C ++调用(或Windows和/或Linux框架名称/ API调用)

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 thread calls a function. The function creates a child thread and returning, so call is non-blocking and parent thread can continue to do some job.

例如在pthread_join得到的结果是不适合的,这样的结果必须存储somewhare堆和家长必须约通知。我要的是像在父母线程回调函数,那会子线程后执行愿同工作。

For example pthread_join to get result is not suitable, so result must be stored somewhare in heap and parent must be notified about. What I want is something like callback function in parent thread, that would be executed after child thread is ready with job.

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

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

感谢您的帮助。

推荐答案

而不是使用不同的框架,并指出,你提到在pthread_join()在你的问题,你仍然可以实现与POSIX C.预期的效果对于这一点,你可以使用信号来确认并行任务完成的线程。在这种方法中,你安装了信号处理为用户自定义信号( SIGUSR1 ,例如),创建一组具有不同任务的工作线程,并让他们发送信号指示父当他们完成了。

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.

下面的程序说明了尝试。在这个例子中,我使用 SIGUSR1 的通知一些处理完成的父线程。直到子线程中断父线程不停地忙碌着做一些I / O。需要注意的是,为了清楚起见,任何类型的无错误处理code已被放

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;
}

该示例使用LinuxThreads实现的信号,这是从什么POSIX定义完全不同。如果你所关心的便携性或合规,上述方案应进一步修订。

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.

这篇关于异步函数调用C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆