Question Threading Learning

davegravy

New member
Joined
Apr 6, 2012
Messages
2
Programming Experience
3-5
I have taught myself how to create and start/manage threads and I have tried to implement this in a mockup program. The mockup program iterates through a large integer arraylist. For each item in the list it increments the integer several times within a loop. The program takes 30 seconds to implement non-threaded.

I have added multiple threads to the mockup program, and fed each thread a section of the arraylist for processing. Each section is k elements long. The main UI thread contains a thread management loop whereby if a thread is determined to be done, it is fed another section of the list for processing.

I have experimented with different numbers of threads and different values for k, but the best performance I have been able to acheive is 7.5 seconds. My machine is an i7 (2600) quad core with hyperthreading (8 logical cores).

Should I be satisfied with this, or am I clearly not harnessing multithreading's full potential?

I eventually intend to apply this to a ray-tracing program I am writing. I have read a bit about backgroundworker and threadpool techniques, but they don't seem to offer much advantage to this application. Am I mistaken?

Thanks in advance.

Dave
 
How many available threads (logical cores) you have on the CPU dictates how many threads are useful. Having 16 threads on an 8-core machine just means each thread will share processor time with another. Plus you have to consider your UI thread and a system thread. So on a 8 logical core machine, 6 threads are available 100% of the time, if no other program is actively using the cpu. There are other considerations though, for example not every thread is necessarily working 100% of the time. Having extra threads on the pool lets your program take advantage of dead time from other threads. It's very rare that a single thread uses 100% of one logical core 100% of the time.
 
Last edited:
Threadpool is beneficial if you have many shorter lived tasks, in that used threads are kept alive for a while and reused for queued or upcoming tasks, this saves time creating threads.
 
Thanks for the replies, they are helpful.

A further question: I am sharing the array across all my threads, and no two threads are ever given a common array element to operate on. I am not using locking for this reason. Is this okay? From a performance perspective, is there any advantage to feeding each thread a copy of the original array?

I'm also still interested in knowing if the execution time improvement I achieved is reasonable.
 
If you want to use one list as a source for multiple threads and you're using .NET 4.0, you should probably use a ConcurrentStack or ConcurrentQueue. Both are specifically thread-safe, which is the whole purpose of the System.Collections.Concurrent namespace. Both have a constructor that will take your original array as an argument. The difference is the order in which the items will be removed from the collection. The queue will return the items in ascending order of the index in the original array while the stack will return them in descending order.

That said, if you use the ThreadPool then you would actually specify the data for each task when you call QueueUserWorkItem, so there's no need to retrieve data from inside the thread entry method. Even if you're creating Thread objects directly, you can still pass data into the entry method via the Start method.
 
Back
Top