首页 > 编程语言 > 详细

c++实现互斥锁

时间:2019-07-30 03:32:13      阅读:137      评论:0      收藏:0      [点我收藏+]
typedef struct __lock_t
{
int flag;
} lock_t;

int TestAndSet(int *ptr, int new)
{
int old = *ptr;
*ptr = new;
return old;
}

void init(lock_t *mutex)
{
// 0: lock is available
// 1: lock is held
mutex->flag = 0;
}

void lock(lock_t *mutex)
{
while (mutex->flag == 1)
{ // Test the flag.
; // Wait the lock
mutex->flag = 1; // Set the lock, i.e. start to hold lock
}
}

void unlock(lock_t * mutex)
{
mutex->flag = 0;
}

c++实现互斥锁

原文:https://www.cnblogs.com/liugangBlog/p/11267166.html

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