C++ 在main函数中调用join child pthread
我有测试代码:C++ 在main函数中调用join child pthread,c++,c,main,pthread-join,pthread-exit,C++,C,Main,Pthread Join,Pthread Exit,我有测试代码: #include <stdio.h> #include <unistd.h> #include <pthread.h> pthread_t th_worker, th_worker2; void * worker2(void *data) { for(int i = 0; i< 1000000; i++){ printf("thread for worker2----%d\n", i);
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
pthread_t th_worker, th_worker2;
void * worker2(void *data) {
for(int i = 0; i< 1000000; i++){
printf("thread for worker2----%d\n", i);
usleep(500);
}
}
void * worker(void *data){
pthread_create(&th_worker2, NULL, worker2, data);
for(int i = 0; i< 100; i++){
printf("thread for worker-----%d\n", i);
usleep(500);
}
}
void join(pthread_t _th){
pthread_join(_th, NULL);
}
-->段故障错误
否则,我呼吁:
join(the_worker);
join(th_worker2);
--->嗯
为什么在上述情况下会出现段故障错误?
谢谢你的帮助 如果你发布了所有代码,你就有了一个竞态条件
main
与worker
的开始同步,但不与worker2
同步
也就是说,main
在worker
有机会调用pthread\u create
并使用有效的[non-null]值设置thu worker2
之前,试图加入thu worker2
因此,在第二个pthread\u create
完成之前,thu worker2
将无效,但对于main
来说已经太晚了。它已经获取了THU worker2
,该值为空,并且main
将出错
当您为th\u worker
添加联接时,它会起作用,因为它保证了同步和无竞争条件
要在无连接的情况下实现此保证,请执行以下主要操作:
int
main()
{
char *str = "hello thread";
pthread_create(&th_worker, NULL, worker, (void *) str);
// give worker enough time to properly start worker2
while (! th_worker2)
usleep(100);
/* problem in here */
join(th_worker2);
return 1;
}
更好的方法是添加一个额外的变量。这样一来,就不需要第一个循环了(但我把它留在了里面):
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int worker_running;
pthread_t th_worker;
int worker2_running;
pthread_t th_worker2;
void *
worker2(void *data)
{
// tell main we're fully functional
worker2_running = 1;
for (int i = 0; i < 1000000; i++) {
printf("thread for worker2----%d\n", i);
usleep(500);
}
return NULL;
}
void *
worker(void *data)
{
// tell main we're fully functional
worker_running = 1;
pthread_create(&th_worker2, NULL, worker2, data);
for (int i = 0; i < 100; i++) {
printf("thread for worker-----%d\n", i);
usleep(500);
}
return NULL;
}
void
join(pthread_t _th)
{
pthread_join(_th, NULL);
}
int
main()
{
char *str = "hello thread";
pthread_create(&th_worker, NULL, worker, (void *) str);
// give worker enough time to properly start worker2
// NOTE: this not necessarily needed as loop below is better
while (! th_worker2)
usleep(100);
// give worker2 enough time to completely start
while (! worker2_running)
usleep(100);
/* problem in here (not anymore!) */
join(th_worker2);
return 1;
}
#包括
#包括
#包括
int-worker_运行;
pthread_t_工作线程;
int worker2_运行;
pthread_t_worker2;
空虚*
worker2(无效*数据)
{
//告诉main我们已经完全正常了
工人2_运行=1;
对于(int i=0;i<1000000;i++){
printf(“工作线程2---%d\n”,i);
美国LEEP(500);
}
返回NULL;
}
空虚*
工人(无效*数据)
{
//告诉main我们已经完全正常了
工人_运行=1;
pthread_create(&th_worker2,NULL,worker2,data);
对于(int i=0;i<100;i++){
printf(“工作线程------%d\n”,i);
美国LEEP(500);
}
返回NULL;
}
无效的
连接(pthread_t_th)
{
pthread_join(_th,NULL);
}
int
main()
{
char*str=“你好线程”;
pthread_create(&th_worker,NULL,worker,(void*)str);
//给工人足够的时间正确开始工作2
//注意:这不一定需要,因为下面的循环更好
而(!THU worker2)
usleep(100);
//给工人2足够的时间完全开始工作
当(!worker2\u运行时)
usleep(100);
/*这里的问题(不再是了!)*/
加入(工作人员2);
返回1;
}
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int worker_running;
pthread_t th_worker;
int worker2_running;
pthread_t th_worker2;
void *
worker2(void *data)
{
// tell main we're fully functional
worker2_running = 1;
for (int i = 0; i < 1000000; i++) {
printf("thread for worker2----%d\n", i);
usleep(500);
}
return NULL;
}
void *
worker(void *data)
{
// tell main we're fully functional
worker_running = 1;
pthread_create(&th_worker2, NULL, worker2, data);
for (int i = 0; i < 100; i++) {
printf("thread for worker-----%d\n", i);
usleep(500);
}
return NULL;
}
void
join(pthread_t _th)
{
pthread_join(_th, NULL);
}
int
main()
{
char *str = "hello thread";
pthread_create(&th_worker, NULL, worker, (void *) str);
// give worker enough time to properly start worker2
// NOTE: this not necessarily needed as loop below is better
while (! th_worker2)
usleep(100);
// give worker2 enough time to completely start
while (! worker2_running)
usleep(100);
/* problem in here (not anymore!) */
join(th_worker2);
return 1;
}