Class: Worker::Threader

Inherits:
Object
  • Object
show all
Defined in:
lib/buzzcore/extra/thread_utils.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aWorkerClass, &aCreateBlock) ⇒ Threader

Returns a new instance of Threader.



491
492
493
494
495
496
497
498
499
# File 'lib/buzzcore/extra/thread_utils.rb', line 491

def initialize(aWorkerClass,&aCreateBlock)
	@create_proc = aCreateBlock
	@worker_class = aWorkerClass
	if @create_proc
		@worker = @create_proc.call(@worker_class)
	else
		@worker = @worker_class.new
	end
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



483
484
485
# File 'lib/buzzcore/extra/thread_utils.rb', line 483

def thread
  @thread
end

#workerObject (readonly)

Returns the value of attribute worker.



483
484
485
# File 'lib/buzzcore/extra/thread_utils.rb', line 483

def worker
  @worker
end

Class Method Details

.start_new(aWorkerClass, aTimeout = 0.1, &aCreateBlock) ⇒ Object



485
486
487
488
489
# File 'lib/buzzcore/extra/thread_utils.rb', line 485

def self.start_new(aWorkerClass,aTimeout=0.1,&aCreateBlock)
	threader = Threader.new(aWorkerClass,&aCreateBlock)
	threader.start(aTimeout)
	return threader
end

Instance Method Details

#start(aTimeout = 0.1) ⇒ Object



501
502
503
504
# File 'lib/buzzcore/extra/thread_utils.rb', line 501

def start(aTimeout=0.1)
	@thread = Thread.new(@worker) { |aWorker| aWorker.main_proc }
	@worker.wait_for_started(aTimeout)
end

#stop(aTimeout = 0.1) ⇒ Object



506
507
508
509
510
511
512
513
# File 'lib/buzzcore/extra/thread_utils.rb', line 506

def stop(aTimeout=0.1)
	@worker.stop
	@worker.wait_for_stopped(aTimeout)
	@thread.exit unless !@thread or (@thread.join(0) and not @thread.alive?)
	#@thread.join()
	@worker = nil
	@thread = nil
end