Several improvements to jeb's answer.
No need for an additional lock file. The queue.txt file may serve as its own lock.
In addition, access to the inner block itself must be guaranteed using SUCCESS (errorlevel 0), so that repetition will only occur when the redirect failed. (CALL ) (pay attention to the final space) is a completely unintuitive, but extremely effective way to eliminate any error. Not required here, but (CALL) (note the lack of space) is an effective way to fix the error.
:lockedAppend 2>nul ( >>queue.txt ( REM Do any amount of processing within this block. REM All stdout will safely be appended to the queue REM You could even call out to a subroutine, or another batch file echo Any text you want REM The CALL below ensures that the block exits with ERRORLEVEL 0 - success (call ) ) )||goto :lockedAppend
source share