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

  • SEARCH
  • Home
  • 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 639155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:48:41+00:00 2026-05-13T20:48:41+00:00

First one: how can I create a Thread that doesn’t start right away.If I

  • 0

First one:

  • how can I create a Thread that doesn’t start right away.If I use initialize without a block an exception gets raised.

  • how can I subclass Thread, so that I may add some custom attributes, but keep the same functionality as the base Thread class? I’d also like to not have to use the initialize(&block) method for this.

To better illustrate this:

For the first question:

x = Thread.new
x.run = {
  # this should happen inside the thread
}
x.start # i want to manually start the thread

For the second:

x = MyThread.new
x.my_attribute = some_value
x.run = {
  # this should happen when the thread runs
}
x.start

I’m looking for something similar to this. Hope you can help.

  • 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-05-13T20:48:41+00:00Added an answer on May 13, 2026 at 8:48 pm

    Question 1

    Examining the MRI 1.8.7 source revealed no obvious way to start a thread in the “stopped” state.

    What you can do is to have the thread block on a locked mutex, then unlock the mutex when you want the thread to go.

    #!/usr/bin/ruby1.8
    
    go = Mutex.new
    go.lock
    t = Thread.new do
      puts "Thread waiting to go"
      go.lock
      puts "Thread going"
    end
    puts "Telling the thread to go"
    go.unlock
    puts "Waiting for the thread to complete"
    t.join
    
    # => Thread waiting to go
    # => Telling the thread to go
    # => Thread going
    # => Waiting for the thread to complete
    

    Question 2 (Sort Of)

    Did you know you can pass arguments to your thread? Anything passed to Thread.new gets passed through as block arguments:

    #!/usr/bin/ruby1.8
    
    t = Thread.new(1, 2, 3) do |a, b, c|
      puts "Thread arguments: #{[a, b, c].inspect}"
      # => Thread arguments: [1, 2, 3]
    end
    

    There are also “thread local variables,” a per-thread key/value store. Use Thread#[]= to set values, and Thread#[] to get them back. You can use string or symbols as keys.

    #!/usr/bin/ruby1.8
    
    go = Mutex.new
    go.lock
    t = Thread.new(1, 2, 3) do |a, b, c|
      go.lock
      p Thread.current[:foo]    # => "Foo!"
    end  
    t[:foo] = "Foo!"
    go.unlock
    t.join
    

    Question 2, Really

    You can do what you want to do. It’s a lot of work, especially when the usual way of handling threads is so simple. You’ll have to weigh the plusses and minuses:

    #!/usr/bin/ruby1.8
    
    require 'forwardable'
    
    class MyThread
    
      extend Forwardable
    
      def_delegator :@thread, :join
      def_delegator :@thread, :[]=
      def_delegator :@thread, :[]
    
      def initialize
        @go = Mutex.new
        @go.lock
        @thread = Thread.new do
          @go.lock
          @stufftodo.call
        end
      end
    
      def run(&block)
        @stufftodo = block
        @go.unlock
        @thread.join
      end
    
    end
    
    t = MyThread.new
    t[:foo] = "Foo!"
    t.run do
      puts Thread.current[:foo]
    end
    t.join
    
    # => "Foo!"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Java's ConcurrentMap for a map that can be used from
I've got a one-to-many relationship set up. (Ex. A Person with many Phone Numbers).
I am using this PictureBox on a form, to this picture box I use
I've got a multithreaded app that manipulates in-memory data (no database or network access).
I have to execute some code in the context of the main thread. I
I have a supposedly single-threaded FLTK application with a popup menu, created with Fluid.
I had my webpage validated for xhtml transitional till I added this table (see
I'm trying to work out in my head the best way to structure a
The Problem Our company make specialized devices running Windows XP (Windows XPe, to be
I have some basic questions about core data (which I am new to) and

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.