@ -41,7 +41,8 @@ ThreadPool::ThreadPool(int num_threads) {
for ( ; ; ) {
for ( ; ; ) {
std : : unique_lock < std : : mutex > lock ( mu_ ) ;
std : : unique_lock < std : : mutex > lock ( mu_ ) ;
// Wait until work is available or we are shutting down.
// Wait until work is available or we are shutting down.
cv_ . wait ( lock , [ = ] ( ) { return shutdown_ | | ! callbacks_ . empty ( ) ; } ) ;
if ( ! shutdown_ | | callbacks_ . empty ( ) )
cv_ . wait ( lock , [ = ] ( ) { return shutdown_ | | ! callbacks_ . empty ( ) ; } ) ;
// Drain callbacks before considering shutdown to ensure all work
// Drain callbacks before considering shutdown to ensure all work
// gets completed.
// gets completed.
if ( ! callbacks_ . empty ( ) ) {
if ( ! callbacks_ . empty ( ) ) {
@ -71,7 +72,7 @@ ThreadPool::~ThreadPool() {
void ThreadPool : : ScheduleCallback ( const std : : function < void ( ) > & callback ) {
void ThreadPool : : ScheduleCallback ( const std : : function < void ( ) > & callback ) {
std : : lock_guard < std : : mutex > lock ( mu_ ) ;
std : : lock_guard < std : : mutex > lock ( mu_ ) ;
callbacks_ . push ( callback ) ;
callbacks_ . push ( callback ) ;
cv_ . notify_all ( ) ;
cv_ . notify_one ( ) ;
}
}
} // namespace grpc
} // namespace grpc