Skip to content
How and why to use threading in C++
Profile icon

Have you heard of multithreading and just wish you could make your programs faster?
Well... unfortunately just using threads is not enough, you have to use them correctly.
More threads can make things faster, but too many threads or using them on the wrong things can make it slower!

If you are building a house you undoubtedly have a lot of work to do, but if you have a team you can get it done faster.
If you do it all yourself without any help it will take forever, and it will take just as long or longer if all of you are working on the same thing.
So if everyone gets a different task then multiple things can be done at the same time and by the time they are all finished the house is ready to be put together.

Now this isn't a really great example as I couldn't think of a very good example of a program that benefits from threading off the top of my head but the idea is there and if you made it into a game or something with some more complicated logic like physics it would really make a difference.

I also couldn't find a very significant project to demonstrate thread safety reliably but I'll explain it.
To make that house you need wood or whatever you are making it out of right?
Well if everyone needs a piece of wood and two of them want the same piece what then?
Remember that you have a builder team of idiots so instead of just getting a different piece they both work on the same piece! Each one had a different job so the resulting plank came out an unrecognizable mess!
But if you tell them to take turns, everything works how it should. The second person has to wait for the first one to get their piece.

Basically, the whole benefit of threading is that you can do multiple things at the same time, like web requests for example.
If you don't have anything else to do while your threads are working, you might be better off with just a single thread.

You are viewing a single comment. View All
Profile icon

@CSharpIsGud Ah yes, I seem to have forgotten that your house building examples were given to emphasize that threads aren't meant to all do the same thing.
I forgot that too soon.

But I still feel like that ties more into the last part of your sentence, not the beginning, which is what I am arguing against.

...or using them on the wrong things can make it slower!

Whereas just using more threads won't make anything slower, it's the misuse of threads that can slow you down.

Also, do you have any estimate on how expensive it is to spawn a thread? If it's an expensive operation, then simply spawning the threads could waste more time than it could save.

I was recently thinking about making a multi-threaded compiler.
The program that would, from the very beginning, be split into four separate threads: the lexer, the parser, the optimizer, and the compiler (maybe the compiler and optimizer would be the same thing).
They would work in sequence, yet they would each work independently. The parser would have to wait for the lexer to lex the next token, yet the opposite is not true, the lexer would not have to wait for the parser to parse the token.
I am thinking of streaming the code all the way through, instead of just trying to do everything all at once.

In your opinion, would this be a bad idea or not? Do you believe it would operate slower than the single-threaded version? Do you have any suggestions as to how it should be organized overall?