質問

I have some code which I need to lock using a semaphore or mutex. The code is something like this:

callA();
callB();
callC();
.
.
.
callZ();

I would like to know the efficient way to lock it. The options I am thinking are

  • lock before callA() and unlock after callZ(). My concern is the lock remains set for a pretty long period.

  • lock and unlock after each function call. I am worried about the 'too much overhead' of grabbing and releasing the lock.

Appreciare your help !!!

役に立ちましたか?

解決

It all depends on your use case. How much lock/unlock/lock/unlock performance penalty can you tolerate? Weighed against this, how long are you willing to make another task block while waiting for the lock? Are some of the threads latency-critical or interactive and other threads bulk or low-priority? Are there other tasks that will take the same lock(s) through other code paths? If so, what do those look like? If the critical sections in callA, callB, etc.. are really separate then do you want to use 26 different locks? Or do they manipulate the same data, forcing you to use a single lock?

By the way, if you are using Linux, definitely use (pthreads) mutexes, not semaphores. The fast path for mutexes is completely usersparce. Locking and unlocking them when there is no contention is quite cheap. There is no fast path for semaphores.

Without knowing anything else, I would advise fine grained locking, especially if your individual functions are already organized to not make assumptions that would only be true if the lock is held across them all. But as I said, it really depends what you're doing and why you're doing it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top