首页 > 其他 > 详细

Operating System: Three Easy Pieces --- Pthread Locks (Note)

时间:2015-10-26 13:28:48      阅读:242      评论:0      收藏:0      [点我收藏+]

The name that the POSIX library uses for a lock is a mutex, as it is used to provide mutual

exclusion between threads, i.e., if one thread is in the critical section, it excludes the others

from entering until it has completed the section. Thus, when you see the following POSIX

threads code, you should understand that it is doing the same thing as above (we again use

wrappers that check for errors upon lock and unlock):

pthread_mutex_t lock  = PTHREAD_MUTEX_INITITALIZED;

pthread_mutex_lock(&lock);

balance = balance + 1;

pthread_mutex_unlock(&lock);

You might also notice here that the POSIX version passes a variable to lock and unlock, as we

may be using different locks to protect different variables. Doing so can increase concurrency:

instead of one big lock that is used any time any critical section is accessed (a coarse-gained

locking strategy), one will often protect different data and data structures with different locks,

thus allowing more threads to be in locked code at once (a more fine-grained approach).

Operating System: Three Easy Pieces --- Pthread Locks (Note)

原文:http://www.cnblogs.com/miaoyong/p/4910796.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!