* Q: How i can use Mutex and thread ..
@ 1998-12-23 14:44 Laurent Colombet
1999-01-04 16:27 ` Xavier Leroy
0 siblings, 1 reply; 2+ messages in thread
From: Laurent Colombet @ 1998-12-23 14:44 UTC (permalink / raw)
To: caml, caml-list
Hi,
I have the following error :
Objective Caml version 2.00
# let forks = Array.create 5 (Mutex.create());;
The external function `caml_mutex_new' is not available
and i think that it's the same thing with ocmal 2.01 :
Objective Caml version 2.01
# let forks = Array.create 5 (Mutex.create());;
Reference to undefined global `Mutex'
#
could somebody help me ?
thanks in advance
Laurent
P.S. Examples of Ocaml programs using thread are welcome
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Q: How i can use Mutex and thread ..
1998-12-23 14:44 Q: How i can use Mutex and thread Laurent Colombet
@ 1999-01-04 16:27 ` Xavier Leroy
0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 1999-01-04 16:27 UTC (permalink / raw)
To: Laurent Colombet, caml, caml-list
> I have the following error :
> Objective Caml version 2.01
> # let forks = Array.create 5 (Mutex.create());;
> Reference to undefined global `Mutex'
If you wish to use threads under the toplevel environment, you need to
build a custom toplevel first:
ocamlmktop -custom -thread -o threadedocaml threads.cma -cclib -lthreads
(This is for bytecode threads; if you're using POSIX threads or
Windows threads, see the manual, chapter "The threads library", for
the correct libraries to use.)
Then, just run "threadedocaml" and voilà, you've got the thread
functions.
> P.S. Examples of Ocaml programs using thread are welcome
Enclosed below are two good examples: one shows a "producer" thread
and a "consumer" thread connected via a bounded circular buffer
(illustrates mutexes and conditions); the other is the well-known
channel-based sieve of Eratosthenes (illustrates events).
Hope this helps,
- Xavier Leroy
(******************** Classic producer-consumer **********************)
type 'a prodcons =
{ buffer: 'a array;
lock: Mutex.t;
mutable readpos: int;
mutable writepos: int;
notempty: Condition.t;
notfull: Condition.t }
let create size init =
{ buffer = Array.create size init;
lock = Mutex.create();
readpos = 0;
writepos = 0;
notempty = Condition.create();
notfull = Condition.create() }
let put p data =
Mutex.lock p.lock;
while (p.writepos + 1) mod Array.length p.buffer = p.readpos do
Condition.wait p.notfull p.lock
done;
p.buffer.(p.writepos) <- data;
p.writepos <- (p.writepos + 1) mod Array.length p.buffer;
Condition.signal p.notempty;
Mutex.unlock p.lock
let get p =
Mutex.lock p.lock;
while p.writepos = p.readpos do
Condition.wait p.notempty p.lock
done;
let data = p.buffer.(p.readpos) in
p.readpos <- (p.readpos + 1) mod Array.length p.buffer;
Condition.signal p.notfull;
Mutex.unlock p.lock;
data
(* Test *)
let buff = create 20 0
let rec produce n =
print_int n; print_string "-->"; print_newline();
put buff n;
produce (n+1)
let rec consume () =
let n = get buff in
print_string "-->"; print_int n; print_newline();
consume ()
let _ =
Thread.create produce 0;
consume()
(*********************** Eratosthene's sieve ***********************)
open Printf
open Thread
let rec integers n ch =
Event.sync (Event.send ch n);
integers (n+1) ch
let rec sieve n chin chout =
let m = Event.sync (Event.receive chin) in
if m mod n = 0
then sieve n chin chout
else (Event.sync (Event.send chout m); sieve n chin chout)
let rec print_primes ch max =
let n = Event.sync (Event.receive ch) in
if n > max
then ()
else begin
printf "%d\n" n; flush stdout;
let ch_after_n = Event.new_channel () in
Thread.create (sieve n ch) ch_after_n;
print_primes ch_after_n max
end
let go max =
let ch = Event.new_channel () in
Thread.create (integers 2) ch;
print_primes ch max;;
let _ = go 1000
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1999-01-04 19:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-12-23 14:44 Q: How i can use Mutex and thread Laurent Colombet
1999-01-04 16:27 ` Xavier Leroy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox