gpt4 book ai didi

C 多线程共享变量

转载 作者:行者123 更新时间:2023-11-30 20:23:53 25 4
gpt4 key购买 nike

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

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

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;
}

执行my_function后,如何将mainclient的值从0更改为5?

最佳答案

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

您需要在代码中添加互斥体,并使用 pthread_join() 等待线程完成,然后才允许 main() 完成并返回。请参阅下面更正后的示例。

<小时/>

代码 list

<小时/>
/*******************************************************************************
* 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.
<小时/>

关于C 多线程共享变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35302308/

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