multithreading
 All Classes Functions Variables Pages
garbage_collected_object.h
1 
2 #ifndef GARBAGECOLLECTOR_H_INCLUDED
3 #define GARBAGECOLLECTOR_H_INCLUDED
4 
25  Timer
26 {
27 public:
28  juce_DeclareSingleton (GarbageCollector, false)
29  GarbageCollector (int timerInterval = 150)
30  {
31  startTimer (timerInterval);
32  }
34  {
35  clearSingletonInstance();
36  /* If objects in our stack own other objects we might
37  need to do a lot of clearing up. */
38  int count = 100;
39 
40  while (data.size() > 0 && count--) timerCallback();
41 
42  jassert (data.size() == 0);
43  }
46  bool isInList (ReferenceCountedObject* o)
47  {
48  return (std::find (data.begin(), data.end(), o) != data.end());
49  }
50 private:
51  static void add (ReferenceCountedObject* o)
52  {
53  getInstance()->addObject (o);
54  }
55  void addObject (ReferenceCountedObject* o)
56  {
57  jassert (! isInList (o)); /* duplicate. */
58  o->incReferenceCount();
59  data.push_back (o);
60  }
61 
62  void timerCallback()
63  {
64  data.remove_if ([] (ReferenceCountedObject * o)
65  {
66  if (o->getReferenceCount() == 1)
67  {
68  o->decReferenceCount(); /* Will delete it too! */
69  return true;
70  }
71 
72  return false;
73  });
74  }
75  std::list<ReferenceCountedObject*> data;
76  friend class GarbageCollectedObject;
77 };
78 
140  public ReferenceCountedObject
141 {
142 public:
144  {
145  GarbageCollector::add (this);
146  /* If you create the object on something other than the message thread
147  * there's a possible race condition.
148 
149  TODO: Fix this ... */
150  jassert (MessageManager::getInstance()->isThisTheMessageThread());
151  }
153  {
154  jassert (MessageManager::getInstance()->isThisTheMessageThread());
155  }
156 };
157 
158 
159 
160 
161 #endif // SAMPLE_DATABASE_H_INCLUDED
Definition: garbage_collected_object.h:24
Allows objects to be created and destroyed on a non-critical thread but passed over to a critical thr...
Definition: garbage_collected_object.h:139