如果将堆栈变量的地址传递给 pthread 函数会怎样?

What happen if passing the address of the stack variable to the pthread function?

我编写了一个程序让线程执行 orderly.But 当我将堆栈变量的地址传递给线程函数并且程序只是等待 always.Here 是程序:

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t order_cond = PTHREAD_COND_INITIALIZER;
int count = 0;

void* thread_print(void* num){
    sleep(10);
    int number = *(int*)num;
    //printf("%d", number);
    for(int i = 0; i < 10; i++){
        pthread_mutex_lock(&mutex);
        while((count%3)!=number){
            pthread_cond_wait(&order_cond, &mutex);
        }
        switch(number){
            case 0:{
                printf("A");
                break;
            }
            case 1:{
                printf("B");
                break;
            }
            case 2:{
                printf("C");
                break;
            }
        }
        count++;
        pthread_mutex_unlock(&mutex);
        pthread_cond_broadcast(&order_cond);
    }
    return (void*)0;
}

int main(){
    pthread_t p_id[3];
    printf("fail\n");
    void* tret;
    for(int i = 0; i<3;i++){
        int success = pthread_create(&p_id[i], NULL, thread_print, (void*)&i);
        if(success != 0){
            printf("fail\n");
        }
    }
    for(int i = 0; i < 3; i++){
        pthread_join(p_id[i], &tret);
    }
    //sleep(10);
    pthread_mutex_lock(&mutex);
    while(count < 30){
        pthread_cond_wait(&order_cond, &mutex);
    }
    pthread_mutex_unlock(&mutex);
    return 0;
}

我做了一些 effort.When 我将 i 传递给线程函数 use int number = (int)num,它可以很好地工作。 那么当我将 i 的地址传递给线程函数时会发生什么?

But when I pass a stack variable's address to the thread function and the program just wait always.

将自动变量的地址传递给线程函数没有固有问题。事实上,这样做很常见。然而,一个问题,线程函数在指向的变量的生命周期结束后取消引用指针,如果指向的值的值可能会出现不同类型的问题-to变量被另一个线程改变,比如父线程。

您的程序出现了两种问题:它传递局部变量的地址 i 然后更改该变量的值,然后 然后 该变量的生命周期结束当循环终止时,这很容易在线程尝试使用指针之前发生。

您可以通过适当的同步来解决这两个问题,以确保线程函数在循环移动到下一次迭代之前读取变量。对于每个符合规范的实现,这将是一个正确的解决方案。

但是, i 转换为指针、传递结果并让线程函数将其转换回来可能更容易。两种转换都表现出实现定义的行为,但实际上,这在大多数系统上都可以正常工作。如果您想确定的话,您的实施文档应该提供足够的细节来确定它是否支持该方法。

例如:

    // Note that 'i' itself is converted to void *
    int success = pthread_create(&p_id[i], NULL, thread_print, (void *) i);
    // ...

void *thread_print(void *num) {
    // Note that 'num' is converted directly (back) to an int, not dereferenced
    int number = (int) num;
    // ...

还要注意,sleep() 并不能解决同步问题。它最好隐藏它们,但在您的特定情况下它会突出它们。