由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 精华区翻出来的MS老题,thread safe
相关主题
pthread mutex能不能用与thread和process之间Monitor和semaphore, mutex是什么关系?
thread, semaphore, 问题。问个semaphore 和 mutex的问题
thread c++ 问题编程题又一道
Restaurant Reservation System...question about the read/write locker
about critical sectionc++是不是准备加一个glue layer把系统给隔离出来?
how to answer this question, thanks问一个读写锁的问题
mutex和semaphore的差别到底是什么?java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?
谁能推荐一个read-writer lock的C++实现? (转载)连续release mutex/semphore 2次有什么问题吗?
相关话题的讨论汇总
话题: mutex话题: semaphore话题: thread话题: binary话题: int
进入Programming版参与讨论
1 (共1页)
r******m
发帖数: 6
1
“2)写一个circular queue,要求thread safe”
safe倒是简单,弄个mutex就行,但是策略应该如何?
比如,一个线程要插入,检测到锁了,是报错返回呢,还是等?
有没有大虾讲解讲解,谢谢了。
g*****g
发帖数: 34805
2
sleep一会,重新试。更高级的策略是注册到一个队列上,写线程结束之后
唤醒这个队列。

【在 r******m 的大作中提到】
: “2)写一个circular queue,要求thread safe”
: safe倒是简单,弄个mutex就行,但是策略应该如何?
: 比如,一个线程要插入,检测到锁了,是报错返回呢,还是等?
: 有没有大虾讲解讲解,谢谢了。

D*******a
发帖数: 3688
3
mutex不是OS自己给的功能么?
线程可以有多种方法操作mutex的吧,比如死等和等固定长时间然后返回。

【在 r******m 的大作中提到】
: “2)写一个circular queue,要求thread safe”
: safe倒是简单,弄个mutex就行,但是策略应该如何?
: 比如,一个线程要插入,检测到锁了,是报错返回呢,还是等?
: 有没有大虾讲解讲解,谢谢了。

b***y
发帖数: 2799
4
就是啊,死等就行啊,另一个一释放,它就拿到了。这又不存在semaphore的问题。

【在 D*******a 的大作中提到】
: mutex不是OS自己给的功能么?
: 线程可以有多种方法操作mutex的吧,比如死等和等固定长时间然后返回。

r******m
发帖数: 6
5
谢谢各位。
看来我对mutex的理解不对,我想说的应该是semaphore。
老大能不能顺便讲讲mutex跟semaphore的区别?
(google了一下,不是太明白)
g*****g
发帖数: 34805
6
mutex就是只有一个坑位,semaphore就是可以有多个坑位。
都是坑满了就得外面排队。

【在 r******m 的大作中提到】
: 谢谢各位。
: 看来我对mutex的理解不对,我想说的应该是semaphore。
: 老大能不能顺便讲讲mutex跟semaphore的区别?
: (google了一下,不是太明白)

D*******a
发帖数: 3688
7
我倾向于把semaphore理解成小票

【在 g*****g 的大作中提到】
: mutex就是只有一个坑位,semaphore就是可以有多个坑位。
: 都是坑满了就得外面排队。

b***y
发帖数: 2799
8
Maybe my answer upstairs misled you.
mutex is just a binary semaphore. The only difference is that semaphore
controls several resources whereas mutex controls only one.
As of waiting or returning after detecting a locked mutex, it depends on the
user of the mutex, not the mutex or semaphore itself.
It's like you go to a public restroom and find all the toilets are occupied.
It's up to you to just wait there or go tell your dad. It doesn't matter
how many toilets there, one or many, they are al

【在 r******m 的大作中提到】
: 谢谢各位。
: 看来我对mutex的理解不对,我想说的应该是semaphore。
: 老大能不能顺便讲讲mutex跟semaphore的区别?
: (google了一下,不是太明白)

D*******a
发帖数: 3688
9
mutex cannot be unlocked by another thread other than the owner thread

the
occupied.
and

【在 b***y 的大作中提到】
: Maybe my answer upstairs misled you.
: mutex is just a binary semaphore. The only difference is that semaphore
: controls several resources whereas mutex controls only one.
: As of waiting or returning after detecting a locked mutex, it depends on the
: user of the mutex, not the mutex or semaphore itself.
: It's like you go to a public restroom and find all the toilets are occupied.
: It's up to you to just wait there or go tell your dad. It doesn't matter
: how many toilets there, one or many, they are al

c*****z
发帖数: 182
10
hi, this remind me an interview question
what is the difference between mutex and binary semaphere
is that a dummy question or i missed something?

the
occupied.
and

【在 b***y 的大作中提到】
: Maybe my answer upstairs misled you.
: mutex is just a binary semaphore. The only difference is that semaphore
: controls several resources whereas mutex controls only one.
: As of waiting or returning after detecting a locked mutex, it depends on the
: user of the mutex, not the mutex or semaphore itself.
: It's like you go to a public restroom and find all the toilets are occupied.
: It's up to you to just wait there or go tell your dad. It doesn't matter
: how many toilets there, one or many, they are al

相关主题
how to answer this question, thanksMonitor和semaphore, mutex是什么关系?
mutex和semaphore的差别到底是什么?问个semaphore 和 mutex的问题
谁能推荐一个read-writer lock的C++实现? (转载)编程题又一道
进入Programming版参与讨论
m*****e
发帖数: 4193
11

There isn't really a conceptual difference. But there may be different
limitations in specific implementations.

【在 c*****z 的大作中提到】
: hi, this remind me an interview question
: what is the difference between mutex and binary semaphere
: is that a dummy question or i missed something?
:
: the
: occupied.
: and

D*******a
发帖数: 3688
12
you can use binary semaphore as a mutex but not the other way around

【在 c*****z 的大作中提到】
: hi, this remind me an interview question
: what is the difference between mutex and binary semaphere
: is that a dummy question or i missed something?
:
: the
: occupied.
: and

c*****z
发帖数: 182
13
why cannot other way around, you mean mutex is alway busying waiting or
something?

【在 D*******a 的大作中提到】
: you can use binary semaphore as a mutex but not the other way around
g*****g
发帖数: 34805
14
mutex is a binary semaphore, or a special case of semaphore

【在 c*****z 的大作中提到】
: why cannot other way around, you mean mutex is alway busying waiting or
: something?

D*******a
发帖数: 3688
15
mutex can only be unlocked by the thread which locks it
semaphore can be released by any thread
therefore you cannot use mutex as a binary semaphore under some cases

【在 c*****z 的大作中提到】
: why cannot other way around, you mean mutex is alway busying waiting or
: something?

m*****e
发帖数: 4193
16
wrong

【在 D*******a 的大作中提到】
: mutex can only be unlocked by the thread which locks it
: semaphore can be released by any thread
: therefore you cannot use mutex as a binary semaphore under some cases

D*******a
发帖数: 3688
17
讲讲?

【在 m*****e 的大作中提到】
: wrong
r******m
发帖数: 6
18
稍稍学习了一下,这mutex的实现还要弄成个class
在这里是不是overkill啦?
弄个bool的变量,
简单点像这样行不?
template
class CirQ {
int flag;
int front, back;
int size;
T* q;
public:
CirQ(int len) : flag(0), front(0), back(0), size(len) {
q = new T[len];
}
int insert(T&);
...
}
template
int CirQ::insert(T& element) {
while (flag);
if (!full()) {
flag = 1;
q[back++] = element; back%=size;
flag = 0;
return 0;
}
else return -1;
}
x****u
发帖数: 44466
19
一般情况下遇到锁阻塞是正常思路,OS设计者的苦心也在这。如果类没有逻辑错误,也
不会因为阻塞一小会而死锁。
策略应该分读者优先和写者优先两类考虑吧,一个mutex怕是不够?去年搞过一个对性
能极度敏感的map,现在记不清楚了。

【在 r******m 的大作中提到】
: “2)写一个circular queue,要求thread safe”
: safe倒是简单,弄个mutex就行,但是策略应该如何?
: 比如,一个线程要插入,检测到锁了,是报错返回呢,还是等?
: 有没有大虾讲解讲解,谢谢了。

x****u
发帖数: 44466
20
绝对有必要,这种东西只要不影响最终代码品质,写的越详细越好。你看stl能被优化
成几个简单变量的东西,实际上展开后的模板和宏长的惊人,最后统统被优化。

【在 r******m 的大作中提到】
: 稍稍学习了一下,这mutex的实现还要弄成个class
: 在这里是不是overkill啦?
: 弄个bool的变量,
: 简单点像这样行不?
: template
: class CirQ {
: int flag;
: int front, back;
: int size;
: T* q;

r******m
发帖数: 6
21
面试的时候要写这么完善么?
在别的地方也看见这题了,要求不能用现成的library
1 (共1页)
进入Programming版参与讨论
相关主题
连续release mutex/semphore 2次有什么问题吗?about critical section
Multi-thread可以mutex锁资源,Multi-process怎么锁资源?how to answer this question, thanks
C++ InitializeCriticalSection问题mutex和semaphore的差别到底是什么?
面试问题一问 (转载)谁能推荐一个read-writer lock的C++实现? (转载)
pthread mutex能不能用与thread和process之间Monitor和semaphore, mutex是什么关系?
thread, semaphore, 问题。问个semaphore 和 mutex的问题
thread c++ 问题编程题又一道
Restaurant Reservation System...question about the read/write locker
相关话题的讨论汇总
话题: mutex话题: semaphore话题: thread话题: binary话题: int