File: thread_group.cc

package info (click to toggle)
gnuradio 3.7.13.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 45,992 kB
  • sloc: cpp: 155,723; python: 88,934; xml: 42,165; ansic: 41,036; fortran: 927; asm: 803; sh: 224; lisp: 31; makefile: 17
file content (98 lines) | stat: -rw-r--r-- 3,145 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* -*- c++ -*- */
/*
 * Copyright (C) 2001-2003 William E. Kempf
 * Copyright (C) 2007 Anthony Williams
 * Copyright 2008 Free Software Foundation, Inc.
 *
 *  Distributed under the Boost Software License, Version 1.0. (See accompanying
 *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 */

/*
 * This was extracted from Boost 1.35.0 and fixed.
 */

#include <gnuradio/thread/thread_group.h>

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);
      }
    }

    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();
    }

    void thread_group::add_thread(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);
      BOOST_ASSERT(it == m_threads.end());
      if(it == m_threads.end())
        m_threads.push_back(thrd);
    }

    void thread_group::remove_thread(boost::thread* thrd)
    {
      boost::lock_guard<boost::shared_mutex> guard(m_mutex);

      // 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);
      BOOST_ASSERT(it != m_threads.end());
      if(it != m_threads.end())
        m_threads.erase(it);
    }

    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();
      }
    }

    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();
      }
    }

    size_t thread_group::size() const
    {
      boost::shared_lock<boost::shared_mutex> guard(m_mutex);
      return m_threads.size();
    }

  } /* namespace thread */
} /* namespace gr */