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