由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请问pure storage 的那道用spin lock and flags to implement mutex怎么做
相关主题
read-write locker的实现急求大神指导一道面经
一个多线程的题目,这个写法可以过关么面试题
请教大牛用mutex lock实现reader writer lock请问C++ threading w/ lock free algorithms
multi-threading guru们一个thread如何kill另外一个thread?
谁给讲讲test-and-set怎么实现mutex?人生中第一次面试
请教一下那道H2O的题embedded Linux ,面试了几次,都问这个问题了。不知道答案??
碰到面试官水平太差看不懂答案怎么办?找工总结(弱面经和offer)
是不是被印度人故意往沟里带FLAG rej/offer 求比较
相关话题的讨论汇总
话题: lock话题: mutex话题: flag话题: spin话题: pthread
进入JobHunting版参与讨论
1 (共1页)
r*******a
发帖数: 7
1
我想用conditional variable, 可是pthread_cond_wait() only take mutex. 难道要
自己写cv_wait() 和cv_signal?
我的思路:
typedef struct mutex {
pthread_spin_lock_t lock;
int flag;
} mutex_t;
void mutex_lock(mutex_t *m)
{
pthread_spin_lock(&m->lock);
while (flag == 1)
cv_wait(flag, &m->lock); // I don't know which cv_wait to use that will
take spin lock
flag = 1;
pthread_spin_unlock(&m->lock);
}
void mutex_unlock(mutex_t *m)
{
pthread_spin_lock(&m->lock);
flag = 0;
cv_signal(flag);
pthread_spin_unlock(&m->lock);
}
a******7
发帖数: 106
r*******a
发帖数: 7
3
Mutex needs to put thread on sleep (no busy waiting), how do I block the
thread and wake them up?

【在 a******7 的大作中提到】
: http://en.cppreference.com/w/c/atomic/atomic_compare_exchange
a******7
发帖数: 106
4
atomic flag(0);
void lock() {
while (flag.compare_exchange_strong(0, 1));
}
void unlock() {
flag = 0;
}
其实bool就够了
http://en.cppreference.com/w/cpp/atomic/atomic_flag
r*******a
发帖数: 7
5
Sorry that I can't type chinese.
The difference between spin lock and mutex is that spin lock will do busy
wait and hog the cpu, but mutex wont. Using this atomic flag, the thread
will be still busy waiting.

【在 a******7 的大作中提到】
: atomic flag(0);
: void lock() {
: while (flag.compare_exchange_strong(0, 1));
: }
: void unlock() {
: flag = 0;
: }
: 其实bool就够了
: http://en.cppreference.com/w/cpp/atomic/atomic_flag

a******7
发帖数: 106
6
哦 我明白你的意思了 我不熟pthread 但这样行吗
condition_variable cv;
void lock() {
while (flag == 1) cv.wait();
flag = 1;
}
void unlock() {
flag = 0;
cv.notify_all();
}
c*********i
发帖数: 36
7
马上onsite了,求好心人解答,如何没有busy waiting
1 (共1页)
进入JobHunting版参与讨论
相关主题
FLAG rej/offer 求比较谁给讲讲test-and-set怎么实现mutex?
问一道multithreading的题请教一下那道H2O的题
问个dutch flag碰到面试官水平太差看不懂答案怎么办?
thread safe hash table是不是被印度人故意往沟里带
read-write locker的实现急求大神指导一道面经
一个多线程的题目,这个写法可以过关么面试题
请教大牛用mutex lock实现reader writer lock请问C++ threading w/ lock free algorithms
multi-threading guru们一个thread如何kill另外一个thread?
相关话题的讨论汇总
话题: lock话题: mutex话题: flag话题: spin话题: pthread