When you are doing multi-threading, try to isolate the thread area as much as possible.
Consider an example. Imagine you have a wall and you want it to be painted in black and white stripes. And to speed up the work, you decide to hire two employees.
Now you can select this task in two ways. 1. Assign black stripes to one worker and white to another worker. 2. Divide the wall into two sections and assign the left section to one employee and the right to the second employee.
Now which one is likely to give the best performance?
In the general case, the 2nd one is better, since the working area is well debugged, and there is no need to wait until another one does its job. Of course, the 1st approach is not mistaken, but it is possible that one worker draws in some place, and the second guy can also reach the same place and will have to wait for the completion of the first. Now imagine what would happen if you had 10 workers, each of whom paints only one particular color.
Multithreaded programming is similar. If you can better break down these issues, you can work with threads efficiently.
source share