gpt4 book ai didi

c - C : How can I stop the threads from interfering with each other? 中的多线程

转载 作者:行者123 更新时间:2023-12-03 12:48:29 25 4
gpt4 key购买 nike

我是使用 pthreads 的新手。我想创建一个程序,其中六个不同的线程将分别输出不同的数字。线程可以按任何顺序运行,但是每个线程只能运行一次。

因此,可能的输出是:

Thread: 5
Thread: 2
Thread: 3
Thread: 6
Thread: 1
Thread: 4

或者它可以是任何其他顺序。

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

void *apples(void *void_apple_num){
int *thread_num = (int *) void_apple_num;
printf("Thread: %d\n", *thread_num);
return NULL;
}



int main(){
pthread_t threads[6];
int apple_num;


for(apple_num=0; apple_num<6; apple_num++){
pthread_create(&threads[apple_num], NULL, apples, &apple_num);
}

for(apple_num=0; apple_num<6; apple_num++){
pthread_join(threads[apple_num], NULL);
}

return 0;
}

当我运行程序时,我遇到了线程相互干扰的问题。我不确定某些线程是否运行了两次?但是,我认为问题在于某些线程正在使用来自不同线程的 apple_num 指针。

这是我得到的两个示例输出:

输出 1:

Thread: 5
Thread: 0
Thread: 1
Thread: 1
Thread: 2
Thread: 2

输出 2:

Thread: 1
Thread: 4
Thread: 4
Thread: 5
Thread: 1
Thread: 1

我不完全明白是什么导致了这个问题。我听说线​​程有时可以共享变量?我不知道我是否应该使用互斥锁以某种方式让线程一次运行一个。

我如何编辑我的代码来解决这个问题?

如果其他人问过类似的问题,请将我指向他们的问题。我在研究时找不到任何东西。

最佳答案

你的每个线程都得到一个指向同一个局部变量的指针 apple_num这是由主线程在循环中更改的。由于线程异步启动,局部变量的值 apple_num从任何其他线程的角度来看,主线程中的内容是不确定的。

您需要将该变量的副本传递给每个线程。

一个解决方法是转换 intvoid*并返回:

void *apples(void* apple_num){
int thread_num = (int)void_apple_num;
...
pthread_create(&threads[apple_num], NULL, apples, (void*)apple_num);

正如他们在评论中提到的那样,intptr_tuintptr_t (来自 <stdint.h> )可能更适合无​​损往返,例如uintptr_t -> void* -> uintptr_t .但是 C 标准不需要任何整数来往返 void*来回,它只需要 void* -> intptr_t并返回。

在更现实的场景中,您可能希望将不止一个整数传递给线程,即 struct .这就是线程启动函数接收单个 void* 的基本原理。参数 - 它可以指向任何数据类型的对象(PO​​SIX 要求 void* 也能够存储函数指针)。

将结构传递给线程的示例(不依赖实现定义的整数到 void* 的转换并返回):

struct ThreadArgs {
int thread_num;
// More data members, as needed.
};

void* apples(void* arg){
struct ThreadArgs* a = arg;
printf("Thread: %d\n", a->thread_num);
free(arg);
return NULL;
}

int main() {
pthread_t threads[6];
struct ThreadArgs* a;
int apple_num;

for(apple_num=0; apple_num<6; apple_num++){
a = malloc(sizeof *a);
a->thread_num = apple_num;
pthread_create(&threads[apple_num], NULL, apples, a);
}

for(apple_num=0; apple_num<6; apple_num++)
pthread_join(threads[apple_num], NULL);

return 0;
}

请注意,您不必在堆上分配线程参数结构 (malloc)。如果你将一个自动变量(在栈上)传递给一个线程,你必须确保该变量没有改变并且在线程访问它时仍然存在。从堆中分配线程参数结构是最安全的,并且以 malloc 为代价解决了这个问题。/free电话。

关于c - C : How can I stop the threads from interfering with each other? 中的多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65703111/

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