monitor - wait and pulse Exception in C# -
i have simulation druring time. in every steps have 4 main function f1,f2,f3,f4. f1 must run before , f2 , f3 can run after f1 parallel. when f2 , f3 finished can start f4. want create 4 task these. don't want create task in every step.(because has overhead) want after 1 task , wait next step. write code give exception.
task taskf1, taskf2, taskf3, taskf4; bool lockf1,lockf2, lockf3, lockf4_1,lockf4_2; random r; public void f() { r = new random(); taskf1 = task.factory.startnew(f1); taskf2 = task.factory.startnew(f2); taskf3 = task.factory.startnew(f3); taskf4 = task.factory.startnew(f4); monitor.pulse(lockf1); } private void f1() { while (true) { monitor.wait(lockf1); console.writeline("task 1"); thread.sleep((int)(r.nextdouble() * 1000.0)); monitor.pulse(lockf2); monitor.pulse(lockf3); } } private void f2() { while (true) { monitor.wait(lockf2); console.writeline("task 2"); thread.sleep((int)(r.nextdouble() * 1000.0)); monitor.pulse(lockf4_1); } } private void f3() { while (true) { monitor.wait(lockf3); console.writeline("task 3"); thread.sleep((int)(r.nextdouble() * 1000.0)); monitor.pulse(lockf4_2); } } private void f4() { while (true) { monitor.wait(lockf4_1); monitor.wait(lockf4_2); console.writeline("task 4"); thread.sleep((int)(r.nextdouble() * 1000.0)); monitor.pulse(lockf1); } }
i give exception in 1 of monitor.wait(lockfn)
system.threading.synchronizationlockexception unhandled user code hresult=-2146233064 message=object synchronization method called unsynchronized block of code. source=mscorlib stacktrace: @ system.threading.monitor.objwait(boolean exitcontext, int32 millisecondstimeout, object obj) @ system.threading.monitor.wait(object obj, int32 millisecondstimeout, boolean exitcontext) @ system.threading.monitor.wait(object obj) @ parallel_test.program.f1() in c:\users\sharafi\documents\visual studio 2013\projects\parallel test\parallel test\program.cs:line 191 @ system.threading.tasks.task.innerinvoke() @ system.threading.tasks.task.execute() innerexception:
isn't trying achieve:
var firsttask = new task(f1); var secondtask = firsttask.continuewith(ignored => f2()); var thirdtask = firsttask.continuewith(ignored => f3()); var fourthtask = task.whenall(secondtask, thirdtask).continuewith(ignored => f4()); firsttask.start();
Comments
Post a Comment