I've got some (Entity Framework 4) code that looks like this:
class QueueItem{ public bool Processed { get; set; } // ... other fields } class QueueContext: System.Data.Entity.DbContext { public System.Data.Entity.DbSet<QueueItem> Queue { get; set; } public void ProcessItems() { do { var item = Queue.FirstOrDefault(q => !q.Processed); if(item == null) break; ProcessItem(item); item.Processed = true; SaveChanges(); } while(true); } //... } I want to change it to be multithreaded external to the ProcessItems method. In fact, I want multiple processes/appDomains to be running this code simultaneously. How do I keep multiple processes from picking the same item out of the Queue? I could use a system-wide (named) mutex, but I've seen those be terribly slow. I'm looking for some kind of atomic "pull and mark as in progress". I'm using SqlServerCE4 as the data storage.