Yukihiro Matsumoto wrote: > In message "Re: embedding Ruby 1.9.0 inside pthread" > on Tue, 4 Mar 2008 09:30:34 +0900, Nobuyoshi Nakada <nobu / ruby-lang.org> writes: > > |At Tue, 4 Mar 2008 07:50:00 +0900, > |Suraj Kurapati wrote in [ruby-core:15756]: > |> I'm having trouble embedding Ruby inside a pthread because calling > |> ruby_sysinit() is segfaulting. > | > |You can't call ruby_init() in child threads. > > Because GC needs to know system stack address, that is only taken from > main thread. Ah! Thank you, both, for the explanation. I moved the ruby initialization calls to the main thread as you suggested and the example worked! The revised main.c is now: $ cat main.c #include <stdio.h> #include <pthread.h> #include <ruby.h> pthread_t gRubyThread; pthread_mutex_t gCProgLock; void* gRubyThread_body(void* dummy) { char* file = "hello.rb"; // the file to run printf("Ruby interpreter is loading file: %s\n", file); void* node = rb_load_file(file); printf("Ruby thread is starting interpreter\n"); ruby_run_node(node); printf("Ruby thread is done, waking up C program...\n"); pthread_mutex_unlock(&gCProgLock); return NULL; } RUBY_GLOBAL_SETUP int main(int argc, char** argv) { int fake_argc = 0; char* fake_argv[1]; printf("C program is calling ruby_sysinit()\n"); ruby_sysinit(&fake_argc, &fake_argv); printf("C program is calling RUBY_INIT_STACK()\n"); RUBY_INIT_STACK; printf("C program is calling ruby_init()\n"); ruby_init(); printf("C program is putting Ruby thread in control...\n"); pthread_mutex_init(&gCProgLock, NULL); pthread_mutex_lock(&gCProgLock); pthread_create(&gRubyThread, NULL, gRubyThread_body, NULL); pthread_mutex_lock(&gCProgLock); // C program blocks here printf("C program is back in control, exiting...\n"); return 0; } Running this new main.c program gives the expected output: $ ./main.so C program is calling ruby_sysinit() C program is calling RUBY_INIT_STACK() C program is calling ruby_init() C program is putting Ruby thread in control... Ruby interpreter is loading file: hello.rb Ruby thread is starting interpreter Hello World! Ruby thread is done, waking up C program... C program is back in control, exiting... However, when I tried loading RubyGems inside hello.rb, Ruby gives an error: hello.rb:2:in `require': no such file to load -- rubygems (LoadError) from hello.rb:2:in `<main>' $ cat hello.rb puts "Hello World!" require 'rubygems' puts "rubygems OK!" What must I do to initialize rubygems inside the C program? Thanks for your consideration.