我想知道为什么我们可以在中断上下文中使用信号量up(),而在中断上下文中不能使用相同的互斥变量,即mutex_unlock()。下面是内核中的代码片段
/**
* mutex_unlock - release the mutex
* @lock: the mutex to be released
*
* Unlock a mutex that has been locked by this task previously.
*
* This function must not be used in interrupt context. Unlocking
* of a not locked m
什么时候我应该在多线程编程中使用“lock”?只是锁定每个线程要修改的区域,或者锁定每个线程可以访问的区域,即使它不会被修改?
struct share_data {
/* share data */
thread_id;
}
thread 1 will exceute main() function:
Initial share data. /* need lock */
join all thread(share_data.thread_id, &status) /* access share data thread_id, lock or no
void transfer(bank_account &from, bank_account &to, int amount)
{
// lock both mutexes without deadlock
std::lock(from.m, to.m);
// make sure both already-locked mutexes are unlocked at the end of scope
std::lock_guard<std::mutex> lock1(from.m, std::adopt_lock);
st
作为我上一篇文章的后续文章,我将分享我对实现中的一个bug的想法,以验证它确实是一个bug。
这里有一个SynchronizedCollection<E>,它可以通过调用来自JDK的Collections.synchronizedCollection(...)获得:
public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
return new SynchronizedCollection<>(c);
}
static class Sy