Multithreading… Why Not?

So this week I finally got around to giving multithreading a try. I’ve been wanting to give it a go for a good while now but never got around to it because it always seemed so daunting.

I can think of a solid handful of projects where it would have helped and I recall thinking about doing it, but not because again, it seemed daunting… If I only I knew how easy it really is.

That’s hindsight for you I guess…

So let me tell about what I did and what I learnt.

This week I worked In an existing raytracing project that does per-pixel calculations of colour, reflection, shadows, etc. This raytracer is not the best and that’s intentional for optimization of it.


My first step in the process what to assess the raytracer and see how it ticks and find areas that can be optimized for improved performance.

This included many areas, from parts that can be multithreaded, implementation that can be better and the like.

After going through all that I was overwhelmed with options on what I could do to optimize this project. But I’m really only doing this to learn the basics of how to optimize using multithreading, Going through and micromanaging everything for minuscule performance increases would be time-consuming and I can do that at a later date when I'm not pressed for time on other various projects.

So what did I do? With my implementation, the program saw a 685% increase in speed, making it a 87% faster at rendering and what I did was so simple, it surprised me.


Being overwhelmed on the possible routes I could take to optimize this, I took the simplest approach for the time. Of which was to just manipulate the for loop over the pixels and thread that using C++’s built-in threading library. This library doesn't give you much control but it lets you get the bare minimum done and easily at that.

All I did was make it thread the loop over the Y pixels and that was that.

Now, there's PLENTY that I want to do to the project and bring the render time down into the milliseconds. This would require stuff like an octree and I hope - if the valuable resource called time permits - I can do that in the future. Seeing as though I’ve only worked with quadtrees, octrees would be first for me and valuable for me to wrap my head around.

Although, for now, that will do. I’ve learnt how to multithread and I can apply what I have learnt in future projects without that previously discussed daunting feeling!