With such high read / write ratios, you should consider a lock-free solution, for example. nbds
EDIT:
In general, blocking algorithms work as follows:
- organize the data structures so that for each function that you intend to support, there is a point at which you can determine in one atomic operation whether its results are real (i.e. other threads do not change their inputs because they were read ) and pass them; no state changes visible to other threads if you do not commit. This will include the use of platform-specific features, such as Win32 kernel file comparison and exchange codes or operational cell cache line reservation codes.
- each supported function becomes a loop that reads inputs many times and tries to do the job until the commit completes successfully.
In cases of very low rivalry, this is a gain over blocking algorithms, since functions generally succeed for the first time without causing overhead to acquire a lock. As competition grows, winnings become more dubious.
As a rule, the amount of data that can be processed by an atom is small - 32 or 64 bits are common - therefore, for functions associated with many reads and writes, the resulting algorithms become complex and potentially very difficult to reason. For this reason, it is preferable to look for and accept mature, well-tested and well-understood third-party solutions that are free from blocking for your problem, rather than riding on your own.
Hashtable implementation details will depend on various aspects of the hash and table design. Do we expect the table to grow? If so, we need a way to copy bulk data from the old table to the new one safely. Are we expecting a hash clash? If so, we need some way of passing the counter data. How do we make sure that another thread does not delete the key / value pair between the search that returns it and the caller using it? Maybe some form of reference? - but who owns the link? - or just copying the value when searching? - but what if the values are large?
Locked stacks are well understood and relatively easy to implement (to remove an element from the stack, get the current vertex, try replacing it with the next pointer until you succeed, return it to add an element, get the current vertex and set it as a pointer to the next element until you manage to write a pointer to the element as a new top; on architectures with reservation / conditional write semantics, this is enough so that on architectures that only support CAS, you need to add There is a nonce or version number for atomic data to avoid the problem of excellent presentation on a real approach that allows growth / collision. Others exist, Google quickly finds a lot of papers.
Blocking free and pending algorithms - exciting areas of research; I encourage the reader to google. However, naive locks, free implementations, can easily look reasonable and behave correctly most of the time, while in reality they are unsafe. Although it's important to adhere to the principles, I highly recommend using an existing, well-understood and proven implementation to turn your own.