Can I create a semaphore with locked transactions in MySQL?
Plan A -- If the task takes only a few seconds,

    START TRANSACTION;
    do all the task (possibly including FOR UPDATE)
    COMMIT;


Plan B -- If the task takes longer than a few seconds.  (Note:  The UPDATEs can probably be done with a single statement, hence no need for a transaction.)

    (with autocommit=ON)
    UPDATE ... to grab an item and announce who has the item
    do the task
    UPDATE ... to release the item


Plan C -- Read about GET_LOCK() and RELEASE_LOCK().  Note that there is a timeout on the GET.

lock_wait_timeout (which applies to Plan A) defaults to 50 seconds.  But I think you should not have transactions that last anywhere near that long.  Hence, Plan B.

Yes, Plan A might include

SELECT .. WHERE claimed=0 ORDER BY RAND() LIMIT 1  FOR UPDATE;


But notice the FOR UPDATE.  And there should be no case for failure.  You have limited the choices to claimed=0.  Caution:  if this is a huge table, the random selection will take time.