Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this thread safe? Breakpoints get hit multiple times
    primarykey
    data
    text
    <p>I have the following code:</p> <pre><code>public class EmailJobQueue { private EmailJobQueue() { } private static readonly object JobsLocker = new object(); private static readonly Queue&lt;EmailJob&gt; Jobs = new Queue&lt;EmailJob&gt;(); private static readonly object ErroredIdsLocker = new object(); private static readonly List&lt;long&gt; ErroredIds = new List&lt;long&gt;(); public static EmailJob GetNextJob() { lock (JobsLocker) { lock (ErroredIdsLocker) { // If there are no jobs or they have all errored then get some new ones - if jobs have previously been skipped then this will re get them if (!Jobs.Any() || Jobs.All(j =&gt; ErroredIds.Contains(j.Id))) { var db = new DBDataContext(); foreach (var emailJob in db.Emailing_SelectSend(1)) { // Dont re add jobs that exist if (Jobs.All(j =&gt; j.Id != emailJob.Id) &amp;&amp; !ErroredIds.Contains(emailJob.Id)) { Jobs.Enqueue(new EmailJob(emailJob)); } } } while (Jobs.Any()) { var curJob = Jobs.Dequeue(); // Check the job has not previously errored - if they all have then eventually we will exit the loop if (!ErroredIds.Contains(curJob.Id)) return curJob; } return null; } } } public static void ReInsertErrored(long id) { lock (ErroredIdsLocker) { ErroredIds.Add(id); } } } </code></pre> <p>I then start 10 threads which do this:</p> <pre><code>var email = EmailJobQueue.GetNextJob(); if (email != null) { // Breakpoint here } </code></pre> <p>The thing is that if I put a breakpoint where the comment is and add one item to the queue then the breakpoint gets hit multiple times. Is this an issue with my code or a peculiarity with VS debugger?</p> <p>Thanks,</p> <p>Joe</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload