Coffeehouse Post

Single Post Permalink

View Thread: Question on C# Threads
  • User profile image
    Sven Groot

    @magicalclick: And how do you wait for the int to become zero? Polling? Not to mention that addition/subtraction is not thread safe unless you're using interlocked operation. This method of doing it is not just dumb, but stupid.

    If you want to go that route you can use WaitHandles, or a Semaphore, or in .Net 4.0 the CountdownEvent (which is a thread-safe way of doing what you described).

    There's a simple rule with multi-threading: if you think you've found a simple way to do something, it's nearly always wrong (unless that simple way is using a class someone else wrote that encapsulates the hard stuff, like Task or BackgroundWorker).