Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3501

Allocate two objects simultaneously in multi-threading

From "dot c." <dot.cyclone@gmail.com>
Newsgroups comp.lang.ruby
Subject Allocate two objects simultaneously in multi-threading
Date 2011-04-26 00:48 -0500
Organization Service de news de lacave.net
Message-ID <0039bf1f7e71556659570eceb980da34@ruby-forum.com> (permalink)

Show all headers | View raw


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/.

Back to comp.lang.ruby | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Allocate two objects simultaneously in multi-threading "dot c." <dot.cyclone@gmail.com> - 2011-04-26 00:48 -0500
  Re: Allocate two objects simultaneously in multi-threading elise huard <huard.elise@gmail.com> - 2011-04-29 02:27 -0500

csiph-web