[Mono-list] full duplex sockets and threads

Jonathan Pryor jonpryor at vt.edu
Mon May 8 06:42:36 EDT 2006


On Sun, 2006-05-07 at 13:48 -0400, P. Oscar Boykin wrote:
> "All public static members of this type are safe for multithreaded
> operations. No instance members are guaranteed to be thread safe."
> 
> Which I believe is boilerplate, but would imply that it might not be
> safe to read in one thread and write in another.

The boilerplate message actually means "don't attempt to use an instance
of Socket from multiple threads simultaneously without using a
synchronization mechanism."  In short, this is bad:

	// Thread 1
	socket.Send (...);

	// Thread 2
	socket.Receive (...);

Instead, you'd need to do this:

	// Thread 1
	lock (socket) {
		socket.Send (...);
	}

	// Thread 2
	lock (socket) {
		socket.Receive (...);
	}

I have no idea if this will actually work with Sockets (you'd have to
test), but that's what the boilerplate message actually refers to --
whether or not you need to provide your own synchronization.

 - Jon




More information about the Mono-list mailing list