为什么不能将“ for”索引直接传递给pthread_create调用?

Axe*_*rré 3 c pthreads

#define THREADS_NUMBER 10
Run Code Online (Sandbox Code Playgroud)

给定一个函数f:

void *f(void *arg){

    pthread_mutex_lock(&mutex);
    printf("%i\n", *((int*) arg);
    pthread_mutex_unlock(&mutex);

}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么要这样写:

pthread_t threads[THREADS_NUMBER];
for(int i = 0; i < THREADS_NUMBER; i++){
    pthread_create(&threads[i], NULL, f, &i);
}
for(int i = 0; i < THREADS_NUMBER; i++){
    pthread_join(threads[i], NULL);
}
Run Code Online (Sandbox Code Playgroud)

输出此:

2 4 4 5 5 6 8 8 9 10
Run Code Online (Sandbox Code Playgroud)

在编写此代码时:

int t[10];
for(int i = 0; i < 10; i++)
    t[i] = i;
pthread_t threads[THREADS_NUMBER];
for(int i = 0; i < THREADS_NUMBER; i++){
    pthread_create(&threads[i], NULL, f, &t[i]);
}
for(int i = 0; i < THREADS_NUMBER; i++){
    pthread_join(threads[i], NULL);
}
Run Code Online (Sandbox Code Playgroud)

输出此:

0 1 4 3 5 2 6 7 9 8
Run Code Online (Sandbox Code Playgroud)

(如果你没有注意到它传递给线程函数的参数的差异fpthread_create电话。)

kir*_*dar 5

尝试展开循环。这两种情况将如下所示。

第一种情况:

pthread_create(&threads[0], NULL, f, &i);
pthread_create(&threads[1], NULL, f, &i);

…
pthread_create(&threads[9], NULL, f, &i);
Run Code Online (Sandbox Code Playgroud)

第二种情况:

pthread_create(&threads[0], NULL, f, &t[0]);
pthread_create(&threads[0], NULL, f, &t[1]);
…
pthread_create(&threads[9], NULL, f, &t[9]);
Run Code Online (Sandbox Code Playgroud)

如果您已经注意到,在第一种情况下,您始终会传递&i到每个线程,因此每个线程将指向相同的线程i及其最新内容。