Skip to content

Commit eb94dfb

Browse files
committed
Merge pull request opencv#10885 from alalek:issue_10881
2 parents 7be0c78 + 491502a commit eb94dfb

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

modules/core/src/parallel_impl.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class WorkerThread
190190
{
191191
public:
192192
ThreadPool& thread_pool;
193-
unsigned id;
193+
const unsigned id;
194194
pthread_t posix_thread;
195195
bool is_created;
196196

@@ -418,14 +418,15 @@ void WorkerThread::thread_body()
418418
stat.threadWake = getTickCount();
419419
#endif
420420

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+
421428
if (!stop_thread)
422429
{
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);
429430
ParallelJob* j = j_ptr;
430431
if (j)
431432
{
@@ -480,10 +481,6 @@ void WorkerThread::thread_body()
480481
}
481482
}
482483
}
483-
else
484-
{
485-
pthread_mutex_unlock(&mutex);
486-
}
487484
#ifdef CV_PROFILE_THREADS
488485
stat.threadFree = getTickCount();
489486
stat.keepActive = allow_active_wait;
@@ -595,40 +592,42 @@ void ThreadPool::run(const Range& range, const ParallelLoopBody& body, double ns
595592
CV_LOG_VERBOSE(NULL, 5, "MainThread: wake worker threads...");
596593
for (size_t i = 0; i < threads.size(); ++i)
597594
{
595+
WorkerThread& thread = *(threads[i].get());
596+
if (
598597
#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 ||
603599
#endif
600+
thread.has_wake_signal
601+
|| !thread.job.empty() // #10881
602+
)
604603
{
605-
pthread_mutex_lock(&threads[i]->mutex);
606-
threads[i]->job = job;
604+
pthread_mutex_lock(&thread.mutex);
605+
thread.job = job;
607606
#if !defined(CV_USE_GLOBAL_WORKERS_COND_VAR)
608-
isActive = threads[i]->isActive;
607+
bool isActive = thread.isActive;
609608
#endif
610-
threads[i]->has_wake_signal = true;
609+
thread.has_wake_signal = true;
611610
#ifdef CV_PROFILE_THREADS
612611
threads_stat[i + 1].reset();
613612
#endif
614-
pthread_mutex_unlock(&threads[i]->mutex);
613+
pthread_mutex_unlock(&thread.mutex);
615614
#if !defined(CV_USE_GLOBAL_WORKERS_COND_VAR)
616615
if (!isActive)
617616
{
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
619618
}
620619
#endif
621620
}
622621
else
623622
{
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;
627626
#ifdef CV_PROFILE_THREADS
628627
threads_stat[i + 1].reset();
629628
#endif
630629
#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
632631
#endif
633632
}
634633
}

0 commit comments

Comments
 (0)