Firstly, assuming that adding / removing elements to the list is not a cause of multithreading (instead, the taxation process is the logic for defining / creating these elements). If the insertion / deletion time of the list is a bottleneck, perhaps you should review your data structure.
Further, assuming that each thread will not interact with each other (one thread does not delete the record that was inserted by the other) and that each thread has a finite amount of work. Each thread does not touch the actual linked list; instead, each thread supports two additional lists.
It works as follows:
Each thread starts and creates two additional lists of records for deletion and insertion.
Given that the stream is not sorted when the stream is completed, we can simply add lists of additional elements for each stream to the beginning or end of the existing list.
Next, for deleted items, we combine the list of items to remove from each stream, and then we look at the original linked list and delete the items as they occur (performance here can be improved by using hashtable for the list of items to be deleted.
This works very well, provided that the two assumptions at the beginning are considered true. This also means that there is no need for mutexes or locks, your main list is updated only at the end with one thread after all threads are merged back into the main thread.
source share