Project Description
Concurrent Collection makes it easier for programmers working in a multithreaded environment to manage data, you'll no longer have to worry about locks, and thread safety. It's developed in C#

Project Principles
  • All collections should be accessible by multiple threads, with minimal thought required for thread safety
  • All methods should be threadsafe
  • Collections should be usable on the Compact Framework, this means as few allocations as possible

Design Principles
  • Concurrent namespace
    • Collections in this namespace are truly concurrent, all methods are threadsafe and reentrant. No locking is involved
  • Sharded namespace
    • Collections in this namespace are semi concurrent, each collection internally has several "shards" each shard can be accessed by only one thread.
    • Locking is used to ensure exclusive access to threads, usually spinlocking
  • Spinlocked namespace
    • Collections in this namespace are just pretending to be concurrent, all methods acquire a spinlock on the entire collection.

Transactions
  • Single operations can be performed on the sets with ease. For example adding/removing to a set. However, performing two operations in a row has problems, for example if you check if something is in a set, and then try to use that item (knowing it's in the set) things could go wrong if another thread removes the item after you check but before you use it.

ShardedDictionary<X, Y> dictionary = new ShardedDictionary<X, Y>(10);
if (dictionary.contains(foo))
{
//another thread might remove foo here
dictionary[foo].bar(); //which would cause this to throw a KeyNotFoundException
}

To overcome this problem, collections can run transactions. There are many different kinda of transaction, but in general to use a transaction you tell the collection what keys you wish to operate on, and then give it a method to run. In this way, the above code could lock "foo" to make sure nothing removes it, and then operate on foo, confident that this thread has exclusive to foo.

Blog
  • I have a blog martindevans.appspot.com/blog/latest where I talk about game development and C# development, I'm almost certain to talk about this project on there in some detail soon

Last edited Dec 21, 2009 at 4:55 PM by martindevans, version 10