-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
线程之间的竞争 #5
Comments
我觉得会. 这种情况, 执行 |
我试了下, 确实应该加锁: #include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void *thread_a(void *arg)
{
printf("thread a start\n");
sleep(2);
printf("arg value(a): %d\n", *(int *)arg);
printf("thread a end.\n");
return NULL;
}
void *thread_b(void *arg)
{
printf("thread b start\n");
printf("arg value(b): %d\n", *(int *)arg);
printf("thread b change *arg to 10\n");
*(int *)arg = 10;
printf("thread b end.\n");
return NULL;
}
int main()
{
int a = 5;
pthread_t pthread_a;
pthread_t pthread_b;
pthread_create(&pthread_a, NULL, thread_a, (void *)&a);
sleep(1);
pthread_create(&pthread_b, NULL, thread_b, (void *)&a);
pthread_join(pthread_a, NULL);
pthread_join(pthread_b, NULL);
return 0;
} 输出:
不过说回来, 解引用就在线程入口处, 感觉一般情况不会出问题. 个人感觉这里用条件变量就好. |
确实会发生,已经验证过了,因为我们保证不了线程的运行顺序。能想到的解决办法就是加锁,在accept前加锁,在新创建的线程中解锁。 |
可以这么改,不必使用锁。因为参数是以值传递的。
accept_request中这样将参数转换为int:
|
确实是这样的,huntinux 的方法更好,没必要使用锁,多谢。 |
Merged
我感觉可以改成以下的内容,使用动态分配,在线程里释放内存空间,参考了UNP
|
fix thread conflict when passing arg as pointer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
在main函数中,传递给线程的参数client_sock是一个local variable,会产生竞争吗?
if (pthread_create(&newthread , NULL, (void *)accept_request, (void *)&client_sock) != 0) perror("pthread_create");
比如连接1对应的线程运行前,连接2把client_sock修改了。
是否应该在堆上分配
The text was updated successfully, but these errors were encountered: