@@ -190,7 +190,7 @@ class WorkerThread
190
190
{
191
191
public:
192
192
ThreadPool& thread_pool;
193
- unsigned id;
193
+ const unsigned id;
194
194
pthread_t posix_thread;
195
195
bool is_created;
196
196
@@ -418,14 +418,15 @@ void WorkerThread::thread_body()
418
418
stat.threadWake = getTickCount ();
419
419
#endif
420
420
421
+ CV_LOG_VERBOSE (NULL , 5 , " Thread: checking for new job" );
422
+ if (CV_WORKER_ACTIVE_WAIT_THREADS_LIMIT == 0 )
423
+ allow_active_wait = true ;
424
+ Ptr<ParallelJob> j_ptr; swap (j_ptr, job);
425
+ has_wake_signal = false ; // TODO .store(false, std::memory_order_release)
426
+ pthread_mutex_unlock (&mutex);
427
+
421
428
if (!stop_thread)
422
429
{
423
- CV_LOG_VERBOSE (NULL , 5 , " Thread: checking for new job" );
424
- if (CV_WORKER_ACTIVE_WAIT_THREADS_LIMIT == 0 )
425
- allow_active_wait = true ;
426
- Ptr<ParallelJob> j_ptr; swap (j_ptr, job);
427
- has_wake_signal = false ;
428
- pthread_mutex_unlock (&mutex);
429
430
ParallelJob* j = j_ptr;
430
431
if (j)
431
432
{
@@ -480,10 +481,6 @@ void WorkerThread::thread_body()
480
481
}
481
482
}
482
483
}
483
- else
484
- {
485
- pthread_mutex_unlock (&mutex);
486
- }
487
484
#ifdef CV_PROFILE_THREADS
488
485
stat.threadFree = getTickCount ();
489
486
stat.keepActive = allow_active_wait;
@@ -595,40 +592,42 @@ void ThreadPool::run(const Range& range, const ParallelLoopBody& body, double ns
595
592
CV_LOG_VERBOSE (NULL , 5 , " MainThread: wake worker threads..." );
596
593
for (size_t i = 0 ; i < threads.size (); ++i)
597
594
{
595
+ WorkerThread& thread = *(threads[i].get ());
596
+ if (
598
597
#if !defined(CV_USE_GLOBAL_WORKERS_COND_VAR)
599
- bool isActive = threads[i]->isActive ;
600
- if (isActive || threads[i]->has_wake_signal )
601
- #else
602
- if (threads[i]->has_wake_signal )
598
+ thread.isActive ||
603
599
#endif
600
+ thread.has_wake_signal
601
+ || !thread.job .empty () // #10881
602
+ )
604
603
{
605
- pthread_mutex_lock (&threads[i]-> mutex );
606
- threads[i]-> job = job;
604
+ pthread_mutex_lock (&thread. mutex );
605
+ thread. job = job;
607
606
#if !defined(CV_USE_GLOBAL_WORKERS_COND_VAR)
608
- isActive = threads[i]-> isActive ;
607
+ bool isActive = thread. isActive ;
609
608
#endif
610
- threads[i]-> has_wake_signal = true ;
609
+ thread. has_wake_signal = true ;
611
610
#ifdef CV_PROFILE_THREADS
612
611
threads_stat[i + 1 ].reset ();
613
612
#endif
614
- pthread_mutex_unlock (&threads[i]-> mutex );
613
+ pthread_mutex_unlock (&thread. mutex );
615
614
#if !defined(CV_USE_GLOBAL_WORKERS_COND_VAR)
616
615
if (!isActive)
617
616
{
618
- pthread_cond_broadcast/* pthread_cond_signal*/ (&threads[i]-> cond_thread_wake ); // wake thread
617
+ pthread_cond_broadcast/* pthread_cond_signal*/ (&thread. cond_thread_wake ); // wake thread
619
618
}
620
619
#endif
621
620
}
622
621
else
623
622
{
624
- CV_Assert (threads[i]-> job .empty ());
625
- threads[i]-> job = job;
626
- threads[i]-> has_wake_signal = true ;
623
+ CV_Assert (thread. job .empty ());
624
+ thread. job = job;
625
+ thread. has_wake_signal = true ;
627
626
#ifdef CV_PROFILE_THREADS
628
627
threads_stat[i + 1 ].reset ();
629
628
#endif
630
629
#if !defined(CV_USE_GLOBAL_WORKERS_COND_VAR)
631
- pthread_cond_broadcast/* pthread_cond_signal*/ (&threads[i]-> cond_thread_wake ); // wake thread
630
+ pthread_cond_broadcast/* pthread_cond_signal*/ (&thread. cond_thread_wake ); // wake thread
632
631
#endif
633
632
}
634
633
}
0 commit comments