Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming (general purpose) thread-safe data structures?

I'm looking for a good name to give to data structures that are thread safe / internally synchronized.

The C++ standard uses the term atomic, but atomic has some rather special meaning. Microsoft uses the term Concurrent in their Thread-Safe Collections (or in C++ _concurrent in the Parallel Containers).

What I really would like would be a generic wrapper for (value) types that provides a similar set of operations to what std::atomics do, but with a different name, and some typedefs derived from it. (use case: something like std::atomic for std::string)

Which of the following would you consider useful / not useful and why?

  • SynchronizedThingamajig (or thingamajig_synchronized or synchronized_thingamajig)
  • Concurrent...
  • ThreadSafe...
  • Safe...
  • Parallel...
  • Locked...
  • Mutex ... or Mutexed...
  • Multithreaded...

For the string example I gave, maybe a synchronized_string or a concurrent_string would make most sense, or would that clash with any other connotation?

like image 234
Martin Ba Avatar asked Jun 18 '13 10:06

Martin Ba


People also ask

Which data structure is thread-safe?

A thread-safe class is a class that guarantees the internal state of the class as well as returned values from methods, are correct while invoked concurrently from multiple threads. The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc.

How do you write a thread-safe function?

Thread Safe Code Lets look at the following code snippet : ... ... ... char arr[10]; int index=0; int func(char c) { int i=0; if(index >= sizeof(arr)) { printf("\n No storage\n"); return -1; } arr[index] = c; index++; return index; } ... ... ...

How can you tell if an object is thread-safe?

A method will be thread safe if it uses the synchronized keyword in its declaration. However, even if your setId and getId methods used synchronized keyword, your process of setting the id (if it has not been previously initialized) above is not. .. but even then there is an "it depends" aspect to the question.

What is meant by Threadsafe?

Thread safety is the avoidance of data races—situations in which data are set to either correct or incorrect values, depending upon the order in which multiple threads access and modify the data.


1 Answers

Useful answer from comment:

Both Microsoft PPL and Intel TBB use concurrent_*. My only suggestion is: Don't use parallel when you mean concurrent. (Parallel is one kind of concurrency, but these data structures should work even on a single processor time-multiplexing multiple threads.) You might also look at the monitor pattern.

-- Wandering Logic Jun 18 at 12:02

To which I might add that from the choices I gave, after thinking some more about it, only concurrent and synchronized seem to make sense.

like image 66
Martin Ba Avatar answered Sep 28 '22 11:09

Martin Ba