I have a use case that I'm not sure how to solve (my experience with async/concurrent code is rather simplistic).
Basically I have two types of tasks: generate tasks and a combine task. I would like generate tasks to run concurrently from a queue (the generate tasks are quite CPU intensive, so I want to limit the number that run at a time). When the queue runs dry, it should then run the combine task. While the combine task is running, new generate tasks can be added to the queue, but should not run till the combine task is done. (By extension, the combine task should not run if there are any generate tasks running.)
Also, there will often be long periouds of time where no tasks are running, so I don't want a busy wait for new generate tasks.
What would be the correct way to handle this? Is there a common idiom or pattern for such a use case?