Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0000015OCaml~DO NOT USE (was: OCaml general)public1999-12-21 16:001999-12-21 16:07
Assigned To 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0000015: bytecode threads in Ocaml-2.04 / 2.02 / 1.05
DescriptionFull_Name: Pawel Wojciechowski
Version: 2.04
Submission from: (
Submitted by: xleroy


Just to ask if all is fine with bytecode threads in OCaml version 2.04?

In the program below I create two threads, one is doing some computation
(counter), second thread is executing a wait function. In the main program
I wait until the second thread terminates and then I print a current state
of the computation being done by the first thread. I checked three kinds
of wait function: a blocking input from console, a non-blocking low-level
input from ThreadUnix module and a simple recursive loop.

For each function "wait":

In Ocaml 1.05 the counter thread is never blocked

In Ocaml 2.02 the counter thread is never blocked but.. if we change
         the program so as we don't create a separate thread for a function
         wait then if wait executes read_line() or read() the program prints
         0 as a result (thus it seems like I/O operation blocks the thread

In Ocaml 2.04 The program behaves abnormally and suspends execution in
         every case.

I installed Ocaml* on i686 Linux and I'm compiling the program using:

ocamlc -thread -custom unix.cma threads.cma -cclib -lunix -cclib -lthreads
$1 -o $2

best regards, -
Pawel Wojciechowski

open Unix
open ThreadUnix

let res = ref 0
let s = Mutex.create()

let rec counter i =
  Mutex.lock s; res := i; Mutex.unlock s;
  counter (i + 1)

  let wait () = let _ = read_line () in ()
  let wait () =
    let line = String.create 60 in
    let len = read stdin line 0 60 in
    print_string (String.sub line 0 len)
  let wait () =
    let rec loop i =
      if i < 3000000 then loop (i + 1) else i
    in loop 0

(* VERSION 1: two threads *)

let main () =
  let _ = Thread.create counter 0 in
  let t = Thread.create wait () in
  Thread.join t;
  Mutex.lock s; print_int !res; Mutex.unlock s

let _ = main ()

(* VERSION 2: one thread *)

let main () =
  let _ = Thread.create counter 0 in
  Mutex.lock s; print_int !res; Mutex.unlock s

let _ = main ()

Pawel T. Wojciechowski, Computer Lab., University of Cambridge, office +44(1223 )334 602, fax 335 908

TagsNo tags attached.
Attached Files

- Relationships

-  Notes
administrator (administrator)
1999-12-21 16:05

> Just to ask if all is fine with bytecode threads in OCaml version 2.04?

I thought so :-) But you've found an interesting bug. Basically, the
preemption of long-running threads (such as "counter" in your example) stopped
working when I revised the handling of signals in OCaml 2.02. The problem
didn't show up on my tests because all their threads perform enough
communications to still work under cooperative (non-preemptive) scheduling.

This will be fixed in release 3.00. If you're interested, I'll send you a
against 2.99.

Thanks for your bug report,

- Xavier Leroy
administrator (administrator)
1999-12-21 16:07

Fixed in 3.00

- Issue History
Date Modified Username Field Change
2005-11-18 10:13 administrator New Issue
2017-02-23 16:36 doligez Category OCaml general => -OCaml general
2017-03-03 17:55 doligez Category -OCaml general => -(deprecated) general
2017-03-03 18:01 doligez Category -(deprecated) general => ~deprecated (was: OCaml general)
2017-03-06 17:04 doligez Category ~deprecated (was: OCaml general) => ~DO NOT USE (was: OCaml general)

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker