Skip to content
New issue

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

预加载第二个segment的时候,是不是可以不用加写锁 #215

Open
YIXIANG0234 opened this issue Jan 5, 2024 · 1 comment
Open

Comments

@YIXIANG0234
Copy link

// 以下代码是不是可以不用加写锁,代码段一和代码段二对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);
}

@wei-coder-feng
Copy link

代码2中的应该是为了防止多个线程切换,比如线程1和线程2同时切换了导致又回到了原来的segment,所以加了写锁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants