From: elise huard Date: 2011-04-29T16:27:08+09:00 Subject: Re: Allocate two objects simultaneously in multi-threading Hi Vince, good point, but at the moment this is not an issue, thanks to the Global Interpreter Lock: only one Ruby thread is ever executed at the same time. While this is not a great situation, it will at least make sure that such a problem will never occur. http://en.wikipedia.org/wiki/Global_Interpreter_Lock Elise On Tue, Apr 26, 2011 at 7:48 AM, dot c. wrote: > I'm studying Ruby 1.9.2 source code. Some issues about simultaneity. > > Object.new will call following funtion to allocate space from heap: > static VALUE  rb_class_allocate_instance(VALUE klass) > { >    NEWOBJ(obj, struct RObject); >    OBJSETUP(obj, klass, T_OBJECT); >    return (VALUE)obj; > } > > #define NEWOBJ(obj,type) type *obj = (type*)rb_newobj() > > VALUE > rb_newobj(void) > { >    if (during_gc) { >  dont_gc = 1; >  during_gc = 0; >  rb_bug("object allocation during garbage collection phase"); >    } > >    return rb_newobj_from_heap(objspace); > } > > static VALUE > rb_newobj_from_heap(rb_objspace_t *objspace) > { >    VALUE obj; > >    if ((ruby_gc_stress && !ruby_disable_gc_stress) || !freelist) { >  if (!heaps_increment(objspace) && !garbage_collect(objspace)) { >      during_gc = 0; >      rb_memerror(); >  } >    } > >    obj = (VALUE)freelist; >    freelist = freelist->as.free.next; > >    MEMZERO((void*)obj, RVALUE, 1); > >    return obj; > } > > > > I doesn’t see any code(in rb_newobj_from_heap), which is to make sure > only one thread can call the function rb_newobj at the same time in > multi-threading. > The issue found in many other places. It seems that there can be only > one thread in Ruby. But class Thread can create another thread. > > x = Thread.new { > for a in 1..999 >  Object.new > end > } > for b in 1..999 >  Object.new > End > > Please tell me if some other mechanism solve the simultaneity issue. > Thanks Vince > > -- > Posted via http://www.ruby-forum.com/. >