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.
It might be better to use an actual message queue like MSMQ, Azure Queues, or even Sql Server Service Broker (wouldn’t work with CE, of course). Something like Redis might be easier too. If none of those are an option, you’ll probably have to do something like the following: