Note that there are some explanatory texts on larger screens.

plurals
  1. POEfficient approach to multithreaded set difference
    primarykey
    data
    text
    <p>I have a finite set of consumer threads each consuming a job. Once they process the job, they have a list of subjobs that were listed in the consumed job. I need to add the subjobs from that list that I don't already have in the database. There are 3 million in the database, so getting the list of which ones aren't already in the database is slow. I don't mind each thread blocking on that call, but since I have a race condition (see code) I have to lock them all on that slow call, so they can only call that section one at a time and my program crawls. What can I do to fix this so the threads don't slow down for that call? I tried a queue, but since the threads are pushing out lists of jobs faster than the computer can determine which ones should be added to the database, I end up with a queue that keeps growing and never empties.</p> <p>My code:</p> <pre><code>IEnumerable&lt;string&gt; getUniqueJobNames(IEnumerable&lt;job&gt; subJobs, int setID) { return subJobs.Select(el =&gt; el.name) .Except(db.jobs.Where(el =&gt; el.set_ID==setID).Select(el =&gt; el.name)); } //...consumer thread i lock(lockObj) { var uniqueJobNames = getUniqueJobNames(consumedJob.subJobs, consumerSetID); //if there was a context switch here to some thread i+1 // and that thread found uniqueJobs that also were found in thread i // then there will be multiple copies of the same job added in the database. // So I put this section in a lock to prevent that. saveJobsToDatabase(uniqueJobName, consumerSetID); } //continue consumer thread i... </code></pre>
    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