5

Does it make sense to use std::shared_ptr < std::thread> ? The logic is simple: if thread is not needed, delete it, if new is required - realocate it. is there any way to compare this concept with pooled threads?

I do know the exact ammount of threads in my system(I develop Image processing algorithm, and I wanted to give each child of "algorithm" class an individual thread (maybe to make it private, then no shared_ptr is required), where this algorithm will be running, and idle this private thread if no image was provided. Is it a bad concept?

11
  • 5
    IMHO: don't use a pointer unless you absolutely need to. Commented May 15, 2018 at 14:35
  • Perhaps stackoverflow.com/questions/9127816/… ? Commented May 15, 2018 at 14:37
  • 3
    Shared ownership of a thread makes little sense. Whatever is going to join() it should also own it. Commented May 15, 2018 at 14:38
  • 1
    @tyker detach() is not normally recommended because process termination is brutal and rare and non-reproducible errors might be encountered because they're doing something critical when they're brutally culled. It's a classic bug that doesn't show up in relatively low load free running tests and then fouls in high load production just when you don't want it and have a real job debugging and fixing it. Essentially every detach() that isn't synchronized back is a race condition. We hate those. Commented May 15, 2018 at 16:11
  • 1
    @jameslarge I think I see what you're getting at. The worker could well be some class other than std::thread<> and that does offer options like cleanly evolving into from 1 to multiple threads in a future version. Say V1: processes colour image to grey scale in 1. V2: Cuts it up and does different pieces in parallel. That said one advantage of close coupling may be to achieve 25fps reliably. While separation is a good aim, it's still a matter of which lunch you want to pay for. All non-trivial real-time DIP application will be performance challenged at some point and ultimately bound by it. Commented May 15, 2018 at 16:51

2 Answers 2

12

You probably miss the fact fact std::thread destructor does not terminate the thread. As already mentioned in the comments, if detach or join was not called before, std::thread destructor calls std::terminate. In other words, std::shared_ptr<std::thread> is pretty useless.

A std::thread is a rather low-level object. You may like to have a look at:

Sign up to request clarification or add additional context in comments.

6 Comments

You could provide a custom deleter to join with the thread before closing it. Though I don't think that's a good design in general, the few cases that std::shared_ptr<std::thread> might be useful also seem like the cases where such a deleter could work.
@FrançoisAndrieux Well, calling std::thread::join does not terminate the thread either.
So as far as I understand, it is better to give the private thread for each object inside the system, but not to make it shared? And the best way is to make it pooled, is it? So shall I dynamicly change the number of threads in pool in case I connect one more camera?
@hagor It depends on what you intend to use those threads for. There are multiple patterns of how threads can be used.
@MaximEgorushkin You're right, I misunderstood the context.
|
2

This answer is in the question:

give each child of "algorithm" class an individual thread (maybe to make it private, then no shared_ptr is required)

Only use std::shared_ptr<> where ownership is genuinely shared.

There's usually no harm in idle threads hanging about but on many systems there is an overhead of even a ceiling on thread instances even if many are not running.

If there's a risk of thread proliferation or too much creating and destroying of threads and swapping, introduce a thread-pool and still don't use shared_ptr<> because the pool with own the threads.

9 Comments

I disagree that there is no harm in leaking thread objects. Proper designed should not leak resources.
Thank you, for reply! I do image processing with let's say 25 frames pro second, and if my algorithm is fast enough, my algorithm thread is hanging for lets say (1/50 second) that is a leak of resources. In other case, let's say, my algorithm is a bit slower then fps, so I will need one more thread, which will be taken from the pool. Which is a good design. The question is: shall i somehow change the number of threads inside the pool?
Re, "Only use shared_ptr<> where ownership is genuinely shared." I've worked on projects where we decided that all pointers should be shared_ptr, regardless of whether the ownership actually was shared or not. The reason was, simplicity: Only one kind of pointer semantics to think about.
First answer first. In real-time image processing you should try and avoid changing the pool size after setup. A reason for using a pool is that creating and terminating threads has an overhead such as allocating a stack for it to use. That might show up as lag in your animation. If you allocate too many there'll be lots of task swapping overhead. Allocate too few you're not taking full advantage. use en.cppreference.com/w/cpp/thread/thread/hardware_concurrency to determine what is there. You should probably use less than that as your pool is not the only process. You'll end up tuning.
Second answer, 'only' use shared_ptr isn't unreasonable but general advice is not to. unique_ptr is practically overhead free in execution and shared has an overhead. But worse you can't (easily) take ownership back from shared_ptr but unique_ptr;;release() gives it back. That can be important for inter-operability with say library code that expects ownership. I'd say both are so ubiquitous in modern C++ that mix-and-match shouldn't really confound anyone. In fact 'object x owns the object' is the commonest case and has the simplest semantics....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.