我很难将论点传递给rb_thread_call_without_gvl
。这是我使用的简单代码。
#include <stdio.h>
#include <ruby.h>
#include <ruby/thread.h>
VALUE summa(VALUE self, VALUE x)
{
double result;
result = NUM2DBL(x) + NUM2DBL(x);
printf("The sum in C is %f\n", result);
return DBL2NUM(result);
}
VALUE no_gvl(VALUE self)
{
double arg = 3.0;
double *ptr = &arg;
rb_thread_call_without_gvl(summa, ptr, NULL, NULL);
return Qnil;
}
void Init_csum()
{
VALUE myModule = rb_define_module("MyModule");
VALUE myClass = rb_define_class_under(myModule, "MyClass", rb_cObject);
rb_define_method(myClass, "summa", summa, 1);
rb_define_method(myClass, "no_gvl", no_gvl, 0);
}
然后,我尝试用脚本client.rb
调用Ruby的扩展
require './csum'
obj = MyModule::MyClass.new # MyClass is defined in C
puts "The sum in R is " + obj.summa(7.0).to_s
puts obj.no_gvl
最后我的extconf.rb
require 'mkmf'
extension_name = 'csum'
create_makefile(extension_name)
我是C语言的初学者,但我需要创建一个可以使用库而不受单个线程限制的扩展。看看我的其他问题。
当我make
这个分机时,我收到一条警告:
warning: incompatible pointer types passing 'VALUE (VALUE, VALUE)' to parameter of type 'void *(*)(void *)'
虽然我明白它说了什么,但我看不出如何解决它。我应该忽视它吗?此外,当我运行client.rb
时,当它调用obj.no_gvl
时,会有一个分段错误。
我在MacOSX10.10.5上,我通过rbenv
使用Ruby2.0.0-p247。
发布于 2016-05-06 00:41:49
如果你还没看过,包括一些文档。(我已经链接到您正在使用的版本,但这是一个很老的红宝石,如果可能的话,您应该考虑更新。这个API在当前版本中是相同的,至少可以达到2.3.1。)
该功能原型看起来如下:
void *rb_thread_call_without_gvl(void *(*func)(void *), void *data1,
rb_unblock_function_t *ubf, void *data2);
正在调用的函数应该接受一个void *
参数并返回void *
,即它是一个普通的C函数,而不是像在示例中那样实现void *
方法的C函数。实际上,它不能实现Ruby方法,因为这样做意味着访问受GVL保护的结构。
要使用它,您需要将不使用锁而执行的代码移动到具有正确接口的函数中,而不使用任何Ruby。下面是一个示例(基于您自己的),它创建了一个Ruby方法,该方法将传递给它的参数加倍,并在不使用GVL的情况下完成工作:
#include <stdio.h>
#include <ruby.h>
#include <ruby/thread.h>
// The function that does the work, accepts void* and returns void*
void* doubleArg(void* x) {
// Unpack the arg and cast back to double.
double val = *(double*)x;
double result = val + val;
printf("The sum in C is %f\n", result);
// If you wanted you could wrap up some data here to return to
// Ruby land.
return NULL;
}
// The function implementing the Ruby method
VALUE double_no_gvl(VALUE self, VALUE arg) {
// First wrap up the input as a pointer.
// You'll probably want to do some checking on the type of the
// argument here too.
double argAsDouble = NUM2DBL(arg);
double *ptr = &argAsDouble;
// Now call the function without the GVL.
// It might be worth looking into providing
// an ubf function here too.
rb_thread_call_without_gvl(doubleArg, ptr, NULL, NULL);
return Qnil;
}
void Init_csum() {
VALUE myModule = rb_define_module("MyModule");
VALUE myClass = rb_define_class_under(myModule, "MyClass", rb_cObject);
rb_define_method(myClass, "double_no_gvl", double_no_gvl, 1);
}
您可以用这样的脚本调用它:
require './csum'
obj = MyModule::MyClass.new
obj.double_no_gvl(3.9)
https://stackoverflow.com/questions/37061787
复制相似问题