RubyInline passing VALUE with pthread

I am trying to apply C multithreading to a ruby class method
Here is the code:

require ‘inline’

class Example

inline :C do |builder|
builder.include “<pthread.h>”

builder.c '
static void run_thread(VALUE name){
    // I added a ruby User class to test posting a variable from

thread to db
VALUE user = rb_const_get( rb_cObject, rb_intern(“User”) );
rb_funcall(user, rb_intern(“add”), 1, name);
}’

builder.c '
static void simple(VALUE name){
    run_thread(self, name);
}'


builder.c '
static void threads(VALUE name){
    pthread_t pth;

    pthread_create(&pth, NULL, (void *)run_thread, (VALUE *)name );

    pthread_join(pth, NULL);
}'

end

end

if I run Example.new.run_thread(“Adam”) i get the expected result

(0.3ms) BEGIN
SQL (19.9ms) INSERT INTO users (created_at, name, updated_at)
VALUES (‘2012-03-16 04:53:15’, ‘Adam’, ‘2012-03-16 04:53:15’)
(0.5ms) COMMIT

if I run Example.new.simple(“Adam”) I get the expected result

(0.3ms) BEGIN
SQL (19.9ms) INSERT INTO users (created_at, name, updated_at)
VALUES (‘2012-03-16 04:53:15’, ‘Adam’, ‘2012-03-16 04:53:15’)
(0.5ms) COMMIT

if I run Example.new.threads(“Adam”) i get the error

[FATAL] failed to allocate memory

I think the problem is that I am not passing the VALUE properly to
thread.
I would need guidance with this please. Thank you.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs