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
|
|
|
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 |