Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:41:11+00:00 2026-06-01T17:41:11+00:00

Is it 0K to use class variables to communicate with threads in Rails? Are

  • 0

Is it 0K to use class variables to communicate with threads in Rails?

Are there any pros or cons compared to using queues?

I’m not sure if this works in any environment, it worked in mine: Ruby Enterprise + Passenger + nginx + Rails 3

Here’s an example:

# rails runner "ThreadJob.new.run"
class ThreadJob

  @@counter = 0

  def run
    producer = Thread.new do
      1.upto 10 do
        sleep 1
        @@counter+= 1
        puts "Producer: #{@@counter}"
      end
    end

    consumer = Thread.new do
      sleep 0.5
      1.upto 10 do
        sleep 1
        @@counter-= 1
        puts "Consumer: #{@@counter}"
      end
    end

    producer.join
    consumer.join

    puts @@counter # result should be 0
  end
end

I’m wondering if this can work if I modify @@counter from within another process.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:41:13+00:00Added an answer on June 1, 2026 at 5:41 pm

    No, it’s not ok to update class variables from multiple threads like your example.

    Take a look at the explanation of why, and how to solve this using the Mutex class:

    http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it reasonable to use private static variables to establish invariants in your class?
In Java I sometimes use class variables to assign a unique ID to each
I would like to use a class and its class variables as a global
Is it better practice to use class variables, or the session cache in ASP.NET?
Edit: I solved it by getting all of the class variables using get_class_vars(), and
My client will communicate with clients using a struct/class called annoucement. I suppose I
Trying to use class variables in thread and getting EXC_BAS_ACCESS. Code snippet: @interface ViewController
Actually I am using the Rhino plugin in Eclipse. To use a Java class
In my language I can use a class variable in my method when the
In Ruby 1.9, I can use its class variable like the following: class Sample

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.