-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: cqy123456 <[email protected]>
- Loading branch information
Showing
4 changed files
with
106 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
#include <mutex> | ||
#include <condition_variable> | ||
|
||
namespace diskann { | ||
class Semaphore { | ||
public: | ||
Semaphore(long count = 0) : count(count) {} | ||
void Signal() | ||
{ | ||
std::unique_lock<std::mutex> unique(mt); | ||
++count; | ||
if (count <= 0) { | ||
cond.notify_one(); | ||
} | ||
} | ||
void Wait() | ||
{ | ||
std::unique_lock<std::mutex> unique(mt); | ||
--count; | ||
if (count < 0) { | ||
cond.wait(unique); | ||
} | ||
} | ||
bool IsWaitting() { | ||
std::unique_lock<std::mutex> unique(mt); | ||
return count < 0; | ||
} | ||
|
||
private: | ||
std::mutex mt; | ||
std::condition_variable cond; | ||
long count; | ||
}; | ||
} // namespace diskann |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters