时间: 2020-11-26|45次围观|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; }

转载于:https://www.cnblogs.com/liugangBlog/p/11267166.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/101385856

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《c++实现互斥锁
   

还没有人抢沙发呢~