[x264-devel] [PATCH 2/2] threadpool: check the exit flag under the run mutex

汪秀军 xiujunwang.dev at gmail.com
Fri Sep 25 15:39:22 UTC 2026


>From 55a4dfa367599153acee94cdccb05611c7b2baf0 Mon Sep 17 00:00:00 2001
From: Xiujun Wang <xiujunwang.dev at gmail.com>
Date: Thu, 24 Sep 2026 22:06:01 +0800
Subject: [PATCH 2/2] threadpool: check the exit flag under the run mutex

threadpool_delete() writes pool->exit under run.mutex, but the outer
worker loop reads it without synchronization. Volatile does not make
that access thread-safe; ThreadSanitizer reports a race during encoder
close.

Use the existing locked check after the condition-variable wait to
leave the worker loop, and remove the unsynchronized outer read.
No new jobs are dequeued after the shutdown request is observed.

Verified with a ThreadSanitizer harness that opens and closes an
encoder without flushing after each input count from 0 through 40.

Signed-off-by: Xiujun Wang <xiujunwang.dev at gmail.com>
---
 common/threadpool.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/common/threadpool.c b/common/threadpool.c
index fadd02ae..3fd0f75f 100644
--- a/common/threadpool.c
+++ b/common/threadpool.c
@@ -34,7 +34,7 @@ typedef struct

 struct x264_threadpool_t
 {
- volatile int exit;
+ int exit;
     int threads;
     x264_pthread_t *thread_handle;

@@ -47,12 +47,17 @@ struct x264_threadpool_t

 REALIGN_STACK static void *threadpool_thread( x264_threadpool_t *pool )
 {
- while( !pool->exit )
+ while( 1 )
     {
         x264_threadpool_job_t *job = NULL;
         x264_pthread_mutex_lock( &pool->run.mutex );
         while( !pool->exit && !pool->run.i_size )
             x264_pthread_cond_wait( &pool->run.cv_fill, &pool->run.mutex );
+ if( pool->exit )
+ {
+ x264_pthread_mutex_unlock( &pool->run.mutex );
+ break;
+ }
         if( pool->run.i_size )
         {
             job = (void*)x264_frame_shift( pool->run.list );
-- 
2.20.1


More information about the x264-devel mailing list