How is the running time of algorithms that are affected by multithreading specified?
For example, a CompareAndSet loop may never be satisfied (if you’re very very unlucky)
AtomicReference<ContainerOfItems> oldContainer;
void AddItem(Item aItem)
{
ContainerOfItems newContainer;
do
{
newContainer = null;
newContainer = new ContainerOfItems();
newContainer.CopyContents(oldContainer);
newContainer.Add(aItem);
}
while (!CompareAndSet(oldContainer, newContainer));
oldContainer = null;
}
In this example (which looks a lot like Java but really is pseudocode) the CopyContents
operation could take a long time, such that oldContainer has been replaced by some other thread causing the CompareAndSet to fail. What’s the running time of this code?
The overall runtime of your program depends highly on how long
copyContents(...)takes and a prediction on how often there are going to be race conditions that cause thecompareAndSet(...)to fail. This will depend on how many threads are running at the same time.However, I suspect that in terms of Big-O, the number of times looping because of
compareAndSet(...)does not matter. For example, if acopyContents(...)takes O(N) time to run, on average it has to loop 3 times to complete thecompareAndSet(...), and you run it N times to add all of the items, the it is O(N^2) — the 3 drops out because of the constant.Also, because you are implying concurrency, there will also be a factor speed improvement because the algorithm will be multi-threaded, but that too will only be a constant factor improvement and not affect the Big-O. So the Big-O can be calculated by looking at the Big-O of
copyContents(...)times (I assume) N.