Mutex - does it work?

Has anybody successfully used Mutex in C#?

I am asking because I have a piece of code that uses locking/unlocking with a Mutex object but occasionally crashes and the crash clearly indicates that there is no mutual exclusion in the code.

Thanks.

threading etc in mono 1.2.5 (as used in unity 2 / unity iphone 1.x) is quite a bit broken.

also mutex won’t help you in case you think about letting an external thread call into unity stuff as unity does not respect nor use mutexes.

If that is the case, how can one use any kind of thread as part of a system?

I have a situation where I use a UDP class to read packets from an external server. The UDP reader is a separate thread because it uses blocking reads.

The problem is, you cannot do Unity things in a thread (such as creating a game object) so I gather all messages in a list and let the regular Update() call implement the actions.

I need a synchronization mechanism to ensure that the background thread does not write the list while the Update code is reading it.

Is there a different way to achieve this without using some kind of lock?

as you commonly would do it in C# I guess:

lock(some System.Collections.* adt)
{
  ... perform ops ...
}

mutex should work too if you prefer that, as long you have code that works smart there … and keep in mind, the price of mutex is that your whole game will freeze until the lock / mutex has been passed

Yes, we do.

We use Mutex in our game, and it seems all ok until I upgraded to 3.0 today. My web player build totally fails on “new Mutex()”. But the standalone version works well.

No solution so far.

Well, that answers my question from a few minutes ago. I just ran into this problem myself. “new Mutex()” fails in the web player.

Locks work fine in background threads in 3.0. Never tried Mutex