caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Using C threads
@ 2011-02-28 21:56 Niki Yoshiuchi
  2011-03-01 12:14 ` Dmitry Bely
  0 siblings, 1 reply; 9+ messages in thread
From: Niki Yoshiuchi @ 2011-02-28 21:56 UTC (permalink / raw)
  To: caml-list

I'm trying to call some OCaml code from a thread created in C.  I know
I'm supposed to call caml_c_thread_{un}register, however this pretty
much guarantees a segfault.  If I leave them out, my code works fine
so long as I don't try and do much of anything.  Here's my code
stripped down to the bare minimum:

C code:

void *my_thread(void *ptr)
{
    caml_c_thread_register();
    for(;;)
    {
        caml_acquire_runtime_system();
        caml_callback(*caml_named_value("my_callback"), Val_unit);
        caml_release_runtime_system();
    }
    caml_c_thread_unregister(); // I realize this will never be
called.  I've tried without the for loop as well, same result
}

CAMLprim value caml_create_callback(value unit)
{
    pthread_t thread1;
    pthread_create(&thread1, NULL, my_thread, NULL);
    return Val_unit;
}

OCaml code:

external create_callback : unit -> unit = "caml_create_callback"

let my_thread () =
    ()

let _ =
    Callback.register "my_callback" random_thread;
    create_callback ();
    while true do
        ()
    done

This code will segfault immediately.  If I remove the thread_register
functions it will run as expected.  If I try and do anything more than
just return unit, it will segfault unless I add in some sleep
statements.  I'm assuming that the sleep function acts as a sort of
poor-man's thread synchronizer.  I've tried endless permutations

Other important info - I'm using OCaml 3.12 and gcc 4.3.2 on Linux.
The return value of caml_c_thread_register is 1, which means that it
is successfully returning.

Thanks,
-Niki Yoshiuchi

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2011-03-04  9:21 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-28 21:56 [Caml-list] Using C threads Niki Yoshiuchi
2011-03-01 12:14 ` Dmitry Bely
2011-03-03 22:16   ` Niki Yoshiuchi
2011-03-03 23:03     ` Dmitry Bely
2011-03-03 23:23       ` Niki Yoshiuchi
2011-03-03 23:38         ` Dmitry Bely
2011-03-03 23:46           ` Niki Yoshiuchi
2011-03-04  0:17             ` Dmitry Bely
2011-03-04  9:21             ` xclerc

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).