[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

threading question, are ops on builtins atomic?

Sam Roberts

2/7/2005 11:57:00 PM


Can I do Array#push from multiple threads at the same time, and know
that all the objs pushed from the threads are on the Array, in some
indeterminate order? What about Array#delete from multiple threads,
and know it was deleted (though only by one of the threads)?

Can I do Socket#recvfrom and Socket#sendto from multiple threads?

I think the answers should be yes, because ruby threads aren't real
threads, they are more mechanisms for dealing with blocking operations
(like socket calls and explicit Mutex) than threads.

I don't want to start peppering my code with unnecessary
Mutex#synchronize calls!

Cheers,
Sam



6 Answers

Florian Gross

2/8/2005

0

Sam Roberts wrote:

> Can I do Array#push from multiple threads at the same time, and know
> that all the objs pushed from the threads are on the Array, in some
> indeterminate order? What about Array#delete from multiple threads,
> and know it was deleted (though only by one of the threads)?

AFAIK yes, because there are usually no Ruby thread context switches in
C functions. But note that += and friends are not atomic.

Charles Mills

2/8/2005 12:25:00 AM

0


Florian Gross wrote:
> Sam Roberts wrote:
>
> > Can I do Array#push from multiple threads at the same time, and
know
> > that all the objs pushed from the threads are on the Array, in some
> > indeterminate order? What about Array#delete from multiple threads,
> > and know it was deleted (though only by one of the threads)?
>
> AFAIK yes, because there are usually no Ruby thread context switches
in
> C functions. But note that += and friends are not atomic.

Is / will this be the case when using pthreads?
in Ruby 2.0?

-Charlie

Yukihiro Matsumoto

2/8/2005 4:18:00 AM

0

Hi,

In message "Re: threading question, are ops on builtins atomic?"
on Tue, 8 Feb 2005 09:30:09 +0900, "Charles Mills" <cmills@freeshell.org> writes:

|Is / will this be the case when using pthreads?
|in Ruby 2.0?

No.

matz.


Glenn Parker

2/8/2005 1:20:00 PM

0

Yukihiro Matsumoto wrote:
> "Charles Mills" <cmills@freeshell.org> writes:
>
> |Is / will this be the case when using pthreads?
> |in Ruby 2.0?
>
> No.

Matz, could you please elaborate just a little bit? I *think* what you
are saying is that operations like Array#delete will *not* be atomic
under pthreads, but it's hard to tell. So, I will try asking again.

What sorts of operations, if any, will be implicitly atomic under pthreads?

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...


Charles Mills

2/8/2005 4:31:00 PM

0


Glenn Parker wrote:
> Yukihiro Matsumoto wrote:
> > "Charles Mills" <cmills@freeshell.org> writes:
> >
> > |Is / will this be the case when using pthreads?
> > |in Ruby 2.0?
> >
> > No.
>
> Matz, could you please elaborate just a little bit? I *think* what
you
> are saying is that operations like Array#delete will *not* be atomic
> under pthreads, but it's hard to tell. So, I will try asking again.

I imagine making operations atomic under pthreads is difficult to
implement (and slow) since Ruby cannot control the context switching
(or at least to the degree it can now using its own thread
implementation), so everything would have to use its own locking
scheme.

>
> What sorts of operations, if any, will be implicitly atomic under
pthreads?

Good question. I assume IO would by one, since when you compile with
pthreads most C stdlibs make stdio functions thread safe, but then I
think ruby 2.0 will not be using stdio. (Maybe this is partly to
ensure IO functions are thread safe?)
....rambling...

-Charlie

Yukihiro Matsumoto

2/8/2005 11:01:00 PM

0

Hi,

In message "Re: threading question, are ops on builtins atomic?"
on Tue, 8 Feb 2005 22:20:21 +0900, Glenn Parker <glenn.parker@comcast.net> writes:

|> |Is / will this be the case when using pthreads?
|> |in Ruby 2.0?
|>
|> No.
|
|Matz, could you please elaborate just a little bit? I *think* what you
|are saying is that operations like Array#delete will *not* be atomic
|under pthreads, but it's hard to tell. So, I will try asking again.

It's hard to assure what would be atomic under the pthread
environment, because atomicity changes for each implementation of
pthread. So the most exact answer would be "no, there's no assured
atomic operation without explicit semaphore".

matz.