From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p1SLue2n003525 for ; Mon, 28 Feb 2011 22:56:40 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AscCAEepa03RVdg2kGdsb2JhbACEJpQKjg0IFQEBAQEJCQwHEQQhoSWJZDyCHYR3L4haAQEDBYEig0R2BIUPhw2ITzo X-IronPort-AV: E=Sophos;i="4.62,242,1297033200"; d="scan'208";a="92257952" Received: from mail-qw0-f54.google.com ([209.85.216.54]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 28 Feb 2011 22:56:34 +0100 Received: by qwj8 with SMTP id 8so4658207qwj.27 for ; Mon, 28 Feb 2011 13:56:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=gcDoZUNl8otMRGqQj+CM6gotYwymgGBv9wlO2HJBzno=; b=uisobRLw6p5KjRLra0iL9+B+NtUb8rK5RPvYxQ3kfSmt+rthB3/9vYzZQeoL3+VY4i TygoJ0FV0Lj3Jlj7fa1/FQxrgrkMwjxEjzMuZ5TOmgA3pts1eEmd4GMQIKEz2kh5SBeM 3SRQLc8N+i0Wqumg9LD1gVA+AjTXaRTe9ELNE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=tcg9SXBPxNZK2ppQeQITjiBvkuuqJ4b1aj1pPzS7BA1bCZ2zazw3TPfz/PFicK4cNs Xs5hEhAaOsDllKigclQ55/9BjS0SKBD3ReoeaI4qzOOo0PF20ojGHqT8IViijXWNcWGp vLbtQqK9WOA5PyXrsf+3SbcAWOyeoolJdE22k= MIME-Version: 1.0 Received: by 10.229.231.21 with SMTP id jo21mr4727860qcb.119.1298930193571; Mon, 28 Feb 2011 13:56:33 -0800 (PST) Received: by 10.229.102.7 with HTTP; Mon, 28 Feb 2011 13:56:33 -0800 (PST) Date: Mon, 28 Feb 2011 16:56:33 -0500 Message-ID: From: Niki Yoshiuchi To: caml-list@inria.fr Content-Type: text/plain; charset=UTF-8 Subject: [Caml-list] Using C threads 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