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 8343685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:08:29+00:00 2026-06-09T06:08:29+00:00

Creating a class which holds some threads, performing tasks and finally calling a callback-method

  • 0

Creating a class which holds some threads, performing tasks and finally calling a callback-method is my current goal, nothing special on this road.

My experimental class does some connection-checks on specific ports of a given IP, to give me a status information.

So my attempt:

check = ConnectionChecker.new do | threads |
   # i am done callback
end

check.check_connectivity(ip0, port0, timeout0, identifier0)
check.check_connectivity(ip1, port1, timeout1, identifier1)
check.check_connectivity(ip2, port2, timeout2, identifier2)

sleep while not check.is_done

Maybe not the best approach, but in general it fits in my case.

So what’s happening:

In my Class I store a callback, perform actions and do internal stuff:

Thread.new -> success/failure -> mark as done, when all done -> call callback:

 class ConnectionChecker

   attr_reader :is_done

   def initialize(&callback)
     @callback     = callback
     @thread_count = 0
     @threads      = []
     @is_done      = false
   end

   def check_connectivity(host, port, timeout, ident)
     @thread_count += 1
     @threads << Thread.new do

       status = false
       pid    = Process.spawn("nc -z #{host} #{port} >/dev/null")

       begin
         Timeout.timeout(timeout) do
           Process.wait(pid)
           status = true
         end
       rescue Process::TimeoutError => e
         Process.kill('TERM', pid)
       end

       mark_as_done
       #returnvalue for the callback.
       [status, ident]
     end
   end

   # one less to go..
   def mark_as_done
     @thread_count -= 1
     if @thread_count.zero?
       @is_done = true
       @callback.call(@threads)
     end
   end
 end

This code – yes, I know there is no start method so I have to trust that I call it all quite instantly – works fine.

But when I swap these 2 lines:

  @is_done = true
  @callback.call(@threads)

to

  @callback.call(@threads)
  @is_done = true

then the very last line,

sleep while not check.is_done

becomes an endless loop. Debugging shows me that the callback is called properly, when I check for the value of is_done, it really always is false. Since I don’t put it into a closure, I wonder why this is happening.

The callback itself can also be empty, is_done remains false (so there is no mis-caught exception).

In this case I noticed that the last thread was at status running. Since I did not ask for the thread’s value, I just don’t get the hang here.

Any documentation/information regarding this problem? Also, a name for it would be fine.

  • 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-09T06:08:31+00:00Added an answer on June 9, 2026 at 6:08 am

    Try using Mutex to ensure thread safety 🙂

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

Sidebar

Related Questions

I'm creating a class that will have one public method, which returns a value
I'm creating a class for a Lua binding which holds a pointer and can
I'm creating a date class in c++, which holds day, month and year variables
In some C++/CLI code I have a native class which has a factory method
Client has sendpoints() method which is called by some other class that I did
I have a class which inherits QAbstractTableModel, and holds some complex structs in a
I'm creating a static class which is going to hold some vectors with info.
I have an interface, IMessage and a class which have several methods for creating
Suppose you are creating a class with multiple .cpp files (which each contain the
I frequently find myself creating classes which use this form (A): abstract class Animal

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.