AI War 2

AI War 2

AI War 2 v0.748 Released! "Macrophage Teeth"

27 Rate up
< >
El Spodo Komodo Jul 8 @ 5:49am 
Three cheers for transparency.
x-4000 (Chris Park)  [developer] Jul 5 @ 5:34pm 
There's been so much discussion and excellent bits shared with me, I'm making a document to contain it all:
Miffe Jul 5 @ 5:23pm 
This is a better benchmark:
The last one accidentally measured the thread creating time.
ThreadPool still wins though.
x-4000 (Chris Park)  [developer] Jul 5 @ 5:09pm 
I don't need threadsafety, and I don't need to be able to set events much. The background thread is basically not-existing (in reality or effectively) until a bool trips that says "now do stuff with some data that is prepped for you and that nothing else will modify." It then does whatever it does, making changes only in that data set or some other area that only it touches. It then finishes, setting another bool saying "I am done, the data is yours now," and then it waits to hear from the main thread again later.

This really does sound like the threadpool situation, since there are not even any locks required, but I wasn't sure if that would be performant enough. Thanks for the microbenchmark! That does help a ton in basically solving that question. :)

Thank you to all three for the answers!
Miffe Jul 5 @ 3:50pm 
Use the ThreadPool, it's way faster.
Microbenchmark here
PoshPorcupine Jul 5 @ 3:29pm 
I agree with Loiathal, but I suspect that the overhead of the CountdownEvent will be too expensive, due to it being thread safe. I make heavy use of AutoResetEvent's for this type of scenario. They aren't as elegant, but it might be performant enough:
Loiathal Jul 5 @ 2:46pm 
So, I'm a Java developer rather than a C# one, but you're trying to have a thread only start processing once data is available to it, correct?

I use the Java version of this quite a bit:

I'll create a thread, pass the CountdownEvent to it, and then call Wait() Then also pass the CountdownEvent to whatever is loading data in. Once the data is loaded, the loading thread/class/whatever calls AddCount(), and the waiting thread will automatically wake up and start processing.

I also use it a lot to verify that I don't have concurrency bugs.

I don't know if this is going to be faster than your current setup, but I've found the Java version to be CONSIDERABLY more reliable and efficient than Thread.sleep calls.