互斥锁挂起或不起作用
Mutex lock either hangs or doesn't work
我正在使用 pthreads 在父进程旁边创建一个子进程。我正在尝试使用互斥锁在子进程中的第一个打印语句之后停止并在父进程中的第二个打印语句之后恢复:
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *print(void *attr)
{
printf("I am the child process\n");
pthread_mutex_lock(&lock);
pthread_cond_wait(&cond, &lock);
printf("The child process is done\n");
pthread_mutex_unlock(&lock);
return 0;
}
int main()
{
pthread_t child;
pthread_mutex_lock(&lock);
printf("I am the parent process\n");
pthread_create(&child, NULL, print, NULL);
pthread_join(child, NULL);
printf("The parent process is done\n");
pthread_mutex_unlock(&lock);
pthread_cond_signal(&cond);
return 0;
}
原输出如下:
I am the parent process
I am the child process
The child process is done
The parent process is done
我正在寻找的输出如下:
I am the parent process
I am the child process
The parent process is done
The child process is done
我这辈子都想不出如何使用互斥锁来实现这一点,我最接近的是它在无限期挂起之前达到前两个语句。我也有原始输出,好像互斥语句什么都不做。
谁能帮我解决这个问题?提前致谢。
您只需要更好地在父线程和子线程之间发送信号。应该这样做:
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int child_done = 0;
int parent_done = 0;
void *print(void *attr)
{
printf("I am the child process\n");
pthread_mutex_lock(&lock);
child_done = 1;
pthread_cond_broadcast(&cond); // Because this thread also waits
while (!parent_done)
pthread_cond_wait(&cond, &lock);
printf("The child process is done\n");
pthread_mutex_unlock(&lock);
return 0;
}
int main()
{
pthread_t child;
pthread_mutex_lock(&lock);
printf("I am the parent process\n");
pthread_create(&child, NULL, print, NULL);
while (!child_done)
pthread_cond_wait(&cond, &lock);
printf("The parent process is done\n");
parent_done = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
pthread_join(child, NULL);
return 0;
}
我正在使用 pthreads 在父进程旁边创建一个子进程。我正在尝试使用互斥锁在子进程中的第一个打印语句之后停止并在父进程中的第二个打印语句之后恢复:
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *print(void *attr)
{
printf("I am the child process\n");
pthread_mutex_lock(&lock);
pthread_cond_wait(&cond, &lock);
printf("The child process is done\n");
pthread_mutex_unlock(&lock);
return 0;
}
int main()
{
pthread_t child;
pthread_mutex_lock(&lock);
printf("I am the parent process\n");
pthread_create(&child, NULL, print, NULL);
pthread_join(child, NULL);
printf("The parent process is done\n");
pthread_mutex_unlock(&lock);
pthread_cond_signal(&cond);
return 0;
}
原输出如下:
I am the parent process
I am the child process
The child process is done
The parent process is done
我正在寻找的输出如下:
I am the parent process
I am the child process
The parent process is done
The child process is done
我这辈子都想不出如何使用互斥锁来实现这一点,我最接近的是它在无限期挂起之前达到前两个语句。我也有原始输出,好像互斥语句什么都不做。
谁能帮我解决这个问题?提前致谢。
您只需要更好地在父线程和子线程之间发送信号。应该这样做:
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int child_done = 0;
int parent_done = 0;
void *print(void *attr)
{
printf("I am the child process\n");
pthread_mutex_lock(&lock);
child_done = 1;
pthread_cond_broadcast(&cond); // Because this thread also waits
while (!parent_done)
pthread_cond_wait(&cond, &lock);
printf("The child process is done\n");
pthread_mutex_unlock(&lock);
return 0;
}
int main()
{
pthread_t child;
pthread_mutex_lock(&lock);
printf("I am the parent process\n");
pthread_create(&child, NULL, print, NULL);
while (!child_done)
pthread_cond_wait(&cond, &lock);
printf("The parent process is done\n");
parent_done = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
pthread_join(child, NULL);
return 0;
}