# Tag Info

8

Just up front, your code doesn't have any blatant bugs which would prevent its use. There are a few things that you could do to improve it though: One thing that I'd try to avoid is singletons. These are effectively globals and bring with them almost all their problems. In particular, it prevents isolating code for unit tests. The alternative is that you ...

8

It's probably worth accepting a second template argument for an Allocator to be passed through to the std::deque: template<class T, class Allocator = std::allocator<T>> class ThreadSafeDeque { std::deque<T, Allocator> deque; }; Technically, we do need to include <condition_variable>; we're not allowed to assume that <mutex>...

5

Redundant summing // The hand vector contains the maximum number of cards // We can now compute its value That is not completely true, the value could have been built up incrementally each time a card was added to the hand, which would remove some duplicated work: hands that share a common prefix would not each recompute the sum of the values of ...

5

This code is broken. Imagine an Account with $100, two threads, one depositing$1, the other withdrawing $50. Thread one acquired lock1, and begins executing: this.balance = getBalance() + amount; and reads$100 ... Thread two acquired lock2, reads the balance (still $100), subtracts$50, stores the new balance (\$50) and releases lock2. ... (back to ...

4

Is it worth parallelising? When you are parallelizing code you have to ask yourself if it's worth doing that. Parallel code doesn't magically give a speedup, because there are various things that can actually slow you down when using parallelism, for example: Spawning and waiting for threads itself costs some time. Threads might be fighting for resources, ...

4

Invoke is a blocking call that returns only after that call has competed. That means your loop is also including the time it takes to marshal over to the GUI thread and complete. You probably don't want that. I would use BeginInvoke instead, which does not wait for the method to complete on the GUI thread. This is also the difference between ...

4

Here's a better algorithm: It has been benchmarked and reduces the time from 29 hours to 1.1 seconds This is approximately 95,000 times faster. Edit: Faster version, described below, reduces the execution time to 0.68 seconds, which is 153,500 times faster. We only need to consider the number of each type of honor and then combinations of remaining spot ...

4

how can I improve the performance of my program? A standard answer is use a better algorithm. Trying to improve performance by shaving cycles while enumerating a 635 013 559 600 strong set is futile. Consider instead enumerating subsets of valuable cards. There are merely $2^{16} = 65536$ of them; a trillion time acceleration. Given a popcount function, ...

3

Design The design is OK; it seems to use data classes and some classes that perform operations. In true OO fashion, I would expect the player to send the message. For instance, you could do Player.sendMessageTo(int otherPlayerID, String message). You could initialize the player with a MessageHandler that does the actual sending of the message. The player ...

3

Other answers cover a lot but I also would suggest replacing while(deque.empty()) { condition.wait(lock); // unlocks, sleeps and relocks when woken up } with condition.wait(lock, [this]{return !deque.empty();}); because it means the same and is more compact (readability being the same or also better).

3

Your singleton initialization pattern is unsafe for a logger that you may want to use from other static initializers (that have undefined initialization order) because you cannot guarantee that the initialize function has been executed. Typically you would use C++11 magic statics for this, but then you can't give a filename to it. Providing a filename as ...

2

It seems your code will work expected. I am sure that there are other improvements for multithreading but I will just suggest some improvements in terms of readability and usability. I hesitated to share but maybe it could be beneficial for you or others. Firstly, intuitive expectation of most of developers is using operator << to stream out things. ...

1

Yes, your approach is correct. However, you parallelize various queries, which probably go to a remote backing database. As you told us nothing about the runtime of these queries, the nature of the database, the complexity of the underlying statements, I can only ask you to measure. Parallelism induces overhead - in the runtime AND in program complexity. ...

1

Logging after an infinite loop The only way that this: while True: time.sleep(1) message = random.randint(1, 101) logging.info("Producer_1 got message: %s", message) queue_1.put(message) event_1.set() is going to terminate is on an exception. That in itself isn't a bad thing - stop iteration exceptions are a common pattern in Python. ...

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