【问题标题】:get return value from 2 threads in C从C中的2个线程获取返回值
【发布时间】:2012-09-25 15:22:13
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <inttypes.h>

typedef struct tmp_num{
    int tmp_1;
    int tmp_2;
}t_num;

t_num t_nums;

void *num_mezzo_1(void *num_orig);
void *num_mezzo_2(void *num_orig);

int main(int argc, char *argv[]){

    pthread_t thread1, thread2;
    int tmp=0,rc1,rc2,num;

    num=atoi(argv[1]);
    if(num <= 3){
        printf("Questo è un numero primo: %d\n", num);
        exit(0);
    }

    if( (rc1=pthread_create( &thread1, NULL, &num_mezzo_1, (void *)&num)) ){
        printf("Creazione del thread fallita: %d\n", rc1);
        exit(1);
    }
    if( (rc2=pthread_create( &thread2, NULL, &num_mezzo_2, (void *)&num)) ){
            printf("Creazione del thread fallita: %d\n", rc2);
            exit(1);
    }

    t_nums.tmp_1 = 0;
    t_nums.tmp_2 = 0;
    pthread_join(thread1, (void **)(&t_nums.tmp_1));
    pthread_join(thread2, (void **)(&t_nums.tmp_2));
    tmp=t_nums.tmp_1+t_nums.tmp_2;
    printf("%d %d %d\n", tmp, t_nums.tmp_1, t_nums.tmp_2);
    if(tmp>2){
        printf("Questo NON è un numero primo: %d\n", num);
    }
    else{
        printf("Questo è un numero primo: %d\n", num);
    }
    exit(0);
}

void *num_mezzo_1(void *num_orig){
    int cont_1;
    int *n_orig=(int *)num_orig;
    t_nums.tmp_1 = 0;
    for(cont_1=1; cont_1<=(*n_orig/2); cont_1++){
        if((*n_orig % cont_1) == 0){
            (t_nums.tmp_1)++;
        }
    }
    pthread_exit((void *)(&t_nums.tmp_1));
    return NULL;
}

void *num_mezzo_2(void *num_orig){
    int cont_2;
    int *n_orig=(int *)num_orig;
    t_nums.tmp_2 = 0;
    for(cont_2=((*n_orig/2)+1); cont_2<=*n_orig; cont_2++){
        if((*n_orig % cont_2) == 0){
            (t_nums.tmp_2)++;
        }
    }
    pthread_exit((void *)(&t_nums.tmp_2));
    return NULL;
}

这个程序是如何工作的:我必须输入一个数字,这个程序会计算它是否是一个素数(我知道这是一个糟糕的算法,但我只需要学习 pthread )。
问题是返回的值太大了。
例如,如果我写“12”,则tmp tmp_1 tmp_2 的值主要是12590412 6295204 6295208
为什么我得到这些数字??

【问题讨论】:

  • 我的第一反应是这段代码不是线程安全的,因为你从两个不同的线程更新同一个变量。但仔细观察后,您似乎将 2 个成员分开。有什么合乎逻辑的理由为什么它们必须在同一个结构中吗?对我来说,这似乎是完全多余的混淆。如果每个线程只处理一个 int 变量,您可以使程序变得更好、更简洁。如果需要,在线程完成后将它们合并在一起。
  • 您可以尝试重新阅读我上一个问题的答案,它可以解决您的部分问题
  • @Minion91 事实上我已经改变了我的程序来满足你的答案:) 我已经将变量从本地更改为全局(我很懒)所以我没有去 malloc 任何东西.. .我说得对吗???
  • @polslinux 不,你只是绕过了部分问题
  • 初始化tmp_1tmp_2 之前你调用pthread_create()。现在你生成写入这些成员的线程,然后在主线程中将它们归零。

标签: c pthreads


【解决方案1】:

问题在于您的退货声明:

第一:

pthread_exit((void *)(&t_nums.tmp_1));
return NULL; 
}

如果你只是返回并死掉,你不需要调用 pthread_exit()

其次,您要返回一个地址 (&),这就是您要打印的内容。试试这个:

return ((void *)(t_nums.tmp_1)); 
}

【讨论】:

    【解决方案2】:

    一些注意事项:

    1. 请记住,pthread_exit 的返回值仅适用于 JOINABLE 线程。在某些其他情况下,您可能会使用不同的 pthread 类型(分离)。

    2. 在您的示例中,您返回的内容如下: void pthread_exit(void **retval);

    3. 没有args时有sigsegv。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 2018-04-15
      • 2021-07-15
      相关资源
      最近更新 更多