I need advice in design and c++ implementation (I am a java guy, new to c++).
I have list of processes.
One thread add/remove process to the list and second thread once in 10ms runs and collects all processes (and removes them from the list) that have timed out. Each process has a creation timestamp when it was added to the list. If 3 sec have passed and the process is still in the list (not removed by the first thread) it is removed from the list by the cleaner process.
This has to be with low overhead.
So I have processListContainer with 3 methods:
add, remove , clean
need to sync between the add/removeById and the clean.
This can be done by clever thinking such as creating a new list when cleaning, and handle multiple lists.
The list size is around 100K and thousands added in sec
You can organize your list of processes into a list of sublists. Each sublist is a collection of processes that would expire within the same 5ms interval. This would allow your
cleanerthread to clear out an entire sublist when it is deciding to remove stale processes, instead of iterating through individual processes.The list initially contains an empty sublist.
The
cleanoperation is a loop with the exit condition being either the head sublist is empty or the head sublist is not expirable. If the head sublist has expired, it cleans out the list. If the list has more than 1 sublist, it removes the resulting empty sublist. The loop repeats.So,
addwould check to see if the process belongs to the last sublist. If so, it adds itself to that sublist. If not, it creates a new sublist, and adds itself to it, and adds the sublist to the list.The
removeelement removes the provided element from the sublist. If the sublist becomes empty, and there is more than one sublist in the list, it removes the empty sublist.In this scheme,
addshould not need to lock, since it only manipulates the tail, or an unexpirable sublist.cleanandremoveboth need locks since they may both manipulate the head of the list.