caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Question on Mutexes
@ 2003-03-18 13:37 Alessandro Baretta
  2003-03-18 18:43 ` David Brown
  2003-03-20 15:13 ` Xavier Leroy
  0 siblings, 2 replies; 4+ messages in thread
From: Alessandro Baretta @ 2003-03-18 13:37 UTC (permalink / raw)
  To: Ocaml

If thread t1 locks mutex m, can thread t2 unlock it?

I ask because I get no sign of exceptions being thrown when 
I execute the following code in the toplevel:

let m = Mutex.create ()
let () = Mutex.lock m
let f m' = Mutex.unlock m'
let t = Thread.create f m
let () = Mutex.lock m

Also, the final call to Mutex.lock does not block the 
execution of the program by any significant time, which 
means that mutex m is unlocked by thread t before being 
locked once again by the original thread.

Is this a bug or a feature? Are threads supposed to be able 
to unlock mutexes held by _other_ threads?

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Question on Mutexes
  2003-03-18 13:37 [Caml-list] Question on Mutexes Alessandro Baretta
@ 2003-03-18 18:43 ` David Brown
  2003-03-20 15:13 ` Xavier Leroy
  1 sibling, 0 replies; 4+ messages in thread
From: David Brown @ 2003-03-18 18:43 UTC (permalink / raw)
  To: Alessandro Baretta; +Cc: Ocaml

On Tue, Mar 18, 2003 at 02:37:09PM +0100, Alessandro Baretta wrote:

> If thread t1 locks mutex m, can thread t2 unlock it?
> 
> I ask because I get no sign of exceptions being thrown when 
> I execute the following code in the toplevel:

Most mutex implementations I've seen assume that the unlock is done by
the same thread.  Posix threads allows you to enable error checking, to
check for this case.  However, it does cause a performance hit.

Perhaps the mutex stuff could be wrapped in something that would check
for the error?

Dave

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Question on Mutexes
  2003-03-18 13:37 [Caml-list] Question on Mutexes Alessandro Baretta
  2003-03-18 18:43 ` David Brown
@ 2003-03-20 15:13 ` Xavier Leroy
  2003-03-24 11:19   ` Alessandro Baretta
  1 sibling, 1 reply; 4+ messages in thread
From: Xavier Leroy @ 2003-03-20 15:13 UTC (permalink / raw)
  To: Alessandro Baretta; +Cc: Ocaml

> If thread t1 locks mutex m, can thread t2 unlock it?
> I ask because I get no sign of exceptions being thrown when 
> I execute the following code in the toplevel:
> [...]
> Is this a bug or a feature? Are threads supposed to be able 
> to unlock mutexes held by _other_ threads?

No, but this isn't necessarily checked.

The bytecode-level implementation of the thread library doesn't check,
and will actually work correctly if thread A locks the mutex and
thread B unlocks it.  

The system-level implementation of the thread library is a thin
wrapper around POSIX or Win32 mutexes.  The POSIX spec says that the
behavior of unlocking by another thread than the one that locked is
undefined: some implementations can signal an error, others can "do
the right thing", others can crash.

- Xavier Leroy

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Question on Mutexes
  2003-03-20 15:13 ` Xavier Leroy
@ 2003-03-24 11:19   ` Alessandro Baretta
  0 siblings, 0 replies; 4+ messages in thread
From: Alessandro Baretta @ 2003-03-24 11:19 UTC (permalink / raw)
  To: Ocaml



Xavier Leroy wrote:

> The system-level implementation of the thread library is a thin
> wrapper around POSIX or Win32 mutexes.  The POSIX spec says that the
> behavior of unlocking by another thread than the one that locked is
> undefined: some implementations can signal an error, others can "do
> the right thing", others can crash.

Of course, Ocaml does the "right thing", doesn't it? ;)

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-03-24 11:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-18 13:37 [Caml-list] Question on Mutexes Alessandro Baretta
2003-03-18 18:43 ` David Brown
2003-03-20 15:13 ` Xavier Leroy
2003-03-24 11:19   ` Alessandro Baretta

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).