From: Niki Yoshiuchi <aplusbi@gmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] Using C threads
Date: Mon, 28 Feb 2011 16:56:33 -0500 [thread overview]
Message-ID: <AANLkTim-aovgVd4uKvjDbq8J-8VeTh=S5CLYz3QcEMxL@mail.gmail.com> (raw)
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
next reply other threads:[~2011-02-28 21:56 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-28 21:56 Niki Yoshiuchi [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='AANLkTim-aovgVd4uKvjDbq8J-8VeTh=S5CLYz3QcEMxL@mail.gmail.com' \
--to=aplusbi@gmail.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox