We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
// 以下代码是不是可以不用加写锁,代码段一和代码段二对setNextReady的更新不存在并发问题,必须代码段一中先执行buffer.setNextReady(true);,才会走到代码段二中,唯一可能造成数据不一致的是nextReady=false时,threadRunning=true,但是buffer.getThreadRunning().compareAndSet(false, true)已经保证了,当线程未执行完代码段一时,是不可能有别的线程进入的
// 代码段一 if (updateOk) { buffer.wLock().lock(); buffer.setNextReady(true); buffer.getThreadRunning().set(false); buffer.wLock().unlock(); } else { buffer.getThreadRunning().set(false); }
// 代码段二 if (buffer.isNextReady()) { buffer.switchPos(); buffer.setNextReady(false); }
The text was updated successfully, but these errors were encountered:
代码2中的应该是为了防止多个线程切换,比如线程1和线程2同时切换了导致又回到了原来的segment,所以加了写锁
Sorry, something went wrong.
No branches or pull requests
// 以下代码是不是可以不用加写锁,代码段一和代码段二对setNextReady的更新不存在并发问题,必须代码段一中先执行buffer.setNextReady(true);,才会走到代码段二中,唯一可能造成数据不一致的是nextReady=false时,threadRunning=true,但是buffer.getThreadRunning().compareAndSet(false, true)已经保证了,当线程未执行完代码段一时,是不可能有别的线程进入的
// 代码段一
if (updateOk) {
buffer.wLock().lock();
buffer.setNextReady(true);
buffer.getThreadRunning().set(false);
buffer.wLock().unlock();
} else {
buffer.getThreadRunning().set(false);
}
// 代码段二
if (buffer.isNextReady()) {
buffer.switchPos();
buffer.setNextReady(false);
}
The text was updated successfully, but these errors were encountered: