Semaphore synchronization primitive example
#include <iostream>
#include <string>
int main(int argc, char** argv)
{
std::string help = "Please enter '+' to lock and '-' to unlock the semaphore. Enter '0' to exit...";
std::cout << help << std::endl;
int resources = 4;
std::string line;
while (getline(std::cin, line))
{
if (line == "+")
{
std::cout << "Semaphore successfully locked!" << std::endl;
else
std::cout << "Failed to lock semaphore! Semaphore resources exceeded..." << std::endl;
}
else if (line == "-")
{
try
{
std::cout << "Semaphore successfully unlocked!" << std::endl;
}
{
std::cout << "Failed to unlock semaphore! Semaphore is fully unlocked..." << std::endl;
}
}
else if (line == "0")
break;
else
std::cout << help << std::endl;
}
return 0;
}
Semaphore synchronization primitive.
bool TryLock()
Try to acquire semaphore without block.
void Unlock()
Release semaphore.
Semaphore synchronization primitive definition.