Question about multithreading and multiple readers one writer (c++)

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
PeteP
What the peck?
Posts: 1451
Joined: Tue Aug 23, 2011 4:51 pm UTC

Question about multithreading and multiple readers one writer (c++)

Postby PeteP » Fri Sep 23, 2016 4:27 pm UTC

I am doing something with opencv and I want to search in one image for several things and I want to split that into multiple threads, since during that search they change the image (by filtering it for different colors) they need their own copies. And of course I should avoid it changing the image while I make copies. So I would use a mutex but since there are multiple readers and only one writer shared_mutex from boost seems more appropriate. But I haven't worked with threads all that much so I wasn't sure whether that is the best choice and decided to ask here.

User avatar
Robert'); DROP TABLE *;
Posts: 728
Joined: Mon Sep 08, 2008 6:46 pm UTC
Location: in ur fieldz

Re: Question about multithreading and multiple readers one writer (c++)

Postby Robert'); DROP TABLE *; » Fri Sep 23, 2016 5:17 pm UTC

I may have misunderstood the question, but what actually performs the shared write? Do the searchers' changes somehow become visible to the other searchers?
...And that is how we know the Earth to be banana-shaped.

User avatar
PeteP
What the peck?
Posts: 1451
Joined: Tue Aug 23, 2011 4:51 pm UTC

Re: Question about multithreading and multiple readers one writer (c++)

Postby PeteP » Fri Sep 23, 2016 5:49 pm UTC

Ah yes, I am constantly reading new images so that is the writing part.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 5310
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Question about multithreading and multiple readers one writer (c++)

Postby Thesh » Tue Nov 29, 2016 4:37 pm UTC

Can we see a high level of the algorithm? I'm not sure how that would work so I'm not sure what the locking strategy would be. If it's something like this when single threaded:

Get base image
Make copy
Make changes
Push changes to base image

Then you would need a mutex around getting the base image and pushing the changes so no one is reading and writing at the same time. Getting the base image is probably quick, so while you can use a shared mutex (shared for reading, exclusive for writing), the difference between that and a traditional mutex (exclusive locks only) in terms of performance are probably unnoticeable. For slow read operations, you gain an advantage with a shared lock for reading, but for fast operations the shared lock has more overhead and probably ends up being slower overall. I generally suggest using a traditional mutex unless your read operation is a major bottleneck (which I suspect it is nothing compared to image processing).
Honesty replaced by greed, they gave us the reason to fight and bleed
They try to torch our faith and hope, spit at our presence and detest our goals

User avatar
PeteP
What the peck?
Posts: 1451
Joined: Tue Aug 23, 2011 4:51 pm UTC

Re: Question about multithreading and multiple readers one writer (c++)

Postby PeteP » Tue Nov 29, 2016 6:37 pm UTC

Nah it was:

Get base image
Make copy
Make changes in copy to make it easier to detect an object

Pushing changes back to the base image wasn't necessary. But yeah I just used a normal mutex in the end or rather two since I wrote the images from the camera in a ring buffer and got the copies from there. It would probably have worked just fine if I had done it directly but whatever.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests