Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locking in one thread, releasing in another

I have a game server than can take requests from a user. A user can request to place pieces. The place method then spawns some async httpwebrequests (with timeouts) to find out if the placement was correct. I want a lock that will be locked when the server receives the placement request, and will be unlocked by the web callback. I would use a ReaderWriterLock, but that only works if I stay in the same thread, and the web request callbacks occur on different threads. Is there another lock I should use?

like image 252
Nikhil Avatar asked Feb 07 '12 22:02

Nikhil


2 Answers

You can use a semaphore. The locking thread acquires a permit. The async thread releases a permit. Semaphores are nifty because they aren't bound to individual threads.

like image 131
Spike Gronim Avatar answered Nov 16 '22 11:11

Spike Gronim


You could use a Semaphore. Quote from the manual;

The Semaphore class does not enforce thread identity on calls to WaitOne or Release.

In other words, you should not have a problem acquiring/releasing from two different threads.

like image 24
Joachim Isaksson Avatar answered Nov 16 '22 11:11

Joachim Isaksson