Y********f 发帖数: 410 | 1 贴个我写的吧,C++,有点繁琐,write优先。
class rwlock
{
public:
rwlock();
void rdlock();
void wrlock();
void unlock();
private:
pthread_mutex_t mutex;
pthread_cond_t condRead;
pthread_cond_t condWrite;
int readerCount;
int writeWaitCount;
bool writeLock;
};
rwlock::rwlock() : readerCount(0), writeLock(false), writeWaitCount(0)
{
pthread_mutex_init(&mutex, NULL); //forget NULL
pthread_cond_init(&condRead, NULL);
... 阅读全帖 |
|
l*******y 发帖数: 1498 | 2 能不能几个data structure公用一个mutex?
因为在某一时刻,不是所有的mutex都被用了。我们可以建一个hash table 存每个
mutex是否被使用的信息。当我们需要用一个mutex时,就去hash table里找一个当前没
在使用的mutex,然后update hash table标记此mutex已经被用了。每次要用mutex时都
去表里找。 |
|
w*s 发帖数: 7227 | 3 our product is OS, so provide these IPC calls,
can i generate random tests for them ?
say i generate random number of threads,
thread 1 creates and gets a mutex,
thread 2 has to wait for it.
The above case is simple, how about nested case,
thread 1 creates and gets a mutex, then i has to wait for an event from
another thread...
Q1: what are the constrains ? the only one i know is thread 2 has to wait
for
the mutex, only after mutex gets created.
Q2: How complicate can this go ? inside mutex blo... 阅读全帖 |
|
c**1 发帖数: 71 | 4 Make sure you understand my implementation. It is incorrect, but the place
you point out is correct. the update of write_count has to happen before
checkinh whether it is safe to continue.
The point of my flawed implementation is to show that the two requirements
you gave are not sufficient.
The following is my answer in one of my interviews.
read_lock() { lock_guard lk(mutex); while(writing) condition.wait(); ++read_
count;}
read_unlock() { lock_guard lk(mutex); if (--read_count == 0) conditi... 阅读全帖 |
|
c*****z 发帖数: 182 | 5 刚看了书上说win32里的mutex用了reference counting,如果没有线程引用它的话就自
动销毁。很困惑:假设线程A调用createmutex生成了个mutex,然后该线程没用它就退
出了。线程B想通过openmutex打开A产生的mutex,那如果A先退出了,此时是不是mutex
就被销毁了,B打不开了那? |
|
w****x 发帖数: 2483 | 6 mutex和semaphore实现读写锁
class ReadWriteMutex
{
public:
ReadWriteMutex(int maxReaders = 32)
: semaphore(maxReaders)
{
}
void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
QMutexLocker locker(&mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
}
void unlockWrite() { semaphore -= semaphore.total(); }
int maxReaders() const ... 阅读全帖 |
|
s********r 发帖数: 403 | 7 [1] That one mutex method shows what the reader / writer lock in semantics,
if you watch carefully, I did not provide the interface such as r_lock()
or w_lock().
That is sufficient to explain the functionality, if they do not try to hold
the mutex again, which does not make any sense. The write() operation only
do write job, it does not hold any mutex.
[2] spinlock() functions in hardware threads environment. It is pointless to
use spinlock() is a uniprocessor environment. And it is not used to... 阅读全帖 |
|
y*****1 发帖数: 53 | 8 题目大概是这样:有一个最基本的Mutex类,里面实现了最基本的lock()和unlock()。
但是如果用这个类,在lock里面再call一次lock的话,会造成deadlock。现在要求实现
一个新的Mutex类,可以使用原来的Mutex类,实现多层的lock。
求解。。。彻底茫然了。。。 |
|
e***m 发帖数: 92 | 9 re entrant mutex
mutex 在被lock时,记录下是哪个thread 调用它, 如果还是同一个thread调,就忽略
这个调用,如此可实现re entrant mutex. |
|
t****t 发帖数: 6806 | 10 notify_*() doesn't release mutex automatically.
The pthread_cond_broadcast() or pthread_cond_signal() functions may
be called
by a thread whether or not it currently owns the mutex that threads
calling
pthread_cond_wait() or pthread_cond_timedwait() have associated with
the con‐
dition variable during their waits; however, if predictable
scheduling behav‐
ior is required, then that mutex shall be locked by the thread
calling
pthread_cond_broadcas... 阅读全帖 |
|
t****t 发帖数: 6806 | 11 notify_*() doesn't release mutex automatically.
The pthread_cond_broadcast() or pthread_cond_signal() functions may
be called
by a thread whether or not it currently owns the mutex that threads
calling
pthread_cond_wait() or pthread_cond_timedwait() have associated with
the con‐
dition variable during their waits; however, if predictable
scheduling behav‐
ior is required, then that mutex shall be locked by the thread
calling
pthread_cond_broadcas... 阅读全帖 |
|
W***o 发帖数: 6519 | 12 我是这么理解的,用一个通俗的“排队上厕所大便"的例子来说一下我的体会:
1. Mutex:
一queue人排队等着去厕所大便,但是只有一个厕所,所以只能FIFO,一个人拿到钥匙
以后,如厕,锁门;这个时候后面所有的人都不能进去,必须要等前面的那个人大便完
开门以后,拿到钥匙才能进去如厕,锁门....
2. Semaphore:
还是一queue人排队等着去厕所大便,这个时候有3个厕所(不分男女假设),都需要钥
匙进去;钥匙都一样,这个需要有人告诉大家有几把钥匙available, 如果是0,那大家
耐心等。如果有1个钥匙,前面的一个人进去大便,如果有3把钥匙,一次放前面三个人
去厕所。
我只写过mutex, 但是看过semaphore 的实现代码,觉得差不多就是这意思,
连续两次RELEASE, mutex 可能就交换好两次钥匙了
semaphore 肯定就是放两个人进去大便了
请大牛赐教 |
|
s**********k 发帖数: 88 | 13 这是condition不是mutex
每个condition需要一个mutex保护,这个用来保护的mutex也是遵从同一个thread开关
的。 |
|
s********r 发帖数: 403 | 14 rw lock 有很多变种。
最基本的,如果不考虑 writer blocked by reader -> starvation, 感觉用一个
mutex 控制 counter 就够了。
fair 一点,用2个 mutex. |
|
s********r 发帖数: 403 | 15 [1] writer block reader
writer(),
一旦发现 reader == 0, 马上进入写操作不释放 mutex, 直到写完。
release_mutex() 发生在写完之后,release 之前 reader 无法占有 mutex 增加
counter,不能进入read 操作。
[2] 这个是最基本版,用来演示原理。考虑 performance,可以不用 reader / writer
lock,好的 option 针对具体 case 有的是。
另外,您觉得在多核系统中, goto sleep 是个好的 option 吗?
进程切换大约多少个时钟周期啊?
two |
|
r*******a 发帖数: 7 | 16 我想用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_unlo... 阅读全帖 |
|
r*******a 发帖数: 7 | 17 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. |
|
w*s 发帖数: 7227 | 18 【 以下文字转载自 Programming 讨论区 】
发信人: wds (大盘5000不是梦), 信区: Programming
标 题: 连续release mutex/semphore 2次有什么问题吗?
发信站: BBS 未名空间站 (Wed Aug 6 21:03:43 2014, 美东)
据说mutex, semphore的情况是不同的,你们怎么看? |
|
W*****x 发帖数: 684 | 19 mutex是sem的一个特例, 连续release对mutex没用, sem有. |
|
y***y 发帖数: 295 | 20 【 以下文字转载自 JobHunting 讨论区 】
发信人: sherwoodluo (sherwoodluo), 信区: JobHunting
标 题: Re: 刚才的电话面试,一点总结
发信站: BBS 未名空间站 (Mon Feb 25 16:39:11 2008)
关于mutex和semaphore的差别我被问到好几次。从来没有找到标准答案。我的理解是
mutex用于互斥访问,而且是忙等待。而semephore则是用于资源管理,属于阻塞等待
。大家指教一下?
,比较简单的一些问题
pattern, boost, standard template library, 需要的我可以邮件给大家 |
|
c*****z 发帖数: 182 | 21 那如果线程A生成了一个mutex,然后线程结束了,之后也再没线程用这个mutex,那它
什么时候被销毁那 |
|
p***o 发帖数: 1252 | 22 你的需求是啥?没法open自然能检测出来:
OpenMutex
Return Value
...
If a named mutex does not exist, the function fails and GetLastError returns
ERROR_FILE_NOT_FOUND.
还有这一段:
If your multi-threaded application must repeatedly create, open, and close
a named mutex object, a race condition can occur. In this situation, it is b
etter to use CreateMutex instead of OpenMutex, because CreateMutex opens a m
utex if it exists and creates it if it does not. |
|
h****b 发帖数: 157 | 23 binary semaphore 和 mutex 有啥分别? google过,好像说是ownership不一样,BS
任何
thread可以release, mutex只允许lock他的release. 谁给讲讲?先谢了 |
|
b********n 发帖数: 609 | 24 【 以下文字转载自 JobHunting 讨论区 】
发信人: forbaby2008 (宝宝), 信区: JobHunting
标 题: Re: 问个cisco的mutex的面试题
发信站: BBS 未名空间站 (Thu Oct 14 13:49:54 2010, 美东)
up一下
后最后问了这一个mutex的题,我实在答不上来,就问他应该怎么做,很简单的回答我
用hashtable,完了上网搜也没搜到答案,来这里问问,希望大牛给指点指点 |
|
b******n 发帖数: 592 | 25 I had same issues before. It is very difficult to really test mutex.
valgrind may help. My solution was to mock mutexes, and hand craft 'senarios
' for testing. Then have a very long test with random executions. |
|
x****o 发帖数: 21566 | 26 1) semaphore has two types: counting semaphore and binary semaphore
2) mutex is similar to binary semaphore but with one biggest difference
"the principle of ownership"
Mutex requires ownership, semaphore does not.
That's it. |
|
i**p 发帖数: 902 | 27 Java synchronized method 在同一thread里是可以多次进入的. 锁是在thread上的.
C++ mutex 在释放之前被同一thread再次调用会造成死锁, 它是锁在mutex这个变量上
的.
请评论. |
|
w***g 发帖数: 5958 | 28 我也是一直莫名地以为mutex是线程专有的,IPC没有mutex。
后来经wflower指教发现确乎是有的。 |
|
f*********8 发帖数: 34 | 29 系统中有很多data structure, 怎么减少mutex的数量? 原话就是这样问的,很郁闷。 |
|
f*********8 发帖数: 34 | 30 up一下
后最后问了这一个mutex的题,我实在答不上来,就问他应该怎么做,很简单的回答我
用hashtable,完了上网搜也没搜到答案,来这里问问,希望大牛给指点指点 |
|
r**u 发帖数: 1567 | 31 这个当然有关系了, data structure越多, 如果多个thread都操作这些data structure
, 每个data structure都要加锁保护.
可以把data structure合并, 减少lock的数目. 但是, 锁起来的code越多, delay就越
大, there is some trade-off.
后最后问了这一个mutex的题,我实在答不上来,就问他应该怎么做,很简单的回答我
用hashtable,完了上网搜也没搜到答案,来这里问问,希望大牛给指点指点 |
|
s********r 发帖数: 403 | 32 我觉得大致是这样
[reader]:
get_mutex();
reader++;
release_mutex()
Read();
get_mutex();
reader--;
release_mutex()
[writer]:
for (;;)
{
get_mutex();
if (reader > 0)
{
release_mutex();
}
write();
release_mutex();
break; // Made it
}
如果想防止 reader block writer forever (starvation), 再加一个 mutex_wr, 当
writer 抢到 mutex_wr 后,阻止 reader++,直到 reader 全部释放,writer 能够拿
到第一个 mutex 进行写操作。 |
|
s********r 发帖数: 403 | 33 #1 You are right about the notification by condition number.
My point is that using one mutex is the easiest way to explain what a basic
reader / writer lock is, before moving to more details.
#2 In certain situations, for SMP, local spin can be better, that is how
spinlock works in kernel. |
|
c**1 发帖数: 71 | 34 if an implementation is incorrect, it doesn't matter whether it is simple.
In user space, thread can go to sleep at any time, nothing can guarantee
that a spin can always get the lock, so all spinlocks in user space have a
limit on how long it can spin, and if it still does not get the lock, it
suspends itself, waiting for other to wake it up. It is impossible to use
only mutex to implement this suspend-and-wait-for-notify.
Even in kernel, it is very hard to get the guarantee that the lock hol... 阅读全帖 |
|
s********r 发帖数: 403 | 35 The semantics of reader / writer lock is:
[1] Allow multiple readers for read_op(), when reader reads, no writer can
write
[2] When writer perform write_op(), no reader can read_op() successfully.
Would you think that one mutex implementation can describe the above
semantics?
Besides, what do you mean:
"Even in kernel, it is very hard to get the guarantee that the lock holder
does not block on anything and will release the lock in a timely manner."
In Linux kernel, the spinlock works on hardwar... 阅读全帖 |
|
c**1 发帖数: 71 | 36 Yellowleaf's implementation looks correct to me. some minor issue though.
* in c++, it is better to use RAII to lock / unlock mutex
* no need to signal if --readerCount != 0 |
|
|
h*******0 发帖数: 270 | 38 如果用synchronized方法可以吗? 不用mutex。这两个有什么区别?优劣? |
|
h*******0 发帖数: 270 | 39 如果用synchronized方法可以吗? 不用mutex。这两个有什么区别?优劣? |
|
r*******a 发帖数: 7 | 40 Mutex needs to put thread on sleep (no busy waiting), how do I block the
thread and wake them up? |
|
q****x 发帖数: 7404 | 41 【 以下文字转载自 JobHunting 讨论区 】
发信人: quantx (X矿工), 信区: JobHunting
标 题: 谁给讲讲test-and-set怎么实现mutex?
发信站: BBS 未名空间站 (Thu Dec 1 22:06:08 2011, 美东)
wiki没看懂。给的例子不是死循环吗?
void lock(bool *lock) {
while (test_and_set (lock) == 1);
}
比如下面的代码怎么映射到汇编里的test-and-set?
pthread_mutex_lock_t xlock;
pthread_mutex_lock(&xlock);
pthread_mutex_unlock(&xlock); |
|
c******n 发帖数: 4965 | 42 mutex implementation is either dead lock or you need support in operating
system, hook up to schedule() call |
|
P*****f 发帖数: 2272 | 43 mutex: must be release by the owner.
semaphore: could be release by other party
简单来说我觉得就是酱紫啊 |
|
|
P*****f 发帖数: 2272 | 45 not really. We are talking about concept only.
In classic OS book, you know mutex has owner concept, while "semaphore"
not.
there is one of the fundamental differences between these two.
This depends on specific implementations. |
|
B*****g 发帖数: 19 | 46 各有各的用途吧。
monitor 可以带内部的conditional variable,也就是说里边还可以有一个waiting
queue.
semaphore 和 mutex 区别比较本质。 |
|
m****t 发帖数: 37 | 47 mutex和lock我感觉是一样的,但是好像又感觉有区别
哪位给我说说吧 |
|
k***r 发帖数: 4260 | 48 mutex is a lock. Not the other way around. |
|
g*****g 发帖数: 34805 | 49 mutex as it's named, is mutually exclusive, a binary semophore |
|
m****t 发帖数: 37 | 50 so, mutex & lock are different? |
|