Allocate two objects simultaneously in multi-threading

D

dot c.

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 =3D (type*)rb_newobj()

VALUE
rb_newobj(void)
{
if (during_gc) {
dont_gc =3D 1;
during_gc =3D 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 =3D 0;
rb_memerror();
}
}

obj =3D (VALUE)freelist;
freelist =3D freelist->as.free.next;

MEMZERO((void*)obj, RVALUE, 1);

return obj;
}



I doesn=E2=80=99t 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 =3D 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/.=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top