Wednesday 15 July 2015

multithreading - C# Enqueue Failure -


I have a simple logging mechanism that thread should be safe; it works most of the time, but every now and then me An exception is found on the line, "_logQ.Enqueue (s);" That the queue is not enough for a long time. Looking at the debugger, sometimes there are only hundreds of items, so I can not see the resource. The queue should be expanded as much as possible. If I catch an exception, unlike the exception of the debugger on the exception, then I see the same error. Do not have some thread here? I do not even know how to start debugging.

  Static Zero Process (Code) (Object State) {while {_logQ.Count> 0} {var s = _logQ.Dequeue (); String deir = ""; Type t = Type.GetType ("Mono.Runtime"); If (t! = Null) {dir = "/ var / log"; } Else {dir = @ "c: \ log"; If (directory! Axis (DIR)) Directory.CreateDirectory (dir); } If (directory.Exists (dir)) {File.AppendAllText (Path.Combine (dir, "admin.log"), DateTime.Now.ToString ("hh: mm: ss") + s + Environment.NewLine); }}} Hold (exception) {} Finally {_isProcessingLogQ = false; }} Public static zero log (strings) {if (_logQ == null) _logQ = new line & lt; String & gt; {}; Lock (_logQ) _logQ.Enqueue (s); If (! _isProcessingLogQ) {_isProcessingLogQ = true; ThreadPool.QueueUserWorkItem (ProcessLogQ); }}  

Note that threads are all call logs (strings). ProcessLogQ is private for logger squares.

* Edit I did not say that it is in a .NET 3.5 environment, so I can not use the work or ConcurrentQueue. I am working on the fixes for the current example under the constraints of .NET 3.5.

** Edit * I believe I have a thread-protected version for .NET 3.5 below. I start the logger thread once from a thread when the program starts, so there is only one thread running to log into the file (T is a stable thread):

  Fixed zero ProcessLogQ () {While (true) {try {lock (_logQ); While (_logQ.Count> 0) {var s = _logQ.Dequeue (); String deir = "../../log"; If (directory! Axis (DIR)) Directory.CreateDirectory (dir); If (Directory.Exists (dir)) {File.AppendAllText (Path.Combine (dir, "s3ol.log"), DateTime.Now.ToString ("hh: mm: ss") + s + Environment.NewLine); }}} Hold (Exception Pre) {Console.WriteLine (ex.Message); } {} Thread in the end. Sleep (1000); }} Public static void start () {lock (t) {if (t.}} Private static void multitraglog (string msg) {lock (_logQ) _logQ.Enqueue (msg);}  

Look at the Workable Library. All the hard work has already been done for you. If you are doing this to learn Learn about multithreading, then read each on locking techniques and professionals and the opposition.

In addition, you go If _logQ is out of your lock statement, I can find it in a static field that you are not starting inside a stationary constructor. You should get this blank probe (which should be inside the lock, it is important Code!!), You can ensure thread-protection by making this static text only and by starting it inside a stable constructor.

In addition to this, Are not properly managing them since there is no lock during queue checking, so it can vary on every frequency. You are losing a lock as your deciding item

Excellent resource:


No comments:

Post a Comment