Unity and ThreadPools

I’m having some troubles here with using threads delegated to the .NET ThreadPool. Can anyone give me some suggestions?

Normally it works fine and I’m fairly certain I don’t have any deadlocking or race conditions. I’m just spinning off threads to do some math that is self-contained.

Half the time, though, I run the code in the Unity Editor and the entire thing seizes up at a random location in the code. There is no rhyme or reason to it. I can have code that works fine, put in some whitespace, recompile, and unity blows up.

Now, I know what you’re thinking: deadlock or race condition. Possibly. But the code is sound.

So the question is: has anyone run into this before with the Unity editor? Have you run into this with mixing languages? Any advice? Sometimes the threads just shit bricks, which, again, you’d think it’d be a code issue but its not.

I do remember having this sort of behavior before but it was a java program and yes, it was a race condition related problem. Usually when things like this happen like debugging or recompiling and seeing different behavior it is highly probable that it is a deadlock or something related to that.

Yeah, it looks like that it was a deadlock. I’m not entirely sure where but one thing I did which seems to have helped is that I

a) Lowered the number of threads in the ThreadPool
b) instead of using lock I translated the .NET 4.0 syntax of lock and used that instead:

	lock_taken = false
		# do something

I suspect that the lock syntax in Boo/Mono is not perfect. Apparently until .NET 4.0 there was a chance for an error partway through a lock to trigger a deadlock and the above code was how it was fixed in 4.0.

Or I could just be making that up, but, I haven’t had any problems since.