# Rb\_eval\_string blocks on loops even if run inside a thread

**URL:** <https://www.ruby-forum.com/t/rb-eval-string-blocks-on-loops-even-if-run-inside-a-thread/172040>\
**Category:** Ruby\
**Created:** [July 13, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/rb-eval-string-blocks-on-loops-even-if-run-inside-a-thread/172040 "2009-07-13T19:31:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [July 13, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/rb-eval-string-blocks-on-loops-even-if-run-inside-a-thread/172040/1 "2009-07-13T19:31:16Z")

</div>

Hey,

I’m trying to use rb\_eval\_string to execute a bunch of code and return  
from  
it straight away. My problem is that the code I want to eval contains a  
continuous loop. Even if I wrap the code in a thread the call still  
blocks  
due to the loop. Note that I don’t get the same behaviour when using  
eval()  
outside of the C API.

E.g:

rb\_eval\_string(“Thread.new { loop { puts 1; sleep 0.2 }}”); \<-- blocks  
eval(“Thread.new { loop { puts 1; sleep 0.2 }}”) \<— doesn’t block

My guess is to spawn a ruby thread in C and call rb\_eval\_string inside  
that,  
I can’t find any examples of using the thread functions on google though  
so  
I’m a bit stuck. Could someone give me a few pointers or perhaps some  
example code?

Many thanks  
Ian

---

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [July 13, 2009, 11:25pm UTC](https://www.ruby-forum.com/t/rb-eval-string-blocks-on-loops-even-if-run-inside-a-thread/172040/2 "2009-07-13T23:25:01Z")

</div>

On 7/13/09, Ian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My guess is to spawn a ruby thread in C and call rb\_eval\_string inside that,  
> I can’t find any examples of using the thread functions on google though so  
> I’m a bit stuck. Could someone give me a few pointers or perhaps some  
> example code?

This is the first time I have ever tried embedding Ruby in C, but I  
hope it helps. (The idea is to run ruby from a C thread so the rest  
of your C program can maybe run in parallel):

#include \<ruby.h\>

#include \<stdio.h\>  
#include \<stdlib.h\>  
#include \<pthread.h\>

void _do\_ruby(void_ param)  
{  
ruby\_init();  
rb\_eval\_string(“loop { puts ‘hello from ruby world’; sleep 5 }”);  
ruby\_finalize();  
}

int main(int argc, char \*argv[])  
{  
pthread\_t ruby\_thread;

printf(“BEFORE DO\_RUBY\n”);

pthread\_create( &ruby\_thread, NULL, do\_ruby, NULL);

printf(“AFTER DO\_RUBY\n”);

pthread\_join(ruby\_thread, NULL);

return 0;  
}

> gcc -o foo -lpthread -lruby1.8 -I/usr/lib/ruby/1.8/i486-linux foo.c  
> ./foo  
> BEFORE DO\_RUBY  
> hello from ruby world  
> AFTER DO\_RUBY  
> hello from ruby world  
> hello from ruby world  
> ^C(eval): (eval):0:in `sleep': (Interrupt) from (eval):0 from (eval):0:in `loop’  
> from (eval):0  
> (eval): [BUG] Segmentation fault  
> ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

Abort

But, be aware:  
“ruby is not thread-safe. If you embed ruby into a multi-threaded  
application then you will need to semaphore protect all access to  
ruby. Warning: Insufficient semaphore protection can be hard  
debuggable.” [1]

[1] [ruby embedded into c++](http://metaeditor.sourceforge.net/embed/#id2842564)

---

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [July 14, 2009, 12:45am UTC](https://www.ruby-forum.com/t/rb-eval-string-blocks-on-loops-even-if-run-inside-a-thread/172040/3 "2009-07-14T00:45:40Z")

</div>

Thanks,

I did think of using pthreads, though the code I need to eval will  
regularly  
touch any number of existing objects so I don’t think there’s a way to  
stay  
safe using pthreads. Do you know how to spawn a ruby green thread in C?

2009/7/13 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
