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@gmail.com> 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@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users