1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-02-05 03:50:07 +00:00

ListenerList: Add a thread safe ListenerList type

This commit is contained in:
Anthony Nicholls 2024-06-03 09:25:30 +01:00
parent 0dfff1454c
commit c4d5ffa7ab
4 changed files with 15 additions and 9 deletions

View file

@ -73,10 +73,7 @@ namespace juce
guaranteed that no more listeners will be called.
By default a ListenerList is not thread safe. If thread-safety is required,
you can provide a thread-safe Array type as the second type parameter e.g.
@code
using ThreadSafeList = ListenerList<MyListenerType, Array<MyListenerType*, CriticalSection>>;
@endcode
use the ThreadSafeListenerList type.
When calling listeners the iteration can be escaped early by using a
"BailOutChecker". A BailOutChecker is a type that has a public member function
@ -87,8 +84,8 @@ namespace juce
@tags{Core}
*/
template <class ListenerClass,
class ArrayType = Array<ListenerClass*>>
template <typename ListenerClass,
typename ArrayType = Array<ListenerClass*>>
class ListenerList
{
public:
@ -402,4 +399,13 @@ private:
JUCE_DECLARE_NON_COPYABLE (ListenerList)
};
//==============================================================================
/**
A thread safe version of the ListenerList class.
@see ListenerList
*/
template <typename ListenerClass>
using ThreadSafeListenerList = ListenerList<ListenerClass, Array<ListenerClass*, CriticalSection>>;
} // namespace juce

View file

@ -602,7 +602,7 @@ private:
uint32 affinityMask = 0;
bool deleteOnThreadEnd = false;
std::atomic<bool> shouldExit { false };
ListenerList<Listener, Array<Listener*, CriticalSection>> listeners;
ThreadSafeListenerList<Listener> listeners;
#if JUCE_ANDROID || JUCE_LINUX || JUCE_BSD
std::atomic<Priority> priority;

View file

@ -147,7 +147,7 @@ private:
String jobName;
ThreadPool* pool = nullptr;
std::atomic<bool> shouldStop { false }, isActive { false }, shouldBeDeleted { false };
ListenerList<Thread::Listener, Array<Thread::Listener*, CriticalSection>> listeners;
ThreadSafeListenerList<Thread::Listener> listeners;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadPoolJob)
};

View file

@ -65,7 +65,7 @@ public:
}
private:
using ListenerListType = ListenerList<Listener, Array<Listener*, CriticalSection>>;
using ListenerListType = ThreadSafeListenerList<Listener>;
// By having a static ListenerList it can outlive the ShutdownDetector instance preventing
// issues for objects trying to remove themselves after the instance has been deleted