Multithreaded vector

I am working on a C ++ project that uses self-created maps to store data. Maps in this sense are more likely to look like a “geographic” map, therefore an image. There are different topics of reading and writing to it. Map data is stored in the std vector of integer vectors. Its size does not change, but only the content of certain pixels through the getter and setter functions.

My problem is this: Sometimes everything works fine, but more often I get damaged images in the sense that the pixel value changes sign or is completely different from what it should be. Could this be a problem of streaming read / write access to pixels, if so, what should I use instead of standard vectors? I tried using the mutex to ensure that only one thread reads or writes to the vector, however, these read / write operations occur so often that the application becomes too slow if I block the vector with each operation.

+4
source share
3 answers

You will need some kind of lock. So that this does not damage your performance too much, you should try to make the lock volume as small as possible. For example, you can lock individual lines of vectors so that records in different lines do not interfere with each other. Which solution is best for you depends on your access patterns and platform.

+6
source

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.

0
source
  • Use light locks. That is, the windows use "CriticalSection". Or write your own user space locks, for example. as in TinyThread (http://tinythreadpp.bitsnbites.eu/). They are written in ASM and should have nearly zero overhead for actual locking and unlocking.

  • Once you are sure that the locks themselves are fast, if everything is still running slowly, this is because you have a lock conflict. For instance. multiple threads that need to block the same resource. In your use case, consider something like a read / write mutex. This is a class that will have a read mutex and a write mutex. The readLock method on a mutex only blocks a few loops to increase the reference count of the mutex. "readUnlock" decreases the reference count. "writeLock" locks the read mutext and sets a flag that forces readers to block read mutexes. And then it locks the write mutexes and performs the write operation. This way you guarantee that only one write operation can occur at a time, and that no reads can occur during recording. But simultaneous reading is allowed.

0
source

All Articles