c# - Why does parallel this code work sometimes? -
I wanted to paragraph a piece of code, but code actually slowed due to the overhead of the barrier and block collection . There will be two threads, where the first piece of work will be found, which will do the other thing. Both operations are not working much, so the overhead of switching safely will quickly overlay the two threads.
So I thought I would try to write some code, which is lean as possible, without barrier etc. Does not behave coherent, but sometimes it works, sometimes it does not happen and I do not know why
This code is used to try to synchronize only two mechanisms The mechanism is there. It does not do anything useful, you just need a minimal code to reproduce the bug.
So here's the code:
// Elements of work in the node class worksheet {public int value; Public Work ITM Next; } Fixed zero testing () {WorkItem fst = null; // First element create action = () = & gt; {Tentative stream = faucet; For (Int i = 0; I <1000; i ++) {Work TM TP = New Worksheet {value = i}; // Create new com class if (fst == faucet) fst = tmp; // If it adds the first time, add it more. Next = tmp; // More behind the list = tmp; // This is the current one; Cur.Next = new task item {value = -1}; // -1 means the stop element #if VERBOSE Console.WriteLine ("has been created"); #end if }; Consumption of action = () = & gt; {// Thread Sleep (1); // It seems to fix this # VERBOSE Console.WriteLine ("Consumption begins"); // Especially think that #endif WorkItem cur = null; Int tot = 0; While (fst == faucet) {} wait for a busy first / busy = fst; #if VERSBOSE console WrightLine ("Consumption previously found"); #endif while breaking (if true) {if (cur.Value == -1); // if the break element breaks; Tot + = cur.Value; While (cur.Next == null) {} // busy waiting to proceed cur = cur.Next; // to proceed) console.light line (total); }; Try {parallel. Invoke (create, consume); } Hold (Collected E) {console. WriteLine (e.Message); Forchcha (var E. Inner Exposure) Console Vitaline (i.e. message); } Console.light line ("consumed .."); Console.ReadKey (); } This is a linked list of functions for this idea. A thread adds items behind that list, and another thread reads them, does something, and setting the next field to see if it is set. As soon as it is set, it will be new and processed as soon as it is set. It chooses the next field in a busy busy loop because it should be set up very quickly, killing the benefit of reflecting code, reference switching, etc. The time it takes to make a work, it will be quite comparable to execute it, so the waste cycle should be quite small.
When I run the code in release mode, sometimes it works, sometimes it does nothing; the problem is in the 'consumer' thread, the 'create' thread always seems to end (You can check with the negligible console. WrightLines). It has always worked in debug mode, adding about 50% hits and miss some console in release.Relance helps in proportion to success, but even then it is not 100%. (# Defined verbose stuff).
When I add threads Sleep in the 'consumer' thread (1) also seems to fix it but due to not being able to reproduce the bug it is not certain that it is certain.
Is there any clue here that what is wrong here? Is it some customization that makes a local copy or some update that does not get updated? something like that?
There is no such thing as partial update right? Like a door, but then he writes a thread half dan and reads partially written memory of the other thread? Just looking ..
I think it should just work .. I think that each time threads come in different order and it fails, but how do I Can not get it and how can I fix it without slowing it down?
Thanks in advance for any suggestions,
Gert-Jan
See also
Comments
Post a Comment