【问题标题】:pthread Return Values to an Arraypthread 将值返回到数组
【发布时间】:2023-03-03 14:51:01
【问题描述】:

我目前正在做一个使用 pthread 的项目。到目前为止,该项目启动用户指定数量的线程并在每个线程上执行一些工作,然后关闭。每个线程都存储在动态分配的内存数组中。我这样做是:

threads = malloc(number_of_threads * sizeof(pthread_t));

然后我在 for 循环中创建每个线程:

pthread_create(&(threads[i]), NULL, client_pipe_run, (void *) &param[i]);

接下来我需要做的是存储这些线程的返回值。我的理解是我需要传递 pthread_join 一个我想要存储返回值的指针的地址。这是我有点困惑的地方。我对这一点的指针很好,然后我的大脑有点崩溃了哈哈。这是我关于如何实现这一点的想法,但我不确定这是正确的:

int *return_vals = malloc(sizeof(int) * number_of_threads);
for(i = 0; i< number_of_threads; i++)
{
pthread_join(&(threads[i]),(void *) &(return_vals[i]));
}

然后为了得到返回值,我会做类似的事情:

int val = *(return_val[0]);

对此的任何帮助将不胜感激!

【问题讨论】:

标签: c pthreads pthread-join


【解决方案1】:

请注意,您正在为线程分配内存,如下所示:

threads = malloc(number_of_thread * sizeof(pthread_t));

但是对于返回值,您可以这样做:

int *return_vals = malloc(sizeof(int *));

即这里也应该计算线程数:

int *return_vals = malloc(number_of_thread * sizeof(int));

然后您可以将返回值转换为void*

void *foo(void *arg) {
    int i = 7;
    return (void*)i;
}

int main(void) {
    int i = 0;
    int thread_count = 3;
    pthread_t* threads = malloc(thread_count * sizeof(pthread_t));
    int *return_vals = malloc(thread_count * sizeof(int));

    // create threads:
    for(i = 0; i < thread_count; ++i)
        pthread_create(&threads[i], NULL, &foo, NULL);

    // wait untill they finish their work:
    for(i = 0; i < thread_count; ++i)
        pthread_join(threads[i], (void**) &return_vals[i]);

    // print results:
    for(i = 0; i < thread_count; ++i)
        printf("Thread %d returned: %d\n", i, return_vals[i]);

    // clean up:
    free(return_vals);
    free(threads);

    return 0;
}

或者您可以确保您的代码不会对您返回的类型的大小小于或等于sizeof(void*) 做出任何假设,并在线程内为返回值动态分配内存:

void *foo(void *arg) {
    int* ret = malloc(sizeof(int));
    *ret = 7;
    return ret;
}

int main(void) {
    int i = 0;
    int thread_count = 3;
    pthread_t* threads = malloc(thread_count * sizeof(pthread_t));

    // array of pointers to return values of type int:
    int **return_vals = calloc(thread_count, sizeof(int*));

    // create threads:
    for(i = 0; i < thread_count; ++i)
        pthread_create(&threads[i], NULL, &foo, NULL);

    // wait untill they finish their work:
    for(i = 0; i < thread_count; ++i)
        pthread_join(threads[i], (void**) &return_vals[i]);

    // print results:
    for(i = 0; i < thread_count; ++i)
        printf("Thread %d returned: %d\n", i, *return_vals[i]);

    // clean up:
    for(i = 0; i < thread_count; ++i)
        free(return_vals[i]);
    free(return_vals);
    free(threads);

    return 0;
}

但如果您选择了后者,请小心您最终可能遇到的内存泄漏。

【讨论】:

  • 哎呀不敢相信我把它漏掉了>。
  • @amura.cxg: 另请注意,您正在创建ints 的数组:因此您应该传递给malloc sizeof(int) * number_of_threads,而不是sizeof(int*) * number_of_threads
  • 我尝试了您建议的第一个实现,当我尝试使用return_vals[i] 时,它给了我一些大整数(例如 134602832),而它应该是 0 或 -1。我在数组上做了一个 memset 并将所有内容设置为 0,因此代码确实正确修改了该值,但它并没有把我期望的东西放在那里。尝试从数组中获取返回值时是否缺少某些内容?
  • @amura.cxg:这就是为什么我把printf 也放在那里,以便清楚应该如何使用这些值。这两个例子对我来说都很好,再看一遍并尝试找到与您的代码的类比。万一你被它困住了,那就把它作为一个新问题发布,也许还有另一个错误。
  • @LihO 为什么在foo 内部的第一个示例中,您将int 转换为void *?我们应该像这样返回:return (void *) &amp;i?因为据我所知void * 是一个指针,而i 不是。
猜你喜欢
  • 1970-01-01
  • 2019-03-03
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
相关资源
最近更新 更多