gpt4 book ai didi

linux - 多线程数据丢失

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:41 26 4
gpt4 key购买 nike

我使用 pthread_create创建多个线程,在每个线程中,我使用线程 ID 作为输入来生成名称字符串 thread_<id> ,这里是有问题的代码:

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

#define THREAD_NUM 40

void *common_function(void *arg);

int main (void)
{
int res;
unsigned int i;
pthread_t t[THREAD_NUM];

memset(t, 0x0, sizeof(t));

for(i = 0; i < THREAD_NUM; i++)
{
/* pass info */
res = pthread_create(&(t[i]), NULL, common_function, (void *)&i);
if(res !=0)
{
printf("thread: %d create fail\n", i);
return -1;
}
}

for(i=0; i<THREAD_NUM; i++)
{
pthread_join(t[i], NULL);
}

return 0;
}

void *common_function(void *arg)
{
unsigned int p;
#ifdef WRITE_FILE
FILE *fd;
char name[64];
#endif

p = *(unsigned int *)arg;
#ifdef WRITE_FILE
memset(name, 0x0, sizeof(name));
sprintf(name, "thread_%02d", p);
fd = fopen(name, "w");
if(fd < 0 )
{
printf("create: %s fail\n", name);
pthread_exit((void *)&p);
}
#endif

#ifdef WRITE_FILE
fprintf(fd, "%02d\n", p);
#else
printf("thread: %02d\n",p);
#endif
usleep(1);

#ifdef WRITE_FILE
fclose(fd);
#endif

pthread_exit((void *)&p);
}

我编译代码使用(我没有看到任何警告):

gcc -Wall -g -pthread -D_REENTRANT -DWRITE_FILE ./test_thread_problem.c

输出是:

-rw-rw-r-- 1 haochen haochen    3  3月 13 08:56 thread_02
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_03
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_04
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_05
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_06
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_07
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_08
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_09
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_11
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_12
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_13
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_14
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_16
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_17
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_18
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_19
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_20
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_21
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_22
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_23
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_24
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_25
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_26
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_28
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_29
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_31
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_32
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_33
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_34
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_35
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_36
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_37
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_38
-rw-rw-r-- 1 haochen haochen 3 3月 13 08:56 thread_39

你会发现有一些文件遗漏了,但是调用pthread_create后它并没有打印出失败信息。或 fopen .我更改为仅打印而不创建文件(没有 -DWRITE_FILE 选项),也可以看到问题:

./a.out
thread: 03
thread: 07
thread: 03
thread: 04
thread: 04
thread: 05
thread: 06
thread: 15
thread: 19
thread: 19
thread: 19
thread: 19
thread: 19
thread: 19
thread: 20
thread: 20
thread: 20
thread: 20
thread: 20
thread: 20
thread: 21
thread: 22
thread: 23
thread: 27
thread: 29
thread: 29
thread: 31
thread: 37
thread: 38
thread: 38
thread: 38
thread: 38
thread: 38
thread: 38
thread: 38
thread: 39
thread: 04
thread: 05
thread: 18
thread: 18

问题是什么?

最佳答案

您将相同的 i 作为参数传递给所有线程。

res = pthread_create(&(t[i]), NULL, common_function, (void *)&i);
^^

这会导致竞争条件,因为主线程修改 i 而所有其他线程都从 i 读取。我建议您为每个线程使用不同的变量。

unsigned int    tid[THREAD_NUM];
...
for(i = 0; i < THREAD_NUM; i++) {
/* pass info */
tid[i] = i;
res = pthread_create(&(t[i]), NULL, common_function, (void *)&tid[i]);
...

关于linux - 多线程数据丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49246795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com