I was given this piece of code to analyze and point out what’s wrong with it:
Interview question, part 2
Another interview question I was asked in the past:
Given only a stack, implement a queue.
Interview question, part 1
When interviewing for a job in the past, I was asked the following question:
Write a multithreaded program that prints a sequence of characters like this: A, B, A, B, A, B… Synchronize properly so it never prints the same character more than once in a row.
Template concepts, sort of
Event objects
No, not the type where you subscribe to an event using an interface or a callback. The type where you wait on an event to be signaled, just like
Class mechanics
A friend asked me to make a post about the mechanics of virtual functions in C++. I thought about it for a few days and decided to write a broader post about several topics
Refactoring std::auto_ptr
A colleague at work was recently tasked with refactoring some legacy code and came across the good old std::auto_ptr. In C++0x it has been deprecated (and later removed outright; I can’t even compile std::auto_ptr
Fast semaphore
I received an interesting piece of code during my recent discussion about the
Better blocking queue
In the previous post we discussed a multi-threaded blocking queue who’s implementation was lacking: it was not exception safe. It left the semaphore
Blocking queue
Where produce-consumer pattern is present it is often the case that one is faster that the other: a parsing producer reads records faster than a processing consumer; a disk reading producer is faster than network sending consumer.Producer and consumer often communicate by queues: the producer will put items on a queue while the consumer will pop items off a queue. What happens when the queue becomes full, or empty?
RAII
I know the topic of RAII has been blogged about plenty of times before. Still, I want to present to you my take on it 🙂 Recently I created a policy-based generic RAII template for holding various types of resources (pointers, file handles, mutexes, etc). The nice thing about
L1 cache lines
Herb Sutter gave an interesting talk titled Machine Architecture: Things Your Programming Language Never Told You:
Sorting and locality
Let’s look at two standard data structures: std::vector and std::list and what happens to the performance of traveling them sequentially once they are sorted.
AoS vs SoA performance
There is an interesting set of articles at Fluent{C++} discussing AoS vs SoA.I decided to do my own performance test: to iterate and accumulate data over an array of 10,000,000 structures that describe a person:
What happens when we “new”
Let us take a look at what the C++ compiler generates for us when we use the keywords new, new[], delete, and delete[](I will skip over std::nothrow versions in this post).
enum to string, take 2
Thanks to Sebastian Mestre (who commented on my previous post) I learned something new today 🙂 The X macro makes the enum to string code much… well, perhaps not cleaner, but shorter 😉 It also solves the problem of having to update the code in 2 places: 1st in the enum itself, 2nd in the enum to string map.Without further ado I present to you the X macro:
enum to string
A fellow engineer at work asked me today how to convert an enum to a string value. This is what I came up with:
shrink_to_fit that doesn’t suck
Lesson learned the hard way: trust the implementation to do the right thing! Everything else has been strikethrough’ed because my approach was flowed from the beginning. See the quoted comments at the bottom of this post.