【发布时间】:2012-12-28 22:33:44
【问题描述】:
如果您有一个线程 (thread1) 阻塞在 sem_wait() 上,而另一个线程 (thread2) 使用 sem_destroy() 破坏了该信号量,那么 thread1 会发生什么?
A quick search on the internet tells me that it produces undefined behavior:
销毁其他进程或线程当前被阻塞的信号量(在 sem_wait(3) 中)会产生未定义的行为。
但是,我碰巧看到它在许多多线程 c++ 应用程序中使用。
我的主要问题:
- 这有什么目的吗?
- 他们试图达到什么目的(例如,这会隐式终止线程)?
- 那不是很不安全吗?
【问题讨论】:
-
it just so happens that I've been reading lately many multi-threaded c++ applications from here and there to learn some nice coding practices and I have seen this done multiple times这并不奇怪。即使您看到它在 linux 内核中完成,也不要依赖未定义的行为。