3

Just attempting to use Interlocked.Increment(ref threads) and it just hangs my game.

public static int threads;

...

Interlocked.Increment(ref threads);

Using System.Threads also.

I've moved it into the main thread and out of it, can't seem to get it to work anywhere. Is this just Unity causing this?

Kara
  • 6,115
  • 16
  • 50
  • 57
Ken Rea
  • 67
  • 8
  • Please reference your game technology, are you using XNA or stuff like that? – oleksii Jan 02 '14 at 21:47
  • Just unity. using UnityEngine; It's a unity project. Unity is an engine designed for game development. C# – Ken Rea Jan 02 '14 at 21:48
  • 3
    Oh... I know Unity as an [IoC container](http://msdn.microsoft.com/en-us/library/ff647202.aspx). I guess this is a totally different thing. – oleksii Jan 02 '14 at 21:51
  • Yes Definitely a different thing all together. I'll sepcify that. . – Ken Rea Jan 02 '14 at 22:04
  • Trying searching for usages of the 'threads' variable, you might find some suspicious code – dcastro Jan 03 '14 at 07:24
  • Don't forget that threads are not a trivial thing in Unity3d. You have to be careful not to interfere with the rendering loop, updates etc. – Alex Jan 03 '14 at 14:02
  • And that statement replaced what? An increment of the `threads` variable, like `threads++;`? I can't imagine `Interlocked.Increment` causing a hang. Are you certain that's the source of your problem? – Jim Mischel Jan 03 '14 at 16:12
  • Unity seems to be weird with threads here. Anything to do with XDocument also causes issues where it just kills the threads. Dunno why, was trying to make loading faster. May not be able to now. Also yes it replaced threads++. when i commented it out, the hang stopped. I think it's just Unity not liking threading. – Ken Rea Jan 04 '14 at 23:23

1 Answers1

1

The .Net Interlocked.Increment doesn't actually lock anything (at least it definitely doesn't for an int); it only ensures the increment happens atomically.

Either it is not actually the change causing your problem or you've got another Interlocked imported into your namespaces.

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101