In my program (basic task manager) I monitor a list of processes, and allow to make changes to process (e.g. kill a process or raise its priority).
To implement it I design it as follows: one thread is the worker thread that does all the work, one thread is updating the status in the GUI. There is a processes list, and I use lock to make it thread-safe. The thing is I’m scanning all the processes on a regular interval (say 1 second) and in the meanwhile I cannot make changes to the list, as another thread is working on it. Therefore my program lag sometimes to display the operation
Is this a good design? What can I do to improve it? I think to lower the interval value.
Rather than having a shared list, consider having a list that your worker thread maintains which is the master list. Once it has done its scan it then passes a copy of the master list to the GUI thread. The GUI thread is then able to update the GUI with the new information contained in the copy whilst the worker thread can be performing another scan.
This removes the need for a lock, because the worker thread is only ever writing to one list and then taking creating a copy (in the same thread).
One problem with this approach is what happens if the background worker is able to gather a new process list faster than the GUI can update. You need to be able to deal with receiving a new process list while it is still processing the previous update.