I have a requirement where single process should handle multiple jobs in parallel. Each of these jobs should be run periodically (example, every 10 seconds). Also Main thread needs to watch for stop signal and when received should stop all the threads and exit.
Below is my approach to handle this requirement.
Main thread will create thread for each job and wait for stop signal.Each thread is responsible for handling the looping mechanism. When main thread receives stop signal, it will send a signal to the threads to stop.
In this mechanism the treads will be running all the time. So I was thinking there may be better approach to handle this. Maybe like, main thread will keep track of when each job should be executed next and will start the thread when required. Thread will perform some operation and exit on completion. In this case threads will not be running all the time.
I currently don't know how the alternate approach can be implemented. So, I wanted to know which of the above might be good approach? Also, if there are any other better alternatives, please do suggest them.
Edit [19/Mar]:
Maybe I should have mentioned this initially but here goes.
Say if I have 2 jobs, both need not be run at the same time. For example, Job 1 should run every 10 seconds & Job 2 should run every 20 second.
Also, if the Job itself takes more time, then there must be some mechanism to identify and handle it properly. Maybe skip the execution or wait for previous job to complete & then start again. Currently, the requirement is not clear on this one. But, I should be able to recognize and handle this situation.