Named 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 named semaphore (several processes support). 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;
}
Named semaphore synchronization primitive.
 
void Unlock()
Release semaphore.
 
bool TryLock()
Try to acquire semaphore without block.
 
Named semaphore synchronization primitive definition.