Boost logo

Boost Users :

Subject: Re: [Boost-users] Thread overhead
From: Steve Lorimer (steve.lorimer_at_[hidden])
Date: 2012-08-20 01:48:34


I don't think it's overhead from boost::thread per se, but rather OS
overhead due to context switching.

In what you call "native foo" all you're doing is call foo in main thread's
context.

In "thread foo" you're adding on the overhead of creating 2 threads, and
then having both run foo() concurrently, and in the meanwhile you're
telling main thread to join these 2 threads.

Check out this wikipedia link if you're interested in understanding context
switching more: http://en.wikipedia.org/wiki/Context_switch

HTH
Steve

On 20 August 2012 14:11, Alireza Haghdoost <haghdoost_at_[hidden]> wrote:

> Hi
>
> Is there anyway to reduce overhead of threadFoo() call in this simple
> program ?
>
>
> class baseClass {
> public:
> void foo(){
> boost::posix_time::milliseconds delay(143);
> boost::this_thread::sleep(delay);
> }
> void threadFoo(){
> threadObjOne = boost::thread(&baseClass::foo, this);
> threadObjTwo = boost::thread(&baseClass::foo, this);
> threadObjOne.join();
> threadObjTwo.join();
> }
> private:
> boost::thread threadObjOne;
> boost::thread threadObjTwo;
> };
>
> int main(){
> std::cout<< "main startup"<<std::endl;
>
> baseClass baseObj;
>
> tick_t startTime,endTime;
>
> rdtscll(startTime);
>
> baseObj.foo();
>
> rdtscll(endTime);
>
> std::cout<<"native foo() call takes "<< endTime-startTime <<"
> clock cycles"<<std::endl;
>
> rdtscll(startTime);
>
> baseObj.threadFoo();
>
> rdtscll(endTime);
>
> std::cout<<"Thread foo() call takes "<< endTime-startTime <<"
> clock cycles"<<std::endl;
>
> }
>
>
> The output is :
> main startup
> native foo() call takes 368222398 clock cycles
> Thread foo() call takes 372600905 clock cycles
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net