由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - How to avoid deadlock ?
相关主题
double-checked lockingDummy DB question: create user时,DB自己会lock/sync吧
[合集] can a single thread run into deadlock?面试问题一问 (转载)
谁能推荐一个read-writer lock的C++实现? (转载)how to debug multi-thread program?
请教 boost locksEBUSY 的定义
C++的一个mutex问题c++问题
scoped lock的问题pthread in cygwin
这样的deadlock如何debug?waiting for N condition variables in linux
C10Mcondional variable thread sync 问题 (转载)
相关话题的讨论汇总
话题: avoid话题: lock话题: locks话题: deadlock话题: thread
进入Programming版参与讨论
1 (共1页)
w*s
发帖数: 7227
1
avoid having locks (if possible),
avoid having more than one lock
always take the locks in the same order.
are these enough ?
thanks !
g*****g
发帖数: 34805
2
Depends on the support, some language like java allows you to
try lock, you can try lock all the locks needed. And release
locks if any of them is not successful.

【在 w*s 的大作中提到】
: avoid having locks (if possible),
: avoid having more than one lock
: always take the locks in the same order.
: are these enough ?
: thanks !

w*s
发帖数: 7227
3
How about c++ ?
i searched online, seems there're algorithms to detect deadlocks,
but not sure companies will implements these.
The only thing i remember is to acquire locks in orders for each thread. But
was not accepted in the interview.

【在 g*****g 的大作中提到】
: Depends on the support, some language like java allows you to
: try lock, you can try lock all the locks needed. And release
: locks if any of them is not successful.

M**u
发帖数: 10158
4
pthread_mutex_trylock

But

【在 w*s 的大作中提到】
: How about c++ ?
: i searched online, seems there're algorithms to detect deadlocks,
: but not sure companies will implements these.
: The only thing i remember is to acquire locks in orders for each thread. But
: was not accepted in the interview.

M**u
发帖数: 10158
5
in pthread, there is error-check mutex
Default mutex is fast mutex, which cannot check

But

【在 w*s 的大作中提到】
: How about c++ ?
: i searched online, seems there're algorithms to detect deadlocks,
: but not sure companies will implements these.
: The only thing i remember is to acquire locks in orders for each thread. But
: was not accepted in the interview.

l*****o
发帖数: 473
6
Using the same order should be enough.
B********e
发帖数: 1062
7
There is a silly but workable method:
never ever use lock/unlock by itself. use scoped_lock.

【在 w*s 的大作中提到】
: avoid having locks (if possible),
: avoid having more than one lock
: always take the locks in the same order.
: are these enough ?
: thanks !

w*s
发帖数: 7227
8
谢谢各位,老了,上面的很多没听到过。
l*****o
发帖数: 473
9
pthread_mutex_trylock won't help at all.
Considering the case like this:
Thread 1:
lock(A);
lock(B);
unlock(B);
unlock(A);
Thread 2:
lock(B);
lock(A);
unlock(A);
unlock(B);
Let's assume that Thread 1 get lockA at first.
Now when thread 2 get lockB, it is definitely allowed (even use pthread_try_
lock) to acquire lockB because no one is acquiring that.
Then if T1 are holding lockA and T2 are holding lockB, then there is no use
to use pthread_mutex_trylock at all because T1 won't release lockA and T2
won't release lockB.
When thread

【在 M**u 的大作中提到】
: pthread_mutex_trylock
:
: But

1 (共1页)
进入Programming版参与讨论
相关主题
condional variable thread sync 问题 (转载)C++的一个mutex问题
多线程的程序设计有什么好书推荐? (转载)scoped lock的问题
question about the read/write locker这样的deadlock如何debug?
is pthread_mutex_destroy() required to call?C10M
double-checked lockingDummy DB question: create user时,DB自己会lock/sync吧
[合集] can a single thread run into deadlock?面试问题一问 (转载)
谁能推荐一个read-writer lock的C++实现? (转载)how to debug multi-thread program?
请教 boost locksEBUSY 的定义
相关话题的讨论汇总
话题: avoid话题: lock话题: locks话题: deadlock话题: thread