我很难尝试解决并发问题:
假设在系统中,用户将注册回调函数 系统和事件发生时将调用回调函数 触发但如果事件已经被触发,则用户将会触发 同步调用回调函数。
例如:
suppose user i registers cb i at time Ti, and the event happens at time Te and assume that T1 < T2 < T3 <...< Tm < Te < Tm+1 < ... < Tn at time Te, cb1..cbm will be called cbm+1 .. cbn will be called synchronously implement two APIs: void register_cb (cb), and void event_trigger()
目前,我的解决方案是:
is_triggered = false;
mutex_a = 1
void event_trigger(){
aquire(mutex_a);
is_triggered = true;
while(!q.empty()){
cb = q.top();
q.pop();
cb();
}
release(mutex_a);
}
void register_cb(cb){
aquire(mutex_a);
if(is_triggered)
release(mutex_a);
cb();
else{
q.push(cb);
release(mutex_a);
}
}
但这并没有解决问题:我认为我错放了互斥锁。我哪里错了?