SHA256
before practice 3
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
#include <condition_variable>
|
||||
#include <stdio.h>
|
||||
#include <mutex>
|
||||
|
||||
class Barrier{
|
||||
std::mutex mtx;
|
||||
std::condition_variable cv;
|
||||
std::size_t count=0;
|
||||
std::size_t max_count=0;
|
||||
bool flushing = false;
|
||||
Barrier (size_t max_count){
|
||||
max_count=max_count;
|
||||
}
|
||||
void arrive_and_wait(){
|
||||
std::unique_lock lock(mtx);
|
||||
while(flushing)
|
||||
cv.wait(lock);
|
||||
if(++count<max_count)
|
||||
{
|
||||
while(! flushing)
|
||||
cv.wait(lock);
|
||||
}
|
||||
else{
|
||||
flushing = true;
|
||||
cv.notify_all();
|
||||
}
|
||||
if(--count==0){
|
||||
flushing = false;
|
||||
cv.notify_all();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user