您的位置:首页 > 移动开发 > Objective-C

mutex 和 semaphone 的区别 (1)

2010-09-04 12:13 423 查看
MSDN:

 

A thread that owns a mutex object can wait repeatedly for the same mutex object
to become signaled without its execution becoming blocked. A thread that waits
repeatedly for the same semaphore object, however, decrements the semaphore's
count each time a wait operation is completed; the thread is blocked when the
count gets to zero. Similarly, only the thread that owns a mutex can
successfully call the ReleaseMutex
function, though any thread can use ReleaseSemaphore to increase the count of
a semaphore object.

 

 

 

本以为数量为1的semaphone和mutex没有区别,读了上面这段说明才知道,mutex的owner线程可以无限次成功调用ReleaseMutex,而ReleaseSemaphore则不判断调用是否来自owner线程。感觉上这种设计让semaphore更符合计数器的这样一个语义。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息