下面是有问题的伪代码:
int c;
pthread_mutex_t mtx;
void inc(int count)
{
pthread_mutex_lock(&mtx);
c += count;
pthread_mutex_unlock(&mtx);
}
int main(void)
{
pthread_mutex_init(&mtx);
signal(SIGUSR1, inc);
signal(SIGUSR2, inc);
sleep(100000); // Sleep for long enough
return 0;
}
这段代码如何以及为什么会导致死锁?
为什么这与以下场景不同:
- Thread 1 acquires the mutex.
- Context switch is made and Thread 2 tries to get the lock and put on waiting list.
- Thread 1 finishes and releases the lock.
- Thread 2 wakes up and continues its execution.
- No deadlock.
最佳答案
您的信号处理程序都将在同一个线程中运行。如果第二个信号到达,而第一个信号的处理程序已锁定互斥量,则您的独立线程将再次尝试锁定互斥量和死锁:
time thread 0
---- --------
0 main:...
1 main:sleep()
... ...
100 <<SIGUSR1>>
101 inc:pthread_mutex_lock()
102 inc:count += ...
103 <<SIGUSR2>>
104 inc:pthread_mutex_lock() // deadlock
关于c - 为什么在信号处理程序中使用互斥锁会出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32413397/