# Ruby Threads From C

**URL:** <https://www.ruby-forum.com/t/ruby-threads-from-c/182758>\
**Category:** Ruby\
**Created:** [February 2, 2010, 11:43am UTC](https://www.ruby-forum.com/t/ruby-threads-from-c/182758 "2010-02-02T11:43:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![coldpeace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f4b2a3/32.png) [@coldpeace](https://www.ruby-forum.com/u/coldpeace)\
**Post date:** [February 2, 2010, 11:43am UTC](https://www.ruby-forum.com/t/ruby-threads-from-c/182758/1 "2010-02-02T11:43:11Z")

</div>

Hi,

I’d like to know how can I do to create ( or instantiate ) a Ruby Thread  
from C .  
actually , I have to Bind Some C Modules to Ruby , So I have to manage  
The ruby Thread inside my C code ( Start , Stop , Resume …ect )

Thx 😉

---

<div class="post-metadata">

**Author:** ![coldpeace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f4b2a3/32.png) [@coldpeace](https://www.ruby-forum.com/u/coldpeace)\
**Post date:** [February 2, 2010, 5:45pm UTC](https://www.ruby-forum.com/t/ruby-threads-from-c/182758/2 "2010-02-02T17:45:30Z")

</div>

> I’d like to know how can I do to create ( or instantiate ) a Ruby Thread  
> from C .

I’d just lookup the Thread class and call a “new” on it, passing it a  
block.

-r

---

<div class="post-metadata">

**Author:** ![coldpeace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f4b2a3/32.png) [@coldpeace](https://www.ruby-forum.com/u/coldpeace)\
**Post date:** [February 2, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/ruby-threads-from-c/182758/3 "2010-02-02T20:50:42Z")

</div>

On Feb 2, 7:45 am, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I’d like to know how can I do to create ( or instantiate ) a Ruby Thread  
> > from C .
> 
> I’d just lookup the Thread class and call a “new” on it, passing it a  
> block.
> 
> -r
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Do you you have an example of doing this from within C that you can  
point to? My C isn’t at an expert level, and I’ve had some trouble  
finding an example that I can work with to do this sort of thing.

Thanks for any links or ideas.

Mike B.

---

<div class="post-metadata">

**Author:** ![coldpeace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f4b2a3/32.png) [@coldpeace](https://www.ruby-forum.com/u/coldpeace)\
**Post date:** [February 2, 2010, 10:52pm UTC](https://www.ruby-forum.com/t/ruby-threads-from-c/182758/4 "2010-02-02T22:52:16Z")

</div>

> Do you you have an example of doing this from within C that you can  
> point to? My C isn’t at an expert level, and I’ve had some trouble  
> finding an example that I can work with to do this sort of thing.

Appears there’s also an rb\_thread\_create, though I have no idea how to  
use it.

[http://betterlogic.com/roger/?p=2474&cpage=1#comment-3372](http://betterlogic.com/roger/?p=2474&cpage=1#comment-3372)

might work…the kicker though is that if you’re looking for something  
like

rb\_go\_and\_do\_this\_in\_some\_other\_thread(“stuff”)

Then I don’t think using ruby’s C API to start a thread will work–you  
may need to run the ruby code in a separate C thread.

GL!  
-r

---

<div class="post-metadata">

**Author:** ![coldpeace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f4b2a3/32.png) [@coldpeace](https://www.ruby-forum.com/u/coldpeace)\
**Post date:** [February 3, 2010, 4:58pm UTC](https://www.ruby-forum.com/t/ruby-threads-from-c/182758/5 "2010-02-03T16:58:18Z")

</div>

> Actually , I tried :
> 
> rb\_funcall3(rb\_const\_get(rb\_cObject, rb\_intern(“Thread”)),  
> rb\_intern(“new”), 0, 0);
> 
> But \>\> `initialize’: must be called with a block (ThreadError)

The code at

[http://betterlogic.com/roger/?p=2474&cpage=1#comment-3372](http://betterlogic.com/roger/?p=2474&cpage=1#comment-3372)

uses rb\_iterate which “passes a block” to a method.  
-r

---

<div class="post-metadata">

**Author:** ![coldpeace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f4b2a3/32.png) [@coldpeace](https://www.ruby-forum.com/u/coldpeace)\
**Post date:** [February 3, 2010, 9:49am UTC](https://www.ruby-forum.com/t/ruby-threads-from-c/182758/6 "2010-02-03T09:49:25Z")

</div>

Roger P. wrote:

> > Do you you have an example of doing this from within C that you can  
> > point to? My C isn’t at an expert level, and I’ve had some trouble  
> > finding an example that I can work with to do this sort of thing.
> 
> Appears there’s also an rb\_thread\_create, though I have no idea how to  
> use it.
> 
> [http://betterlogic.com/roger/?p=2474&cpage=1#comment-3372](http://betterlogic.com/roger/?p=2474&cpage=1#comment-3372)
> 
> might work…the kicker though is that if you’re looking for something  
> like
> 
> rb\_go\_and\_do\_this\_in\_some\_other\_thread(“stuff”)
> 
> Then I don’t think using ruby’s C API to start a thread will work–you  
> may need to run the ruby code in a separate C thread.
> 
> GL!  
> -r

Actually , I tried :

rb\_funcall3(rb\_const\_get(rb\_cObject, rb\_intern(“Thread”)),  
rb\_intern(“new”), 0, 0);

But \>\> `initialize’: must be called with a block (ThreadError)

it seems like the Thread Class should always be called with a Block of  
instrcution to execute as we do with Ruby Code Thread.new{ do … this …  
ect }

I found Some C Methods to deal with Ruby Thread :  
…  
VALUE rb\_thread\_stop (void)  
VALUE rb\_thread\_wakeup (VALUE)  
VALUE rb\_thread\_run (VALUE)  
VALUE rb\_thread\_kill (VALUE)  
VALUE rb\_thread\_create (VALUE(\*(ANYARGS), void \*))  
void rb\_thread\_interrupt (void)  
void rb\_thread\_trap\_eval (VALUE, int)  
void rb\_thread\_signal\_raise (char \*)  
int rb\_thread\_select (ANYARGS)  
void rb\_thread\_wait\_for (ANYARGS)  
VALUE rb\_thread\_current (void)  
VALUE rb\_thread\_main (void)  
…  
[http://ruby-doc.org/doxygen/1.8.4/group\_\_ruby\_\_eval.html](http://ruby-doc.org/doxygen/1.8.4/group __ruby__ eval.html)

But not yet sure that it really create and manage a Ruby Thread (  
especially that this One should be always initialized with a block of  
instructions )
