Named read/write lock synchronization primitive example
#include <iostream>
#include <string>
int main(int argc, char** argv)
{
std::string help = "Please enter '+' or '*' to read/write lock and '-' or '/' to read/write unlock the named lock (several processes support). Enter '0' to exit...";
std::cout << help << std::endl;
std::string line;
while (getline(std::cin, line))
{
if (line == "+")
{
std::cout << "Successfully locked for read!" << std::endl;
else
std::cout << "Failed to lock for read!" << std::endl;
}
else if (line == "*")
{
std::cout << "Successfully locked for write!" << std::endl;
else
std::cout << "Failed to lock for write!" << std::endl;
}
else if (line == "-")
{
try
{
std::cout << "Successfully unlocked reader!" << std::endl;
}
{
std::cout << "Failed to unlock reader!" << std::endl;
}
}
else if (line == "/")
{
try
{
std::cout << "Successfully unlocked writer!" << std::endl;
}
{
std::cout << "Failed to unlock writer!" << std::endl;
}
}
else if (line == "0")
break;
else
std::cout << help << std::endl;
}
return 0;
}
Named read/write lock synchronization primitive.
void UnlockRead()
Release read lock.
void UnlockWrite()
Release write lock.
bool TryLockWrite()
Try to acquire write lock without block.
bool TryLockRead()
Try to acquire read lock without block.
Named read/write lock synchronization primitive definition.