C多线程共享变量

时间:2016-02-09 21:14:21

标签: c multithreading

我需要C语言中的多线程帮助。

最初,我在client函数中有一个名为main的变量。

void *my_function (void *arg) {
    int *client = (int*)arg;
    *client = 5;
    return 0;
}

void *my_function2 (void *arg) { ... }
void *my_function3 (void *arg) { ... }


int main()
{
    int client = 0;
    pthread_t connect_thread, func2_thread, func3_thread;
    pthread_create(&connect_thread, NULL, my_function, (void *)&client);
    pthread_create(&func2_thread, NULL, my_function2, (void *)&client);
    pthread_create(&func3_thread, NULL, my_function3, (void *)&client);

    // how can i make it such that after executing my_function, client's value = 5 in main()??

    pthread_join(my_function, NULL);

    // once the value is updated, i will pass this updated value into another thread

    pthread_join(my_function2, NULL);
    pthread_join(my_function3, NULL);

    return 0;
}

如何在执行client后将mainmy_function的值从0更改为5?

2 个答案:

答案 0 :(得分:1)

代码的问题在于,当main()完成时,它会终止整个程序,包括所有其他线程。您的代码确实会修改client的值,但它不会以安全的方式(通常)执行此操作,因为您应该使用互斥锁来保护在多个线程中访问的数据。在线程完全创建之前,您的程序很可能会终止。

您需要在代码中添加互斥锁,并在允许pthread_join()完成main()之前使用return等待线程完成。请参阅下面的更正示例。

代码清单

/*******************************************************************************
 * Preprocessor directives
 ******************************************************************************/
#include <stdio.h>
#include <pthread.h>


/*******************************************************************************
 * Globals
 ******************************************************************************/
pthread_mutex_t mDataMutex = PTHREAD_MUTEX_INITIALIZER;

/*******************************************************************************
 * Function prototypes
 ******************************************************************************/
void *my_function(void *arg);


/*******************************************************************************
 * Function definitions
 ******************************************************************************/
/*----------------------------------------------------------------------------*/
void *my_function(void *arg)
{
    int *client = (int*)arg;
    printf("Thread started.\n");

    pthread_mutex_lock(&mDataMutex);
    *client = 5;
    pthread_mutex_unlock(&mDataMutex);

    printf("Thread completed.\n");

    return 0;
}


/*----------------------------------------------------------------------------*/
int main(void)
{
    int client = 0;
    int rc;
    void *status;
    pthread_t connect_thread;
    pthread_attr_t attr;

    // Print initial value of variable.
    printf("Initial value of client:%d.\n", client);

    /* Initialize and set thread detached attribute */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    // Create the thread and start it.
    rc = pthread_create(&connect_thread, &attr, my_function, (void *)&client);
    if (rc)
    {
        printf("ERROR; return code from pthread_create() is %d\n", rc);
        return (-1);
    }

    // Clean  up the attribute struct, don't need it anymore
    pthread_attr_destroy(&attr);

    // Wait for the thread to complete.
    rc = pthread_join(connect_thread, &status);

    // Print update value of client
    pthread_mutex_lock(&mDataMutex);
    printf("Updated value of client:%d.\n", client);
    pthread_mutex_unlock(&mDataMutex);

    return 0;
}

样本输出

Initial value of client:0.
Thread started.
Thread completed.
Updated value of client:5.

答案 1 :(得分:0)

您的代码已经更改了值。代码中的错误位于pthread_join调用中,当您传递给pthread_t调用时,将connect_thread类型的线程ID(在您的情况下为#include<pthread.h> #include<stdio.h> void *my_function (void *arg) { int *client = (int*)arg; *client = 5; return 0; } int main() { int client = 0; pthread_t connect_thread, func2_thread, func3_thread; pthread_create(&connect_thread, NULL, my_function, (void *)&client); // how can i make it such that after executing my_function, client's value = 5 in main()?? pthread_join(connect_thread, NULL); // once the value is updated, i will pass this updated value into another thread printf("%d\n", client); return 0; } 变量)作为输入它的功能。

lead()

另一种方法是使用全局变量和互斥量来更改值。