Simple Solution To Hashtables and Thread Safety

Shortly after I posted the original entry on this, I figured out a much more simple way than using version numbers, etc.

Item item = null;
bool lockWasSuccessful = false;

while(true) {
    lock(hashtable) {
        item = hashtable[key];
        lockWasSuccessful = Monitor.TryEntry(item);
    }

    if(lockWasSuccessful == false) {
        Thread.Sleep(0);
        continue;
    }

// If we reach here, the item was successfully locked

    try {

// Application code goes here

    }
    finally {
        Monitor.Exit(item);
    }

    break;
}

So, the secret here is that I use TryEnter to try to lock the object while the hashtable is locked. I then drop the lock on the hashtable and check to see if the TryEnter worked. If it did not, I just repeat the process. If it did, then I’m free to go do application stuff, remembering to drop the lock when I finish. Tres simple!

Does that solution make sense? It is the simplest one I can think of that satisfies my performance requirements. Much more simple than the version number scheme I was using.

Any comments?

– bab

 

Now playing: Dream Theater – Six Degrees Of Inner Turbulence (Disc 2) – VIII. Losing Time – Grand Finale

This entry was posted in 112, 115. Bookmark the permalink.

6 Responses to Simple Solution To Hashtables and Thread Safety

  1. Frans Bouma says:

    You should lock with a simple object, not a big resource. So do:

    object mutex = new object();

    lock(mutex)…

    This is much more efficient. Also, lock code not resources. This means that you shouldn’t do the very inefficient actions you’re doing now. Simply do:

    Item item = null;

    object mutex = new object;

    lock(mutex)

    {

    item = hashtable[key];

    // Application code goes here

    }

    NO code will pass teh lock on the mutex, unless it’s allowed to, it’s the solely thread accessing the LOGIC manipulating the data. All other threads will wait.

  2. Brian Button says:

    Thanks for the answer, Frans, but I must not have explained my problem correctly. I have to lock the resource and not lock the code because the item can be accessed in any one of a number of methods, and I can’t block when some other item is being accessed. In other words, I want to have individual locks for each item, not locks around blocks of code.

    Thanks for reading, and I appreciate the comment.

    – bab

  3. dsf says:

    http://www.greatestjournal.com/users/aochuang1/504.html ????

    http://www.greatestjournal.com/users/aochuang1/693.html ????

    http://www.greatestjournal.com/users/aochuang1/832.html ????

    http://www.greatestjournal.com/users/aochuang1/1211.html ????

    http://www.greatestjournal.com/users/aochuang1/1393.html ????

    http://www.greatestjournal.com/users/aochuang1/1727.html ????

    http://www.greatestjournal.com/users/aochuang1/1892.html ????

    http://www.greatestjournal.com/users/aochuang1/2178.html ????

    http://www.greatestjournal.com/users/aochuang1/2401.html ????

    http://www.greatestjournal.com/users/aochuang1/2672.html ????

    http://www.greatestjournal.com/users/aochuang1/2867.html ????

    http://www.greatestjournal.com/users/aochuang1/3114.html ????

    http://www.greatestjournal.com/users/aochuang1/3450.html ????

    http://www.greatestjournal.com/users/aochuang1/3638.html ???

    http://www.greatestjournal.com/users/aochuang1/3981.html ????

    http://www.greatestjournal.com/users/aochuang1/4142.html ????

    http://www.greatestjournal.com/users/aochuang1/4533.html ????

    http://www.greatestjournal.com/users/aochuang1/4669.html ????

    http://www.greatestjournal.com/users/aochuang1/5076.html ????

    http://www.greatestjournal.com/users/aochuang1/5219.html ??????

    http://www.blogstudio.com/aochuang1/ac25.htm ??????

    http://www.blogstudio.com/aochuang1/ac01.htm ????

    http://www.blogstudio.com/aochuang1/ac02.htm ???

    http://www.blogstudio.com/aochuang1/ac03.htm ????

    http://www.blogstudio.com/aochuang1/ac04.htm ????

    http://www.blogstudio.com/aochuang1/ac05.htm ????

    http://www.blogstudio.com/aochuang1/ac06.htm ????

    http://www.blogstudio.com/aochuang1/ac07.htm ????

    http://www.blogstudio.com/aochuang1/ac08.htm ????

    http://www.blogstudio.com/aochuang1/ac09.htm ????

    http://www.blogstudio.com/aochuang1/ac10.htm ????

    http://www.blogstudio.com/aochuang1/ac11.htm ????

    http://www.blogstudio.com/aochuang1/ac12.htm ????

    http://www.blogstudio.com/aochuang1/ac13.htm ????

    http://www.blogstudio.com/aochuang1/ac14.htm ????

    http://www.blogstudio.com/aochuang1/ac15.htm ????

    http://www.blogstudio.com/aochuang1/ac16.htm ????

    http://www.blogstudio.com/aochuang1/ac17.htm ????

    http://www.blogstudio.com/aochuang1/ac18.htm ????

    http://www.blogstudio.com/aochuang1/ac19.htm ????

    http://www.blogstudio.com/aochuang1/ac20.htm ????

    http://www.blogstudio.com/aochuang1/ac21.htm ????

    http://www.blogstudio.com/aochuang1/ac22.htm ????

    http://www.blogstudio.com/aochuang1/ac023.htm ????

    http://www.greatestjournal.com/users/uk111/449.html ????

    http://www.greatestjournal.com/users/uk111/729.html ????

    http://www.greatestjournal.com/users/uk111/957.html ????

    http://www.greatestjournal.com/users/uk111/1083.html ????

    http://www.greatestjournal.com/users/uk111/1326.html ????

    http://www.greatestjournal.com/users/uk111/1776.html ????

    http://www.greatestjournal.com/users/uk111/2036.html ????

    http://www.greatestjournal.com/users/uk111/2122.html ????

    http://www.greatestjournal.com/users/uk111/2334.html ????

    http://www.greatestjournal.com/users/uk111/2776.html ????

    http://www.greatestjournal.com/users/uk111/2920.html ????

    http://www.greatestjournal.com/users/uk111/3111.html ????

    http://www.greatestjournal.com/users/uk111/3460.html ????

    http://www.greatestjournal.com/users/uk111/3720.html ????

    http://www.greatestjournal.com/users/uk111/4028.html ????

    http://www.greatestjournal.com/users/uk111/4220.html ????

    http://www.greatestjournal.com/users/uk111/4469.html ???

    http://www.greatestjournal.com/users/uk111/4648.html ??????

    http://www.greatestjournal.com/users/uk111/5040.html ????

  4. biseo says:

    Great solution. Thank you for sharing.

  5. thanks for the solution!

    My hashtable ‘object value’ is a class containing three queues – me head hurts! back to christmas trees in BASIC i say.