Coroutine-like functionality can help with that.
Code: Select all
Global ProducerSemaphore = CreateSemaphore()
Global ConsumerSemaphore = CreateSemaphore()
Global Mutex = CreateMutex()
Global Task.s
Procedure Producer(Total)
For i = 1 To Total
; producer task mutex lock to be thread-safe
LockMutex(Mutex)
Task = "bake bread"
PrintN(Task)
UnlockMutex(Mutex)
; signal that there is a new task done
SignalSemaphore(ProducerSemaphore)
; wait for the consumer to consume
WaitSemaphore(ConsumerSemaphore)
Next i
EndProcedure
Procedure Consumer(Total)
For i = 1 To Total
; wait for the producer to produce
WaitSemaphore(ProducerSemaphore)
; consumer task mutex lock to be thread-safe
LockMutex(Mutex)
Task = "eat bread"
PrintN(Task)
UnlockMutex(Mutex)
; signal that there is a new task done
SignalSemaphore(ConsumerSemaphore)
Next i
EndProcedure
; main
OpenConsole("Coroutines-like sequential process")
If CreateThread(@Producer(), 30)
CreateThread(@Consumer(), 30)
EndIf
Input()
CloseConsole()

EDIT: changed thread title, changed queue with a normal variable so the reader can focus at the task at hand, exchanged typos with other typos
Thank you Freak
