Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Threads & the order that module initialization code runs
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: [Caml-list] Threads & the order that module initialization code runs

A quick question about threads:

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

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

--- ---
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:

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

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

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?


Richard Jones.
Merjis Ltd. - 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 Archives:
Bug reports: FAQ:
Beginner's list: