Why hashtable is thread safe




















Thread-safe aka "concurrent" data structures such as hash maps are never fully "concurrent" in the sense that two or more threads have totally concurrent access to any part of them at any time. In practice, this means that threads will be blocked under some circumstances; well-written concurrent structures will employ appropriate strategies to minimize the occurrence of blocking and the delay due to blocking when it does occur.

Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Implementing a hash table with true concurrency Ask Question.

Asked 1 year, 11 months ago. Active 1 year, 11 months ago. Viewed 4k times. Improve this question. Community Bot 1. The only way to implement a hash map with true concurrency is to have an immutable hash map.

Any number of threads can safely read from it that way. What if Ref-count whichever point of entry threads acquire, and when older ones are free you can compact. Basically, a hack for garbage collection and transactional-ish accesses. It's already concurrent, so there are no warranties of consistency lost. Here's a technique for lock-free lookups in an associative table: linkedin.

Updates to the table will block each other, but not lookups. The example code uses an AVL tree, but a hash table could be used instead. I've implemented an intrusive hash table, github. I assume that boost::intrusive has a hash table, may be a better option. Add a comment. Active Oldest Votes.

I have wrestled with this. I did a few iterations on my design First solution : Just have a hash table with a global lock. There is a problem: it does not shrink. Oh, I forgot, to minimize allocations I pool the arrays that make up the nodes of the tree. Improve this answer. Theraot Theraot 8, 2 2 gold badges 23 23 silver badges 34 34 bronze badges.

I think the point was to walk through and figure out what I really knew about threading. He did ask in plain English, "can two of the threads just lock the same mutex at once? I'm curious though how the "copy everything works". Obvoiusly mutexes aren't copyable.

You can afford to only lock to copy because everything else is atomic, on interlocked operations. For growth, I used a monitor yes, a mutex to lock. I didn't copy it However, protecting after the resize? Again, interlocked operations, access to the data is atomic.

Yet, we cannot copy all the data as an atomic operation, and threads should not see a partial copy, thus lock to grow. Thank you.

Quite informative. Going to mark this as a solution as there are a few ways to go about it. Fourth and Fifth are definitely worth a deep dive. You can lock only writes on growth - in the third solution, you keep the old structure available from reads, and when the new copy is done, then you atomically switch the 'readable' pointer to the new structure, unlock, and discard the old structure. Show 3 more comments. That makes it a hell of a lot easier. The general operation you need is compute key, lambda , which looks up the possibly non-existent value associated with the key key , passes it to the callback function lambda and re associates the key with the value returned by lambda or removes the key, if it returns "no value".

Everything else can be implemented in terms of that. Well, almost. You'll probably also want an iterator of some kind. For a practical example, see how Java does it. Imagine we want to add two new key, value pairs into your hashmap : calculating the hash of each key is without race condition.

This would solve the last issue You can exploit this feature and make each bucket of the hashmap a lock-free empty linked-list, at construction, before the hashmap is used concurently. For Example,. It internally wraps all the methods of Map interface with synchronized keyword.

For example, here is the internal put , get and remove method implementation of SynchronizedMap :. Click here to check full code of SynchronizedMap. Similar to HashMap , Synchronized HashMap allows one null key and multiple null values as it just wraps the methods of HashMap with synchronized keyword. Rest of the behavior remains same as original collection.

You can also check SynchronizedSortedMap which is similar data structure to convert thread-unsafe TreeMap and other SortedMap implementation to corresponding thread-safe collection.

Hashtable and SynchronizedMap both acquires lock on entire Map object which provides thread-safety, but not good performance as at a time only one thread can access that Map instance.

Concurrent package. More than one threads can read and write concurrently in ConcurrentHashMap and still it provides thread-safety. Amazing, isn't it? How is it implemented internally?

Well, ConcurrentHashMap divides the Map instance into different segments. And each thread acquires lock on each segment. By default it allows 16 threads to access it simultaneously without any external synchronization i. We can also increase or decrease the default concurrency level while creating ConcurrentHashMap by using below constructor :. Thread acquires a lock on segment in put operation and at a time only one thread can write in that segment. Thread doesn't acquire a lock on segment in get operation and any number of threads can read from the same segment.

If one thread is writing in a segment, can another thread read from that segment? ConcurrentHashMap doesn't allow null keys and null values. We will soon publish our article on the internal working of ConcurrentHashMap. That's all for this topic.

If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable. Arun Singh Raaj wrote: If it is thread-safe object then why it throws ConcurrentModificationException when changes are made while iteration? Forum: Java in General. If Hashtable is thread-safe then why does it throw "ConcurrentModificationException"? Arun Singh Raaj. Optional 'thank-you' note:. If it is thread-safe object then why it throws ConcurrentModificationException when changes are made while iteration?

Paul Clapham. I like



0コメント

  • 1000 / 1000