How do I use an arbitrary string as a lock in C++?

前端 未结 6 1892
半阙折子戏
半阙折子戏 2021-02-04 15:15

Let\'s say I have a multithreaded C++ program that handles requests in the form of a function call to handleRequest(string key). Each call to handleRequest

6条回答
  •  梦毁少年i
    2021-02-04 15:41

    It will depend on the platform, but the two techniques that I'd try would be:

    • Use named mutex/synchronization objects, where object name = Key
    • Use filesystem-based locking, where you try to create a non-shareable temporary file with the key name. If it exists already (=already locked) this will fail and you'll have to poll to retry

    Both techniques will depend on the detail of your OS. Experiment and see which works. .

提交回复
热议问题