Skip to main content
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 ...
JanDotNet's user avatar
  • 8,538
19 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 ...
VisualMelon's user avatar
  • 7,431
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 ...
Loki Astari's user avatar
  • 95.3k
13 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. ...
rolfl's user avatar
  • 97.4k
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. ...
dfhwze's user avatar
  • 13.9k
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 ...
Loki Astari's user avatar
  • 95.3k
12 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 ...
Bailey Parker's user avatar
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 ...
Voo's user avatar
  • 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 --> ...
JanDotNet's user avatar
  • 8,538
11 votes
Accepted

Implementation of a Thread Pool in C++

...
user673679's user avatar
  • 11.8k
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 ...
Disappointed's user avatar
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 ...
indi's user avatar
  • 11.4k
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 ...
Edward's user avatar
  • 66.5k
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. ...
CharlesNRice's user avatar
  • 4,408
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 ...
Johnbot's user avatar
  • 2,974
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 <...
L. F.'s user avatar
  • 9,570
8 votes
Accepted

Simple dispatcher implementation

Because the task within the Dispatcher is a long running task, you should use the option TaskCreationOptions.LongRunning. Otherwise the operation blocks a thread-...
JanDotNet's user avatar
  • 8,538
8 votes

A buffer that holds and provides the latest n items

You can make your code much simpler if you use a LinkedList<T> instead of an array. It has also a much better performance when inserting and removing items in ...
t3chb0t's user avatar
  • 44.3k
8 votes
Accepted

Wrapping IMemoryCache with SemaphoreSlim

Sorry this is a bit of a ramble, but a few things jump out at me: 17?!?! I don't have to tell you about magic numbers! This should probably be configurable... ...
VisualMelon's user avatar
  • 7,431
8 votes

Thread-safe, Convenient and Performant Random Number Generator

As far as I'm aware, using ThreadStatic like this is fine, and will give you the guarantees you need. Overall design Your class is doing 2 things: Managing ...
VisualMelon's user avatar
  • 7,431
8 votes
Accepted

A simple thread-safe Deque in C++

It's probably worth accepting a second template argument for an Allocator to be passed through to the std::deque: ...
Toby Speight's user avatar
  • 77.1k
8 votes

C# threading and synchronizing methods

Taking a look at your Coin class, there are a few things that aren't quite idiomatic C#. The most obvious thing is that you're using what looks nearly like Java Doc comments. C# uses XML doc comments ...
RobH's user avatar
  • 17k
8 votes
Accepted

Basic RAII spinlock class

Instead of creating your own std::lock_guard-style class that works on a std::atomic_flag, consider creating a class that wraps ...
G. Sliepen's user avatar
  • 58.3k
8 votes

C++: algorithm that uses fixed-size buffer of data that are produced in stream, faster than the algorithm speed

Separate the data structure from the workers The class CSyncAlgo is doing way too much, making it harder to understand, and making it less flexible. I would start ...
G. Sliepen's user avatar
  • 58.3k
7 votes

A TaskScheduler that always run tasks in a specific thread

I had a similar problem. Here is an easy way to make all your tasks run exclusively on one thread for all tasks. And you can also run the them Concurrently to using ...
user161231's user avatar
7 votes

Portable periodic/one-shot timer thread - follow-up

I think I spotted a possible deadlock. It will occur in case clearTimer or clear are called from within a timer handler (i.e. ...
wetzeld's user avatar
  • 71
7 votes
Accepted

Linked list node-level locking

Congratulations — I think this is the first concurrency-related code I've reviewed on CodeReview in which I have not managed to find any concurrency-related bugs! It looks like a solid implementation ...
Quuxplusone's user avatar
  • 19.4k
7 votes
Accepted

C# asynchronous notification vector

I don't think that screen space is so limited that methods can't be separated by a blank line, and separation makes it slightly easier to see scope. ...
Peter Taylor's user avatar
  • 24.1k
7 votes

Basic RAII spinlock class

I shiver whenever I see a spin lock. BUT they are exceedingly cheap IF there is usually no contention. So if you need to guarantee something is only accessed by one thread and there are very low risk ...
Loki Astari's user avatar
  • 95.3k

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