gpt4 book ai didi

c - 使用 for 循环创建多个线程时的奇怪行为

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:06:13 25 4
gpt4 key购买 nike

我正在制作一个模拟一部电梯和 49 个人的多线程程序,每个人和电梯都有自己的线程。我正处于开始阶段,只是试图创建线程并验证它们是否正常工作。

我有一个名为 person_threads 的 pthreads 数组,我试图在 for 循环中初始化它,在循环中我发送一 strip 有线程号的消息并在线程中打印出来。出于某种原因,我出现了奇怪的行为,几乎就像 for 循环在创建一些线程之前没有正确迭代(参见循环和输出)。这种行为是随机的,并且每次运行都不同,我不确定我需要做什么才能正确创建线程。如果您对可能导致此问题的原因有任何见解,请提供帮助。

代码

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

#define MAX_PERSONS 49

void *person(void *myvar);
void *elevator(void *myvar);

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

pthread_t elevator_thread;
pthread_t person_threads[MAX_PERSONS]; //My array of threads

char *elev_msg = "Elevator thread started";

pthread_create(&elevator_thread, NULL, elevator, (void *) elev_msg);

//Here is where I try to initialize messages and threads,
//see Persons function and output

for (int i = 0; i < MAX_PERSONS; i++){
char msg[50];
snprintf(msg, sizeof msg, "Person thread %d started", i);
pthread_create(&person_threads[i], NULL, person, (void *) msg);
}

printf("Main function after pthread_creates\n");

pthread_join(elevator_thread, NULL);
for (int i = 0; i < MAX_PERSONS; i++){
pthread_join(person_threads[i], NULL);
}

return 0;
}

void *person(void *myvar){
char *msg;
msg = (char *) myvar;

printf("%s\n", msg);

return NULL;
}

void *elevator(void *myvar){
char *msg;
msg = (char *) myvar;

printf("%s\n", msg);

return NULL;
}

输出

brendan@brendan-Ubuntu-Desk:~/Documents/OS-Project2$ ./elevator 
Elevator thread started
Person thread 7 started
Person thread 7 started
Person thread 7 started
Person thread 7 started
Person thread 7 started
Person thread 7 started
Person thread 7 started
Person thread 8 started
Person thread 9 started
Person thread 10 started
Person thread 12 started
Person thread 12 started
Person thread 14 started
Person thread 15 started
Person thread 15 started
Person thread 16 started
Person thread 17 started
Person thread 18 started
Person thread 19 started
Person thread 20 started
Person thread 21 started
Person thread 22 started
Person thread 23 started
Person thread 24 started
Person thread 25 started
Person thread 26 started
Person thread 27 started
Person thread 28 started
Person thread 29 started
Person thread 30 started
Person thread 31 started
Person thread 32 started
Person thread 33 started
Person thread 34 started
Person thread 35 started
Person thread 36 started
Person thread 37 started
Person thread 38 started
Person thread 39 started
Person thread 40 started
Person thread 41 started
Person thread 42 started
Person thread 43 started
Person thread 44 started
Person thread 45 started
Person thread 46 started
Person thread 47 started
Person thread 48 started
Main function after pthread_creates
Person thread 48 started

请注意有多个 7、15、12 和 48。每次运行时此行为都是随机的。我注意到它实际上总是创建 50 个线程,但我需要正确初始化数组。任何帮助将不胜感激。

最佳答案

原因如下:

for (int i = 0; i < MAX_PERSONS; i++){
char msg[50];
snprintf(msg, sizeof msg, "Person thread %d started", i);
pthread_create(&person_threads[i], NULL, person, (void *) msg);
}

msg参数只在 block 范围内的主线程有效。您需要在堆上分配 msg 缓冲区并在子线程中释放它。

关于c - 使用 for 循环创建多个线程时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40315005/

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