Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Thread safety

Reply
Thread Tools

Thread safety

 
 
Jeff Turc
Guest
Posts: n/a
 
      02-15-2008
With the discussion surrounding merb/rails and thread safety, I've been
somewhat concerned with making my own tiny home-rolled web apps that I
write thread safe, if only to become more knowledgeable on the subject.

I know it's probably hard to generalize, but does anybody have any
simple rules to follow (or resources to share) for writing thread safe
code?

Thanks!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
s.ross
Guest
Posts: n/a
 
      02-15-2008
On Feb 15, 2008, at 10:54 AM, Jeff Turc wrote:

> With the discussion surrounding merb/rails and thread safety, I've
> been
> somewhat concerned with making my own tiny home-rolled web apps that I
> write thread safe, if only to become more knowledgeable on the
> subject.
>
> I know it's probably hard to generalize, but does anybody have any
> simple rules to follow (or resources to share) for writing thread safe
> code?
>
> Thanks!



Anyone else jump right in here. These are mine:

Rule #1: Don't use global variables.
Rule #2: If you do, when you change them, wrap them in a blocking
mechanism such as a mutex or critical section so two threads can't try
at once.
Rule #3: If you are changing the state of an object that is not thread-
local, wrap the change in a mutex (etc.)

So, for example:

require 'thread'
my_object_sync = Mutex.new

Thread.new do
# just lock on this object
my_object_sync.synchronize do
# Update that is not atomic
end
end

Rule #4: Don't be too sure that any operation in Ruby is atomic.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-15-2008
On 15.02.2008 21:08, s.ross wrote:
> On Feb 15, 2008, at 10:54 AM, Jeff Turc wrote:
>
>> I know it's probably hard to generalize, but does anybody have any
>> simple rules to follow (or resources to share) for writing thread safe
>> code?


> Rule #3: If you are changing the state of an object that is not thread-
> local, wrap the change in a mutex (etc.)


Corollary: use thread confinement, i.e. design your app in a way that
most objects are used in a single thread at a time only. Restrict need
for synchronization to the minimum needed.

> Rule #4: Don't be too sure that any operation in Ruby is atomic.


Somehow this reminds me of "Blondie" and "Atomic". Those were the
days...

Cheers

robert
 
Reply With Quote
 
s.ross
Guest
Posts: n/a
 
      02-15-2008
> Somehow this reminds me of "Blondie" and "Atomic". Those were
> the days...


One way or another

 
Reply With Quote
 
Jeff Turc
Guest
Posts: n/a
 
      02-15-2008
Steve Ross wrote:
>> Somehow this reminds me of "Blondie" and "Atomic". Those were
>> the days...

>
> One way or another


I'm gonna getcha!

Thanks guys. big help.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jeff Turc
Guest
Posts: n/a
 
      02-17-2008
For anyone else starting out with threads, I also stumbled across this:

8 Simple Rules for Designing Threaded Applications
http://www.devx.com/go-parallel/Article/37034

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
MenTaLguY
Guest
Posts: n/a
 
      02-21-2008
On Mon, 2008-02-18 at 06:55 +0900, Jeff Turc wrote:
> For anyone else starting out with threads, I also stumbled across this:
>
> 8 Simple Rules for Designing Threaded Applications
> http://www.devx.com/go-parallel/Article/37034


I think this is a good article and I wouldn't mind recommending it to
others.

Thanks Jeff!

-mental


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread-safety and Singleton methods =?Utf-8?B?RGlmZmlkZW50?= ASP .Net 1 01-13-2005 06:07 PM
Thread safety when subclassing the Page class thechaosengine ASP .Net 2 12-10-2004 02:48 PM
What is thread safety? Hans ASP .Net 1 10-12-2004 03:15 PM
A thread safety question Simon Harvey ASP .Net 3 08-06-2004 02:17 PM
LiteralControl thread safety. George Ter-Saakov ASP .Net 1 04-06-2004 10:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57