c - 将参数传递给 pthread

标签 c multithreading unix pthreads

我有以下代码:

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

#define NUM_THREADS 100

struct thread_param {
    char *f1;
    char *f2;
    int x;
};

void *thread_function(void *arg){
    printf("%d\n", ((struct thread_param*)arg)->x);
}

int main(int argc, char *argvs[]){
    int i, thread_cr_res = 0, thread_join_res;
    pthread_t *threads;
    threads = malloc(100 * sizeof(*threads));
    if(threads == NULL){
        fprintf(stderr,"MALLOC THREADS ERROR");
        return (-1);
    }
    for(i = 0; i < NUM_THREADS; i++){
        struct thread_param *tp;
        if((tp = malloc(sizeof(*tp))) == NULL){
            fprintf(stderr,"MALLOC THREAD_PARAM ERROR");
            return (-1);
        }
        tp->f1 = "f1";
        tp->f2 = "f2";
        tp->x = i;
        thread_cr_res = pthread_create(&threads[i], 
                    NULL, 
                    thread_function, 
                    (void*)tp);
        if(thread_cr_res != 0){
            fprintf(stderr,"THREAD CREATE ERROR");
            return (-1);
        }
    }
    return (0);
}

我想要实现的是从线程打印从 0 到 99 的所有数字。我也在试验一种将结构作为线程输入参数传递的方法。

我发现奇怪的是,并不是所有的数字都显示出来,例如:

 ./a.out | grep 9
9
19
29
39
49

有时某些数字会显示两次:

...
75
74
89
77
78
79
91
91

你能解释一下为什么会这样吗? 没有显示错误。

后期编辑: 我已经使用 pthread_join 按照@Yasir 的建议重写了代码。新代码如下所示:

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

#define NUM_THREADS 100

struct thread_param {
    char *f1;
    char *f2;
    int x;
};

void *thread_function(void *arg){
    printf("%d\n", ((struct thread_param*)arg)->x);
}

int main(int argc, char *argvs[]){
    int i, thread_cr_res = 0, thread_join_res;
    pthread_t *threads;
    threads = malloc(100 * sizeof(*threads));
    if(threads == NULL){
        fprintf(stderr,"MALLOC THREADS ERROR");
        return (-1);
    }
    for(i = 0; i < NUM_THREADS; i++){
        struct thread_param *tp;
        if((tp = malloc(sizeof(*tp))) == NULL){
            fprintf(stderr,"MALLOC THREAD_PARAM ERROR");
            return (-1);
        }
        tp->f1 = "f1";
        tp->f2 = "f2";
        tp->x = i;
        thread_cr_res = pthread_create(&threads[i], 
                    NULL, 
                    thread_function, 
                    (void*)tp);
        if(thread_cr_res != 0){
            fprintf(stderr,"THREAD CREATE ERROR");
            return (-1);
        }
    }
    /* Later edit, joining the threads */
    for (i = 0; i < NUM_THREADS; i++){
        thread_join_res = pthread_join(threads[i], NULL);
        if(thread_join_res != 0){
            fprintf(stderr, "JOIN ERROR");
            return (-1);
        }       
    }
    return (0);
}

之后:

./a.out | sort
0
1
10
11
12
13
14
15
16
17
18
19
2
20
21
22
23
24
25
26
27
28
29
3
30
31
32
33
34
35
36
37
38
39
4
40
41
42
43
44
45
46
47
48
49
5
50
51
52
53
54
55
56
57
58
59
6
60
61
62
63
64
65
66
67
68
69
7
70
71
72
73
74
75
76
77
78
79
8
80
81
82
83
84
85
86
87
88
89
9
90
91
92
93
94
95
96
97
98
99

代码运行正常。我仍然无法解释为什么第一个版本的代码输出重复项。

最佳答案

使用 int pthread_join(pthread_t thread, void **value_ptr) 等待线程终止,以便在主线程退出之前获得所有结果。也因为 sizeof(*tp),你最终得到指向这个结构的指针大小,在 32 位系统上它是 4 个字节长。这可能会重写内存中的其他结构。 sizeof(thread_param) 对我来说更有意义。 此外,tp->f1 = "f1"; 指的是一个常量字符串。 IE。您不保存结构中的字符串,而是为所有thread_param 结构使用相同的缓冲区。如果 "f1" 是指向变量缓冲区的指针,这将是不安全的。

UPD:是的,下面关于尺寸的评论是正确的。

关于c - 将参数传递给 pthread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2657994/

相关文章:

对 Makefile (C/unix) 感到困惑

c - 为什么 gcc 忽略 __attribute__((stdcall))?

c - 内核符号已启用但未显示在 .config 中

python - 在 Linux 上伪造 IO 错误

java - 在调试工具 Netbeans 中单击 "stop"按钮时,多线程 java 应用程序输出了一个奇怪的结果

c - windows上的unix系统编程项目?

c - 无法打印整数数组

c++ - C++ 11中与std::atomic的同步

c++ - 获取当前线程时间id的操作开销大吗?

linux - 如何在 bash 脚本中排序?