The current code does if !empty; get_job(), Of course, it's possible for there to be one job left, and two threads race between the !empty check and the get() call.
Instead, we need to check the empty status with the lock held. The easiest way to handle this is to move the check into the get() call, and return an optional.
The current code does
if !empty; get_job()
, Of course, it's possible for there to be one job left, and two threads race between the!empty
check and theget()
call.Instead, we need to check the empty status with the lock held. The easiest way to handle this is to move the check into the
get()
call, and return an optional.