Ocaml beginner here!
I am trying to write a program that will process both Midi and OSC (open sound control) messages. The idea is to generate midi algorithmically and controlling the parameters of the program through OSC messages.
However, I run into problems if I try to combine the two in parallel (using Thread.create & Thread.join).
I think I need threads because both functions are blocking (the OSC receive function (S.recv) blocks until it receives a message, so I cannot call it directly in the Midi generating callback).
let receive_osc_thread () = let port = 7777 in let s = S.create port myHandler in while true do S.recv s (* this blocks until a message is received, therefore I need the Thread. *) done let () = let t1 = Thread.create midi_thread () in let t2 = Thread.create receive_osc_thread () in List.iter Thread.join [t2;t1]
It tends to crash after some time with a “segmentation fault: 11”. I have tried acquiring and releasing the caml runtime (as in: Chapter 20 Interfacing C with OCaml) in various locations of the C code of my Jack Midi client, but cannot get rid of the segfaults.
My question: why do segfaults occur when I combine two threads? Would there be a better way to combine two blocking C calls like this? I have been reading Threads, but feel that maybe my general approach is just not right :-).