caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Threads & the order that module initialization code runs
@ 2003-07-01 13:34 Richard Jones
  2003-07-01 15:24 ` Michel Christophe
  2003-07-01 23:13 ` Yamagata Yoriyuki
  0 siblings, 2 replies; 3+ messages in thread
From: Richard Jones @ 2003-07-01 13:34 UTC (permalink / raw)
  To: caml-list


A quick question about threads:

I have a few modules simulating a network and some machines connected
to the network.

In, for example, network.ml, I have a bunch of initialization code at
the top level:

--- network.ml ---
let new_queue i =
  (Queue.create (),
   Mutex.create (),
   Condition.create ()) in
let out_queue = Array.init nr_machines new_queue

(* There is one queue of incoming messages to the network thread. *)
let in_queue = Queue.create ()

(* Mutex/condition protecting the in_queue. *)
let in_mutex = Mutex.create ()
let in_cond = Condition.create ()
------

And also I have a Main module which starts up the threads:

--- main.ml ---
(* Create a thread to simulate the network. *)
let network_thread = Thread.create Network.run ();;

(* Start a thread for each of the simulated machines. *)
let machine_thread = Array.init nr_machines (Thread.create Machine.run);;
------

It's obviously going to be a problem if the initialization code in
Network hasn't run by the time the machine threads have been started
up in Main (or if the network thread itself gets started up first).

Now it all seems to work, but am I being lucky or do module
initializers run in some sort of well-defined order I can depend on?

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
'There is a joke about American engineers and French engineers. The
American team brings a prototype to the French team. The French team's
response is: "Well, it works fine in practice; but how will it hold up
in theory?"'

-------------------
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] 3+ messages in thread

* Re: [Caml-list] Threads & the order that module initialization code runs
  2003-07-01 13:34 [Caml-list] Threads & the order that module initialization code runs Richard Jones
@ 2003-07-01 15:24 ` Michel Christophe
  2003-07-01 23:13 ` Yamagata Yoriyuki
  1 sibling, 0 replies; 3+ messages in thread
From: Michel Christophe @ 2003-07-01 15:24 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

Thanks for answers



Le mar 01/07/2003 à 15:34, Richard Jones a écrit :
> A quick question about threads:
> 
> I have a few modules simulating a network and some machines connected
> to the network.
> 
> In, for example, network.ml, I have a bunch of initialization code at
> the top level:
> 
> --- network.ml ---
> let new_queue i =
>   (Queue.create (),
>    Mutex.create (),
>    Condition.create ()) in
> let out_queue = Array.init nr_machines new_queue
> 
> (* There is one queue of incoming messages to the network thread. *)
> let in_queue = Queue.create ()
> 
> (* Mutex/condition protecting the in_queue. *)
> let in_mutex = Mutex.create ()
> let in_cond = Condition.create ()
> ------
> 
> And also I have a Main module which starts up the threads:
> 
> --- main.ml ---
> (* Create a thread to simulate the network. *)
> let network_thread = Thread.create Network.run ();;
> 
> (* Start a thread for each of the simulated machines. *)
> let machine_thread = Array.init nr_machines (Thread.create Machine.run);;
> ------
> 
> It's obviously going to be a problem if the initialization code in
> Network hasn't run by the time the machine threads have been started
> up in Main (or if the network thread itself gets started up first).
> 
> Now it all seems to work, but am I being lucky or do module
> initializers run in some sort of well-defined order I can depend on?
> 
> Rich.

-------------------
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] 3+ messages in thread

* Re: [Caml-list] Threads & the order that module initialization code runs
  2003-07-01 13:34 [Caml-list] Threads & the order that module initialization code runs Richard Jones
  2003-07-01 15:24 ` Michel Christophe
@ 2003-07-01 23:13 ` Yamagata Yoriyuki
  1 sibling, 0 replies; 3+ messages in thread
From: Yamagata Yoriyuki @ 2003-07-01 23:13 UTC (permalink / raw)
  To: rich; +Cc: caml-list

From: Richard Jones <rich@annexia.org>
Subject: [Caml-list] Threads & the order that module initialization code runs
Date: Tue, 1 Jul 2003 14:34:15 +0100

> It's obviously going to be a problem if the initialization code in
> Network hasn't run by the time the machine threads have been started
> up in Main (or if the network thread itself gets started up first).
> 
> Now it all seems to work, but am I being lucky or do module
> initializers run in some sort of well-defined order I can depend on?

The modules are initialized in order of their linking.  You will be
safe if you do
     ocamlc -o program network.cmo ... main.cmo
or 
     ocamlopt -o program network.cmx ... main.cmx

Since you refer Network in Main, the compiler emits an error if
network.cmo is not linked before main.cmo, anyway.

--
Yamagata Yoriyuki

-------------------
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] 3+ messages in thread

end of thread, other threads:[~2003-07-01 23:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-01 13:34 [Caml-list] Threads & the order that module initialization code runs Richard Jones
2003-07-01 15:24 ` Michel Christophe
2003-07-01 23:13 ` Yamagata Yoriyuki

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).