vb.net - What's the proper use of WaitOne() function -
i experimented thread pool examples. i've started fibonacci example on msdn web site, this wasn't suitable more 64 calculations, i've resolved code:
imports system.threading module module1 public class fibonacci private _n integer private _fibofn private _doneevent manualresetevent public readonly property n() integer return _n end end property public readonly property fibofn() integer return _fibofn end end property sub new(byval n integer, byval doneevent manualresetevent) _n = n _doneevent = doneevent end sub ' wrapper method use thread pool. public sub threadpoolcallbackmar(byval threadcontext object) dim threadindex integer = ctype(threadcontext, integer) console.writeline("thread {0} started...", threadindex) _fibofn = calculate(_n) console.writeline("thread {0} result calculated...", threadindex) _doneevent.set() end sub public function calculate(byval n integer) integer if n <= 1 return n end if return calculate(n - 1) + calculate(n - 2) end function end class <mtathread()> sub main() const fibonaccicalculations integer = 65 ' 1 event used each fibonacci object dim doneevents(fibonaccicalculations) manualresetevent dim fibarray(fibonaccicalculations) fibonacci dim r new random() ' configure , start threads using threadpool. console.writeline("launching {0} tasks...", fibonaccicalculations) integer = 0 fibonaccicalculations doneevents(i) = new manualresetevent(false) dim f = new fibonacci(r.next(20, 40), doneevents(i)) fibarray(i) = f threadpool.queueuserworkitem(addressof f.threadpoolcallbackmar, i) next console.writeline("all calculations complete.") integer = 0 fibonaccicalculations doneevents(i).waitone() dim f fibonacci = fibarray(i) console.writeline("fibonacci({0}) = {1}", f.n, f.fibofn) next console.read() end sub end module the use of waitone() instead of waitall() resolve problem question is: if don't need display results don't need neither second loop, but... without second loop i've put waitone() function?
your code this:
// start bunch of threads calculations console.writeline("all calculations complete."); // lie! // wait threads exit the primary problem here calculations are not complete when make call console.writeline. well, might complete, don't know unless you've waited on event see it's signaled.
the purpose of waitone tell if calculation has completed. code should written this:
integer = 0 fibonaccicalculations doneevents(i) = new manualresetevent(false) dim f = new fibonacci(r.next(20, 40), doneevents(i)) fibarray(i) = f threadpool.queueuserworkitem(addressof f.threadpoolcallbackmar, i) next console.writeline("all calculations started. waiting them complete.") integer = 0 fibonaccicalculations doneevents(i).waitone() dim f fibonacci = fibarray(i) console.writeline("fibonacci({0}) = {1}", f.n, f.fibofn) next console.writeline("all calculations complete.") you must check event know calculation complete.
now, if don't need know if calculation complete, there's no need waitone @ all. , if you're not going wait on event, there's no real need have event, there? although 1 wonders why you're going calculation , not use result.
Comments
Post a Comment