From: "Suraj N. Kurapati" Date: 2006-04-17T09:25:05+09:00 Subject: Re: cross-thead violation - possible solution? -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Interestingly, the previous example code can be simplified into the following: #include int main(int argc, char** argv) { rb_ary_new(); // causes cross-thread violation on rb_gc() return 0; } Now the reason for the cross-thread violation is quite obvious: you cannot create Ruby objects without initializing the Ruby library first! So, a solution would look like this: #include int main(int argc, char** argv) { ruby_init(); rb_ary_new(); // no cross-thread violation! return 0; } I applied this same technique to the previous example code (with Ruby interpreter inside a pthread. As expected, the cross-thread violation did not occur: #include #include void* ruby_run_handshake(void* dummy) { ruby_init(); ruby_init_loadpath(); ruby_run(); // this never returns } int main(int argc, char** argv) { // run a Ruby interpreter in the background pthread_t rubyThread; pthread_create(&rubyThread, 0, ruby_run_handshake, 0); // create some Ruby objects ruby_init(); rb_ary_new(); // no cross-thread violation! return 0; } Now, I have some questions regarding the above code: 1. Is the Ruby "environment" running inside ruby_run_handshake() different from the Ruby "environment" running inside main()? 2. Does ruby_init() inside main() override the stack pointer prepared by ruby_init() inside ruby_run_handshake()? 3. If we create some Ruby arrays inside main(), will they be garbage collected by the Ruby "environment" inside the pthread? vice versa? Thanks for your attention. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFEQuBUmV9O7RYnKMcRArTkAKCAf6YexFIZtbnH1Y4DWvQucPtWwACdGbQQ hgGSijw1DJTNnXolZGRuSrA= =VXYX -----END PGP SIGNATURE-----