[Spread-users] Question about thread-safety

Theo E. Schlossnagle jesus at omniti.com
Wed Jun 18 10:07:33 EDT 2003

John Schultz wrote:

> Your method would still require that the user ensure that all threads 
> that might use the context, stop using the context before it is reclaimed.

No.  That safety can be completely handled within the library.

> This is common practice for libraries but with almost no additional work 
> (a lookup per call) the table method takes that synchronization problem 
> off of the user's hands.  The user can reclaim/close the handle at any 
> time in any thread and any threads that subsequently try to use that 
> handle will get an INVALID_HANDLE error and can assume that some other 
> thread already closed/reclaimed the handle.

In the context situation, you require no synchronization on the user's part. 
All synchronization facilities are _inside_ the context, which is opaque.  The 
library calls do what is necessary on the context.  For lack of a better example:

SP_join(mailbox mb, const char *group) {
   do stuff

Obviously this isn't the implementation you'd use, but it shows that mailbox 
has a global_lock element and that the library can see it.  The user cannot.

> Finally, we can guarantee in practice (use a big counter and exit if we 
> flip it) that our integer handles are "eternally unique" whereas malloc 
> _might_ return the same pointer twice.

Not if it has been freed.  If it hasbeen freed, then of course malloc could 
return the same value.  The point being that SP_kill and SP_disconnect (or any 
other error for that matter) could never free the context.  The user must call 
SP_context_destroy(mailbox) [or some such function].

This would allow it to work in both multiprocess and multithreaded apps.  The 
problem with the lookuptable is that you just reimplement the problem of 
duplicating the file descriptor table.  Now you duplicate the lookuptable and 
the user (with no visibility to that) has no recourse.

Theo Schlossnagle
Principal Consultant
OmniTI Computer Consulting, Inc. -- http://www.omniti.com/
Phone:  +1 410 872 4910 x201     Fax:  +1 410 872 4911
1024D/82844984/95FD 30F1 489E 4613 F22E  491A 7E88 364C 8284 4984
2047R/33131B65/71 F7 95 64 49 76 5D BA  3D 90 B9 9F BE 27 24 E7

More information about the Spread-users mailing list