class Rufus::Scheduler::Job

def start_work_thread

def start_work_thread
  thread =
    Thread.new do
      ct = Thread.current
      ct[:rufus_scheduler_job] = true
        # indicates that the thread is going to be assigned immediately
      ct[@scheduler.thread_key] = true
      ct[:rufus_scheduler_work_thread] = true
      loop do
        break if @scheduler.started_at == nil
        job, time = @scheduler.work_queue.pop
        break if job == :shutdown
        break if @scheduler.started_at == nil
        next if job.unscheduled_at
        begin
          (job.opts[:mutex] || []).reduce(
            lambda { job.trigger_now(time) }
          ) do |b, m|
            lambda { mutex(m).synchronize { b.call } }
          end.call
        rescue KillSignal
          # simply go on looping
        end
      end
    end
  thread[@scheduler.thread_key] = true
  thread[:rufus_scheduler_work_thread] = true
    #
    # same as above (in the thead block),
    # but since it has to be done as quickly as possible.
    # So, whoever is running first (scheduler thread vs job thread)
    # sets this information
  thread
end