Sujet : Re: signalling a condvar from inside vs. signalling a condvar von outside
De : Bonita.Montero (at) *nospam* gmail.com (Bonita Montero)
Groupes : comp.lang.c++Date : 18. Apr 2025, 21:05:09
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vtub8i$3o27s$1@raubtier-asyl.eternal-september.org>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
User-Agent : Mozilla Thunderbird
Am 18.04.2025 um 21:42 schrieb Chris M. Thomasson:
On 4/17/2025 10:56 PM, Bonita Montero wrote:
Am 17.04.2025 um 19:51 schrieb Chris M. Thomasson:
>
The only thing I can say is that signalling, especially broadcasting, from the outside is ideal no matter what lib's you are using. ..
>
With broadcasting it also doesn't matter if you broadcast from inside
or outside.
>
We have to agree to disagree? Fair enough?
But there's one interesting fact to learn at last: broadcasting is more
efficient than unicasting. That's while I have a wait -counter with my
thread-queue and depending on how much items I've inserted in one run
I broadcast (N >= waiting) or I multiple times have a unicast. A n-cast
would be nice to have with that.
template<typename Entity>
template<std::forward_iterator ForwardIt>
void thread_queue<Entity>::enqueue( ForwardIt &begin, ForwardIt end )
requires std::convertible_to<std::iter_value_t<ForwardIt>, Entity>
{
using namespace std;
// return if there's nothing to do
if( begin == end ) [[unlikely]]
return;
lock_guard lock( m_mtx );
// throw if there are emergencies
if( m_producerEmergencies.size() ) [[unlikely]]
throw thread_queue_emergency( *this, m_producerEmergencies );
// number of items pushed so far
size_t n = 0;
// notify threads while unrolling
defer notify( [&]
{
// no threads to be awakened ?
if( !m_nWaiting )
// yes: nothing to do
return;
// more items pushed than waiting ?
if( n >= m_nWaiting )
// yes: notify them all (doesn't throw)
m_cv.notify_all();
else
// no: notify n threads
do
m_cv.notify_one();
while( --n );
} );
do [[likely]]
{
// push and increment n
m_queue.emplace_back( move( *begin ) );
++n;
} while( ++begin != end );
}
defer is sth. like experimental::scope_exit.
Usually there are more items inserted as threads listening so that
I could do a broadcast, which is much more efficient.