没法贴图,发个博客吧,关于lock
(2012-06-30 12:12:10)
标签:
杂谈 |
关于微博上提到的,pthread mutex的等待排队的实验。
我理解的这个mutex是直接被阻在队列中,由释放者来进行调度,所以是非常重量级的,如果资源短暂开闭,则应用spinlock为佳,例如对一块内存的某个字节进行外科手术打击,则spinlock快进快出好。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include<iostream>
using namespace std;
static pthread_mutex_t foo_mutex;
void* get_maokeng(void* param)
{
}
void* go_maokeng(void* param)
{
};
int main()
{
}
运行结果:【排队的效果很明显】
i like to paifang shit! 0
i first get maokeng! haha! 0
i like to paifang shit! 1
i like to paifang shit! 2
i like to paifang shit! 3
i like to paifang shit! 4
i like to paifang shit! 5
i like to paifang shit! 6
i like to paifang shit! 7
i like to paifang shit! 8
i like to paifang shit! 9
i get maokeng! 1
i get maokeng! 2
i get maokeng! 3
i get maokeng! 4
i get maokeng! 5
i get maokeng! 6
i get maokeng! 7
i get maokeng! 8
i get maokeng! 9