diff options
author | Thomas Habets <thomas@habets.se> | 2020-04-10 17:40:11 +0100 |
---|---|---|
committer | Marcus Müller <marcus@hostalia.de> | 2020-04-11 01:41:39 +0200 |
commit | b5e8a552c09a1b9a1397e731cc6f54d427df9a67 (patch) | |
tree | dc60d92c7f53d5d15cbf42d2c5a2623a557eb7ca /gnuradio-runtime/lib/thread | |
parent | 616879745ce5d61e6acd54ad84d60359a739a27d (diff) |
runtime: Remove most manual memory management
The remaining ones:
* `pmt_pool.cc`, which is a memory allocator so that makes sense
* the tricky and aptly named `sptr_magic.cc`.
Diffstat (limited to 'gnuradio-runtime/lib/thread')
-rw-r--r-- | gnuradio-runtime/lib/thread/thread_group.cc | 47 |
1 files changed, 17 insertions, 30 deletions
diff --git a/gnuradio-runtime/lib/thread/thread_group.cc b/gnuradio-runtime/lib/thread/thread_group.cc index 1d9b8c846c..6c7512ab28 100644 --- a/gnuradio-runtime/lib/thread/thread_group.cc +++ b/gnuradio-runtime/lib/thread/thread_group.cc @@ -13,46 +13,36 @@ */ #include <gnuradio/thread/thread_group.h> +#include <boost/make_unique.hpp> namespace gr { namespace thread { thread_group::thread_group() {} -thread_group::~thread_group() -{ - // We shouldn't have to scoped_lock here, since referencing this - // object from another thread while we're deleting it in the - // current thread is going to lead to undefined behavior any - // way. - for (std::list<boost::thread*>::iterator it = m_threads.begin(); - it != m_threads.end(); - ++it) { - delete (*it); - } -} +thread_group::~thread_group() {} boost::thread* thread_group::create_thread(const boost::function0<void>& threadfunc) { // No scoped_lock required here since the only "shared data" that's // modified here occurs inside add_thread which does scoped_lock. - std::unique_ptr<boost::thread> thrd(new boost::thread(threadfunc)); - add_thread(thrd.get()); - return thrd.release(); + auto thrd = boost::make_unique<boost::thread>(threadfunc); + auto thrdp = thrd.get(); + add_thread(std::move(thrd)); + return thrdp; } -void thread_group::add_thread(boost::thread* thrd) +void thread_group::add_thread(std::unique_ptr<boost::thread> thrd) { boost::lock_guard<boost::shared_mutex> guard(m_mutex); // For now we'll simply ignore requests to add a thread object // multiple times. Should we consider this an error and either // throw or return an error value? - std::list<boost::thread*>::iterator it = - std::find(m_threads.begin(), m_threads.end(), thrd); + auto it = std::find(m_threads.begin(), m_threads.end(), thrd); BOOST_ASSERT(it == m_threads.end()); if (it == m_threads.end()) - m_threads.push_back(thrd); + m_threads.push_back(std::move(thrd)); } void thread_group::remove_thread(boost::thread* thrd) @@ -62,8 +52,10 @@ void thread_group::remove_thread(boost::thread* thrd) // For now we'll simply ignore requests to remove a thread // object that's not in the group. Should we consider this an // error and either throw or return an error value? - std::list<boost::thread*>::iterator it = - std::find(m_threads.begin(), m_threads.end(), thrd); + auto it = std::find_if( + m_threads.begin(), + m_threads.end(), + [&thrd](std::unique_ptr<boost::thread>& it) -> bool { return thrd == it.get(); }); BOOST_ASSERT(it != m_threads.end()); if (it != m_threads.end()) m_threads.erase(it); @@ -72,21 +64,16 @@ void thread_group::remove_thread(boost::thread* thrd) void thread_group::join_all() { boost::shared_lock<boost::shared_mutex> guard(m_mutex); - for (std::list<boost::thread*>::iterator it = m_threads.begin(); - it != m_threads.end(); - ++it) { - (*it)->join(); + for (auto& thrd : m_threads) { + thrd->join(); } } void thread_group::interrupt_all() { boost::shared_lock<boost::shared_mutex> guard(m_mutex); - for (std::list<boost::thread*>::iterator it = m_threads.begin(), - end = m_threads.end(); - it != end; - ++it) { - (*it)->interrupt(); + for (auto& thrd : m_threads) { + thrd->interrupt(); } } |