Nikhil
Nikhil

Reputation: 1151

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?

Upvotes: 8

Views: 2297

Answers (2)

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

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.

Upvotes: 4

Spike Gronim
Spike Gronim

Reputation: 6182

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.

Upvotes: 9

Related Questions