Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C pthread_create()输出重复的数字_C_Multithreading_Pthreads - Fatal编程技术网

C pthread_create()输出重复的数字

C pthread_create()输出重复的数字,c,multithreading,pthreads,C,Multithreading,Pthreads,C代码: #包括 #包括 #包括 #包括 void*helloFunc(void*ptr) { int*数据; 数据=(int*)ptr; printf(“我是线程%d\n”,*数据); } int main(int argc,char*argv[]) { pthread_t hThread[4]; 对于(int i=0;i

C代码:

#包括
#包括
#包括
#包括
void*helloFunc(void*ptr)
{
int*数据;
数据=(int*)ptr;
printf(“我是线程%d\n”,*数据);
}
int main(int argc,char*argv[])
{
pthread_t hThread[4];
对于(int i=0;i<4;i++)
pthread_create(&hThread[i],NULL,helloFunc,(void*)&i);
对于(int j=0;j<4;j++)
pthread_join(hThread[j],NULL);
}
我在Ubuntu20.04上运行代码,发现输出如下

I am thread 2
I am thread 4
I am thread 4
I am thread 3

我想知道为什么输出中有一些重复的数字,而不是一个随机的1、2、3、4的列表

您将
I
的地址传递给每个线程。当线程解引用它来读取它时,它可能在主线程中增加了0倍或更多倍,因此在线程中读取的内容是不确定的

您可以创建一个
int[4]
数组,并将每个
int
的唯一地址传递给启动的线程,也可以将
pthread\u t
及其数据打包到
struct
中,以将连接到某个线程的所有内容存储在一个位置

例如:

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

typedef struct {
    pthread_t th;
    int i;
    /* add more data fields to the struct if needed */
} threadinfo;

void *helloFunc(void *ptr)
{
    threadinfo* ti = (threadinfo*) ptr;
    printf("I am thread %d\n", ti->i);
}

int main(void)
{
    threadinfo hThread[4];
    for (int i = 0; i < 4; i++) {
        hThread[i].i = i;         /* fill in the data fields */

        /* and pass the address of the threadinfo struct as a parameter here */
        pthread_create(&hThread[i].th, NULL, helloFunc, (void *)&hThread[i]);
    }
    for (int j = 0; j < 4; j++)
        pthread_join(hThread[j].th, NULL);
}
#包括
#包括
#包括
#包括
类型定义结构{
pthread_t th;
int i;
/*如果需要,向结构添加更多数据字段*/
}线程信息;
void*helloFunc(void*ptr)
{
threadinfo*ti=(threadinfo*)ptr;
printf(“我是线程%d\n”,ti->I);
}
内部主(空)
{
threadinfo-hThread[4];
对于(int i=0;i<4;i++){
hThread[i].i=i;/*填写数据字段*/
/*并将threadinfo结构的地址作为参数传递给此处*/
pthread_create(&hThread[i].th,NULL,helloFunc,(void*)&hThread[i]);
}
对于(int j=0;j<4;j++)
pthread_join(hThread[j].th,NULL);
}

只有一个
i
变量。因此,每个线程看到的值取决于它相对于父线程执行的迭代运行的时间。这是由操作系统调度决定的。要获得预期的行为,请声明一个数组并向每个线程传递不同的数组元素。@kaylum我想知道是否有“一个
I
变量”。
i
的生命周期是否仅限于循环,而不保证在执行线程函数时它仍然存在?实际上可能是打印的
j
I am thread 3
I am thread 4
I am thread 4
I am thread 4
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

typedef struct {
    pthread_t th;
    int i;
    /* add more data fields to the struct if needed */
} threadinfo;

void *helloFunc(void *ptr)
{
    threadinfo* ti = (threadinfo*) ptr;
    printf("I am thread %d\n", ti->i);
}

int main(void)
{
    threadinfo hThread[4];
    for (int i = 0; i < 4; i++) {
        hThread[i].i = i;         /* fill in the data fields */

        /* and pass the address of the threadinfo struct as a parameter here */
        pthread_create(&hThread[i].th, NULL, helloFunc, (void *)&hThread[i]);
    }
    for (int j = 0; j < 4; j++)
        pthread_join(hThread[j].th, NULL);
}