Boost logo

Boost :

From: Matus Chochlik (chochlik_at_[hidden])
Date: 2008-05-15 04:11:58


Hi,

IMHO the problem is that the tasks are too simple and are executed too quickly
when compared to thread creation time.
A task, or several tasks, that spend less that one second of CPU time
are not worth
creating a new dedicated thread. This is actually why thread pools are created.

In my experience it is much more efficient to put such short tasks into a queue
and execute them sequentially. If you have many of such tasks
(~hundreds and more)
and You want to use multiple CPU's then create several worker threads
at application
startup, that pick the tasks from the queue and execute them.

I didn't try to run Your example myself but I've seen things like this
happening before.

Maybe try to change the

const int n = 100;

to something like

const int n = 10000;

in the MyStruct::operator() and see what happens.

However, it's also possible that I'm missing something :-) and there is indeed
some issue with the implementation of Boost.Threads

On Wed, May 14, 2008 at 9:31 PM, James Sutherland
<James.Sutherland_at_[hidden]> wrote:
> I have been testing thread performance on Linux and Mac. My Linux
> system has two dual-core processors and my Mac has one dual-core
> processor. Both are intel chips.
>
> For the code snippet given below, the execution time should ideally
> decrease as the number of threads increases. However, the opposite
> trend is observed. For example, using -O3 flags on my Linux desktop
> produces the following timings:
> 1 Thread: 0.66 sec
> 2 Threads: 0.9 sec
> 3 Threads: 1.2 sec
> 4 Threads: 1.4 sec
>
> I do not have a lot of experience with threads, and was wondering if
> this result surprises anyone?
>
> James
>
> -----
> #include <boost/thread.hpp>
> #include <iostream>
> #include <vector>
> #include <time.h>
>
> struct MyStruct
> {
> explicit MyStruct(const int i) : tag(i) {}
> void operator()() const
> {
> const int n = 100;
> std::vector<int> nums(n,0);
> for( int j=0; j<1000000; ++j )
> for( int i=0; i<n; ++i )
> nums[i] = i+tag;
> }
> private:
> int tag;
> };
>
> int main()
> {
> using namespace std;
> const int nTasks = 12;
> const int nThreads = 4;
> assert( nTasks%nThreads == 0 );
> assert( nThreads<=nTasks );
> cout << "Executing " << nTasks << " using " << nThreads << "
> threads." << endl;
> time_t t1 = clock();
> for( int itask=0; itask<nTasks; ++itask ){
> boost::thread_group threads;
> for( int i=0; i<nThreads; ++i ){
> threads.create_thread( MyStruct(itask++ + 100) );
> }
> threads.join_all();
> }
> t2 = clock();
> cout << "time: " << difftime( t2,t1)/CLOCKS_PER_SEC << endl;
> }
> ----
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>

-- 
________________
::matus_chochlik

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk