rb_thread_create のサンプル書いてみた。
下記コマンド実行で動くはず。
$ ruby extconf.rb $ make $ ruby fib.rb
test1.c と fib.c が拡張ライブラリで、fib.rb が拡張ライブラリ利用者。
(fib.rb)$LOAD_PATH << "." require "test" include Test Test.thread(fib.c)
#include <stdio.h> int fib( int n ) { if( n < 0 ) return -1; if( n == 0 ) return 1; if( n == 1 ) return 1; if( n > 1 ) return fib( n-1 ) + fib( n-2 ); }(test1.c)
#include "ruby.h" int fib(); int gloval = 0; VALUE wrap_fib( VALUE self ) { int n; int count = 0; for( count = 0; count < 30; count++ ) { n = gloval; printf( "fib(%d):%d\n", n, fib(n) ); gloval = (n + 1) % 30; } return Qnil; } VALUE wrap_thread( VALUE self ) { rb_thread_create( wrap_fib, NULL); return Qnil; } void Init_test() { VALUE module; module = rb_define_module( "Test" ); rb_define_module_function( module, "thread", wrap_thread, 0 ); }(extconf.rb)
require 'mkmf' create_makefile( "test" )
0 件のコメント:
コメントを投稿