Module Thread

Contents

Instructions: Use this module in your project

In the IDE (CLion, Visual Studio Code, Xcode, etc.) you use for your DkSDK project:

  1. Add the following to your project's dependencies/CMakeLists.txt:

    Copy
    DkSDKProject_DeclareAvailable(ocaml
        CONSTRAINT "= 4.14.0"
        FINDLIBS str unix runtime_events threads dynlink)
    DkSDKProject_MakeAvailable(ocaml)
  2. Add the Findlib::ocaml library to any desired targets in src/*/CMakeLists.txt:

    Copy
    target_link_libraries(YourPackage_YourLibraryName
         # ... existing libraries, if any ...
         Findlib::ocaml)
  3. Click your IDE's Build button

Not using DkSDK?

FIRST, do one or all of the following:

  1. Run:

    Copy
    opam install ocaml.4.14.0
  2. Edit your dune-project and add:

    Copy
    (package
      (name YourExistingPackage)
      (depends
      ; ... existing dependenices ...
      (ocaml (>= 4.14.0))))

    Then run:

    Copy
    dune build *.opam # if this fails, run: dune build
  3. Edit your <package>.opam file and add:

    Copy
    depends: [
      # ... existing dependencies ...
      "ocaml" {>= "4.14.0"}
    ]

    Then run:

    Copy
    opam install . --deps-only

FINALLY, add the unix threads library to any desired (library)and/or (executable) targets in your **/dune files:

Copy
(library
  (name YourLibrary)
  ; ... existing library options ...
  (libraries
    ; ... existing libraries ...
    unix threads))

(executable
  (name YourExecutable)
  ; ... existing executable options ...
  (libraries
    ; ... existing libraries ...
    unix threads))
type t

The type of thread handles.

Thread creation and termination

val create : ``('a -> 'b)`` -> 'a -> t

Thread.create funct arg creates a new thread of control, in which the function application funct arg is executed concurrently with the other threads of the program. The application of Thread.create returns the handle of the newly created thread. The new thread terminates when the application funct arg returns, either normally or by raising the Thread.Exit exception or by raising any other uncaught exception. In the last case, the uncaught exception is printed on standard error, but not propagated back to the parent thread. Similarly, the result of the application funct arg is discarded and not directly accessible to the parent thread.

valself : ``unit-> t

Return the handle for the thread currently executing.

valid :t -> int

Return the identifier of the given thread. A thread identifier is an integer that identifies uniquely the thread. It can be used to build data structures indexed by threads.

exception Exit

Exception that can be raised by user code to initiate termination of the current thread. Compared to calling the Thread.exit function, raising the Thread.Exit exception will trigger Fun.finally finalizers and catch-all exception handlers. It is the recommended way to terminate threads prematurely.

  • since 4.14.0
valexit : ``unit-> unit

Terminate prematurely the currently executing thread.

valkill :t -> unit

This function was supposed to terminate prematurely the thread whose handle is given. It is not currently implemented due to problems with cleanup handlers on many POSIX 1003.1c implementations. It always raises the Invalid_argument exception.

  • deprecated Not implemented, do not use

Suspending threads

valdelay : ``float-> unit

delay d suspends the execution of the calling thread for d seconds. The other program threads continue to run during this time.

valjoin :t -> unit

join th suspends the execution of the calling thread until the thread th has terminated.

valyield : ``unit-> unit

Re-schedule the calling thread without suspending it. This function can be used to give scheduling hints, telling the scheduler that now is a good time to switch to other threads.

Waiting for file descriptors or processes

The functions below are leftovers from an earlier, VM-based threading system. The Unix module provides equivalent functionality, in a more general and more standard-conformant manner. It is recommended to use Unix functions directly.

valwait_read :Unix.file_descr -> unit

This function does nothing in the current implementation of the threading library and can be removed from all user programs.

  • deprecated This function no longer does anything
valwait_write :Unix.file_descr -> unit

This function does nothing in the current implementation of the threading library and can be removed from all user programs.

  • deprecated This function no longer does anything
valwait_timed_read :Unix.file_descr ->``float-> bool
valwait_timed_write :Unix.file_descr ->``float-> bool

Suspend the execution of the calling thread until at least one character or EOF is available for reading (wait_timed_read) or one character can be written without blocking (wait_timed_write) on the given Unix file descriptor. Wait for at most the amount of time given as second argument (in seconds). Return true if the file descriptor is ready for input/output and false if the timeout expired. The same functionality can be achieved with Unix.select.

valselect :Unix.file_descrlist``-> Unix.file_descrlist``-> Unix.file_descrlist``->``float-> Unix.file_descrlist`` *Unix.file_descrlist`` *Unix.file_descr list

Same function as Unix.select. Suspend the execution of the calling thread until input/output becomes possible on the given Unix file descriptors. The arguments and results have the same meaning as for Unix.select.

valwait_pid : ``int->int *Unix.process_status

Same function as Unix.waitpid. wait_pid p suspends the execution of the calling thread until the process specified by the process identifier p terminates. Returns the pid of the child caught and its termination status, as per Unix.wait.

Management of signals

Signal handling follows the POSIX thread model: signals generated by a thread are delivered to that thread; signals generated externally are delivered to one of the threads that does not block it. Each thread possesses a set of blocked signals, which can be modified using Thread.sigmask. This set is inherited at thread creation time. Per-thread signal masks are supported only by the system thread library under Unix, but not under Win32, nor by the VM thread library.

valsigmask :Unix.sigprocmask_command ->``int list``-> ``int list

sigmask cmd sigs changes the set of blocked signals for the calling thread. If cmd is SIG_SETMASK, blocked signals are set to those in the list sigs. If cmd is SIG_BLOCK, the signals in sigs are added to the set of blocked signals. If cmd is SIG_UNBLOCK, the signals in sigs are removed from the set of blocked signals. sigmask returns the set of previously blocked signals for the thread.

valwait_signal : ``int list``-> int

wait_signal sigs suspends the execution of the calling thread until the process receives one of the signals specified in the list sigs. It then returns the number of the signal received. Signal handlers attached to the signals in sigs will not be invoked. The signals sigs are expected to be blocked before calling wait_signal.

Uncaught exceptions

valdefault_uncaught_exception_handler : ``exn-> unit

Thread.default_uncaught_exception_handler will print the thread's id, exception and backtrace (if available).

valset_uncaught_exception_handler : ``(``exn->unit)``-> unit

Thread.set_uncaught_exception_handler fn registers fn as the handler for uncaught exceptions.

If the newly set uncaught exception handler raise an exception, default_uncaught_exception_handler will be called.