24 votes

AsyncDictionary - Can you break thread safety?

The reason why there is no async API for a dictionary is, that all operations on a dictionary are so fast, that there is no need for asynchronicity. For concurrent scenarios there is the thread safe ...
  • 8,508
20 votes
Accepted

Creating a thread-safe list using a Lock object

Your new approach is much better than the previous one. Instead of deriving from List and using new to hide the base class ...
  • 20.1k
17 votes
Accepted

Messed up Elevator Management System

There's also a generalization problem. I agree! Whenever you start writing things like: ...
  • 13.8k
17 votes

Concurrent HashSet

General Stuff Would be nice to have inline documentation (///) on classes and public members, but everything is pretty simple and understandable. Some might ...
  • 7,116
16 votes
Accepted

Recursive shared_mutex implementation

Easy Stuff you already know. Probably part of your automated scripts to build new files. #pragma once But for new comers I would point out the more standard ...
  • 92.1k
16 votes
Accepted

Locking mechanism in C#

First of all, your locking seems ok, I don't expect any drawbacks by using multiple threads. That being said, let us review this from bottom to top. ...
  • 50k
13 votes
Accepted

Thread pool worker implementation

Please note that the code should be presented in the same way that you use it to avoid spurious errors. Compiling your code as is, gave me an error on the specialization of ...
13 votes
Accepted

Generic cached value class mimicking Lazy<T>

First off, never lock on this. You never know who else is locking on that instance. Conversely, clients don't know that the cache is locking on itself either. For ...
  • 1,167
13 votes
Accepted

Implementing an asynchronous mutex in C#

A better alternative to the busy wait loop would be to use a Semaphore - in this case a SemaphoreSlim would probably be the most appropriate. Semaphores are not ...
  • 20.1k
13 votes

AsyncDictionary - Can you break thread safety?

Threading Design Your implementation has a very intrusive lock for all read and write operations, using the SemaphoreSlim with max concurrency 1. ...
  • 13.8k
12 votes
Accepted

Dining philosophers problem in C++11

struct Fork { std::mutex mux; }; std::vector<Fork*> forks; I think the problem you want to solve by using pointers here is that a ...
  • 1,109
12 votes

Thread safe logging class in C# to use from DLL

I don't think Log should be public, it is confusing. Me, as a client of your logging API, shouldn't need to create an instance of ...
  • 12.3k
12 votes

Simple generic double buffer pattern

Please correct me if I am wrong, but isn't such scenario possible with accepted answer? ...
12 votes
Accepted

Dining Philosopher correctness

Dinning Philosophers How should I improve my solution for Dining Philosopher's problem? Well first implementing the Dining Philosophers problem would be good. This is an interesting problem you ...
  • 92.1k
12 votes
Accepted

Synchronized implementation of a bank account in Java

In terms of the basic thread locking, it looks like it is doing the right thing, but there are a number of issues in how you are calculating the account balance, and also some escaped locking as well. ...
  • 96.8k
12 votes

AsyncDictionary - Can you break thread safety?

It's pretty hard to break something that uses a global lock around everything. So this seems pretty thread-safe. But that doesn't answer the question of why you'd want to use this. Asynchronous ...
  • 525
11 votes
Accepted

A buffer that holds and provides the latest n items

Naming There is one 'u' in ConcuurentCircularBuffer that wants to be a 'r' --> ConcurrentCircularBuffer There is no need to abbreviate name like size (sz --> ...
  • 8,508
11 votes
Accepted

A thread-safe priority queue keeping its elements unique

Let's start with the good: Ohh recent Python 3, nice! Good documentation Good use of typing. This is great for datastructures. Good formatting of your code. Nice ...
11 votes
Accepted

Implementation of a Thread Pool in C++

...
10 votes
Accepted

Running Code Just Once

There are a few items to make this code better. Naming. Once is an OK name for the class, but the method name IsDone is a problem. This is an 'atomic' operation ...
  • 96.8k
10 votes

Dining philosophers problem in C++11

I have only a few comments: The name you gave to sleep_for_random_time() is ambiguous. Some might interpret that the function sleeps for a random amount of time, ...
  • 17k
10 votes

A buffer that holds and provides the latest n items

In addition to @JanDotNet's answer: You can use "less strict" lock in your code, specifically ReaderWriterLock or ...
10 votes
Accepted

Graceful thread shutdown with std::move

volatile bool thread_shutdown = false; This is definitely not correct. volatile is not for thread safety. What you want here ...
  • 9,036
9 votes

Thread pool worker implementation

Let me start by saying that this is a nicely written class with good documentation. I also think that Nobody's answer provides some good advice, so mostly I'll build on that. I only have one ...
  • 3,261
9 votes
Accepted

Concurrent LRU cache using sychronizedMap() or ReadWriteLock

My experience with variants on the java.util.concurrent.locks.* classes suggests that their sweet spot in terms of performance is when the work to be done is ...
  • 96.8k
9 votes
Accepted

Writing a thread-safe queue in C++

I see some things that may help you improve your code. List all required #includes The code needs the following #includes to ...
  • 64.6k
9 votes
Accepted

Caching Color-Bitmaps as MemoryStreams

First I'm not a fan of recycling IDs. Too much confusion can happen. For example Thread 1 adds a Dog Image and gets ID 1. Thread 2 calls ReleaseAllImages and then adds a Cat image and gets ID 1. ...
  • 4,168
9 votes
Accepted

AsyncDictionary - Can you break thread safety?

If you modify the AsyncDictionary while enumerating its keys/values it throws InvalidOperationException (if the backing ...
  • 2,909
9 votes

Implementation of a Thread Pool in C++

Welcome to Code Review! Here's some suggestions: Sort the #include directives in alphabetical order. <sstream> and <...
  • 9,249
8 votes

Dining philosophers problem in C++11

Does it work. Short answer: No Long answer: You have avoided deadlock by limiting the number of people allowed to eat. But when the philosophers are not eating they are supposed to be thinking (here ...
  • 92.1k

Only top scored, non community-wiki answers of a minimum length are eligible