- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在努力将参数传递给 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 的初学者,但我需要创建一个可以使用库而不受单个线程限制的扩展。看我的other question .
当我制作
扩展时,我收到一条警告
warning: incompatible pointer types passing 'VALUE (VALUE, VALUE)' to parameter of type 'void *(*)(void *)'
虽然我明白它说的是什么,但我不知道如何修复它。我应该忽略它吗?此外,当我运行 client.rb
时,它调用 obj.no_gvl
时出现段错误。
我在 Mac OSX 10.10.5 上,我通过 rbenv
使用 Ruby 2.0.0-p247。
最佳答案
如果你还没有看过,the source for rb_thread_call_without_gvl
includes some documentation . (我已经链接到您正在使用的版本,但那是一个 pretty old Ruby ,如果可能,您应该考虑更新。这个 API 在当前版本中是相同的,至少到 2.3.1。)
函数原型(prototype)如下:
void *rb_thread_call_without_gvl(void *(*func)(void *), void *data1,
rb_unblock_function_t *ubf, void *data2);
被调用的函数应该接受单个 void *
参数并返回 void *
,即它是一个普通的 C 函数,不是 一个实现 Ruby 方法的 C 函数,正如您在示例中所使用的那样。事实上,它不能实现 Ruby 方法,因为这样做意味着访问受 GVL 保护的结构。
要使用它,您需要将要在没有锁定的情况下执行的代码移动到具有正确接口(interface)的函数中,并且不使用任何 Ruby API。这是一个示例(基于您自己的示例),它创建了一个 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)
关于c - Ruby 全局解释器锁 (GIL) - rb_thread_call_without_gvl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37061787/
我的应用程序中有一个 settings.php 页面,它使用 $GLOBALS 来存储网络应用程序中使用的配置。 例如,他是我使用的一个示例设置变量: $GLOBALS["new_login_page
我正在尝试编译我们在 OS 类上获得的简单操作系统代码。它在 Ubuntu 下运行良好,但我想在 OS X 上编译它。我得到的错误是: [compiling] arch/i386/arch/start
我知道distcp无法使用通配符。 但是,我将需要在更改的目录上安排distcp。 (即,仅在星期一等“星期五”目录中复制数据),还从指定目录下的所有项目中复制数据。 是否有某种设计模式可用于编写此类
是否可以在config.groovy中全局定义资源格式(json,xml)的优先级,而不是在每个Resource上指定?例如,不要在@Resource Annotation的参数中指定它,例如: @R
是否有一些简单的方法来获取大对象图的所有关联,而不必“左连接获取”所有关联?我不能只告诉 Hibernate 默认获取 eager 关联吗? 最佳答案 即使有可能有一个全局 lazy=false(谷歌
我正在尝试实现一个全局加载对话框...我想调用一些静态函数来显示对话框和一些静态函数来关闭它。与此同时,我正在主线程或子线程中做一些工作...... 我尝试了以下操作,但对话框没有更新...最后一次,
当我偶然发现 this question 时,我正在阅读更改占位符文本。 无论如何,我回去学习了占位符。一个 SO 的回答大致如下: Be careful when designing your pl
例如,如果我有这样的文字: "hello800 more text 1234 and 567" 它应该匹配 1234 和 567,而不是 800(因为它遵循 hello 的 o,这不是一个数字)。 这
我一直在尝试寻找一种无需使用 SMS 验证系统即可验证电话号码(Android 和 iPhone)的方法。原因纯粹是围绕成本。我想要一个免费的解决方案。 我可以安全地假设 Android 操作系统会向
解决此类问题的规范 C++ 设计模式是什么? 我有一些共享多个类的多线程服务器。我需要为大多数类提供各种运行时参数(例如服务器名称、日志记录级别)。 在下面的伪 C++ 代码中,我使用了一个日志记录类
这个问题在这里已经有了答案: Using global variables in a function (25 个答案) 关闭 9 年前。 我是 python 的新手,所以可能有一个简单的答案,但我
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Does C++ call destructors for global and class static
我正在尝试使用 Objective-C 中的 ArrayList 的等价物。我知道我必须使用 NSMutableArray。我想要一个字符串列表 (NSString)。关键是我的列表应该可以从我类(c
今天刚开始学习 Android 开发,我找不到任何关于如何定义 Helper 类或将全局加载的函数集合的信息,我会能够在我创建的任何 Activity 中使用它们。 我的计划是创建(至少目前)2 个几
为什么这段代码有效: var = 0 def func(num): print num var = 1 if num != 0: func(num-1) fun
$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven'); $alter = &$GLOBALS
我想知道如何实现一个可以在任何地方使用您自己的设置的全局记录器: 我目前有一个自定义记录器类: class customLogger(logging.Logger): ... 该类位于一个单独的
我需要使用 React 测试库和 Jest 在我的测试中模拟不同的窗口大小。 目前我必须在每个测试文件中包含这个beforeAll: import matchMediaPolyfill from 'm
每次我遇到单例模式或任何静态类(即(几乎)只有静态成员的类)的实现时,我想知道这是否实际上不是一种黑客行为,因此只是为了设计而严重滥用类和实例的原则单个对象,而不是设计类和创建单个实例。对我来说,看起
这个问题在这里已经有了答案: Help understanding global flag in perl (2 个回答) 7年前关闭。 my $test = "There was once an\n
我是一名优秀的程序员,十分优秀!