3

If a have a hash shared between 2 threads and if I ensure that thread1 just interacts with key1 and thread2 just with key2, can I assume it as thread-safe? If so, do I need to create key1 and key2 before share the hash over the treads or can each thread create its own key? Is there any place where I can get some information about Perl hashes internal mechanisms and its behavior with threads?

2 Answers 2

2

A hash is an array of linked lists. A hashing function converts the key into a number which is used as the index of the array element ("bucket") into which to store the value. More than one key can hash to the same index ("collision"), so the linked lists handle this case.

If a thread modifies one of the linked lists (e.g. to add an element) while another is navigating it (e.g. to fetch an element), It could lead to problems.

As such, adding elements is not safe. You could address this by precreating the elements of the hash (or array).

So that leaves the question of whether accessing existing elements is safe or not. It might be, but there is no guarantee of that.

You might find these interesting:

  • illguts goes into internal details of Perl data structures.
  • Devel::Peek is a very useful tool.
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, as long as different threads don't step over each other's keys, you're fine. I use a similar idea with arrays (eg having each processing thread record how many items it has processed so that a reporter thread can add up the entries from the array every second or so and report the result).

2 Comments

Internal structure of hashes should be a little more complex than arrays. If you ensure at the begin a fixed size for each array I wouldn't expect problems with individual manipulation of each index, but if two or more threads need to expend the array to fit a new size at "the exact same time", does Perl take care of this thread safety question?
Yes, I have a pre-specified number of threads ($num_threads), and--before starting my threads up--fill a shared array of length $num_threads with zeroes. Then, I pass an argument to each processing thread that is a (distinct) number from 0 to $num_threads - 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.