MultiCores - Software Falls Behind
We seem to be in a period, hopefuly fairly brief, where software development is lagging behind the hardware. If you buy a new server or desktop today its probably going to have at least two cores. Four core machines are also available and the number of cores can only increase over time.
However most software we write will not make use of these extra cores. Sure the operating system may run different programs on different cores, but if your software is processor intensive you are losing a lot by having it run on a single core and can be wasting much of your computers processing power.
You can write multi-threaded code in most languages and doing this will enable to use all the processors. However doing this is not trivial and can lead to bugs which happen randomly and are very difficult to find and fix.
DotNet has the ThreadPool with its QueueUserWorkItem method and the InvokeRequired method which you can call to check if its safe to directly run the code or if you need to invoke it via a delegate.
So we are waiting for the tools providers to do more of the hard work for us.
In the dotnet word we have the Task Parallel Library.
This allows you to replace the standard for loop with a call to the method Parallel.For
One of the paramaters is a delegate which should be the logic to run inside the loop.
Of course this logic has to be such that it doesnt rely on being run sequentially.
Theres a lot more to this experimental library which you can look at here :
I think some of the work in here may end up in the core language at some time.
Of course if TPL ( Task Parallel Library Code ) is run on a single processor machine it will run as normal using the single processor. If you have more than one core available a algorithm looks at processor usage on each core to decide which cores to use.
Allen Bauer( CodeGear Chief Scientist ) is looking at implementing the TPL for Delphi : http://blogs.codegear.com/abauer/2008/02/22/38857
DotNet also has the parallel linq extensions :
This lets you parallilze your LINQ code.
Predating both of the above is the dotnet reasearch language comega :
So if you need to make full use of the computer there are already some alternatives than getting into the low level world of ThreadPools and race conditions. Hopefully these alternatives will mature fairly quicly and move into the core product, however you are still going to have to do some changes to your code.