マルチコア&スレッドいいね

すごく稚拙で当たり前っぽいことをメモとして日記に書くのですがお赦しください。忘れっぽいのでね.....。
マルチコアCPUだとマルチスレッドを使えばシングルスレッドのプログラムも速くなるのかな?という疑問が生まれたので試してみました。

#include <iostream>
#include <boost/thread.hpp>
#include <boost/progress.hpp>
 
void thread_1()
{
        int x = 0;
 
        for( int i = 0; i < 1000; ++i)
        {
                for( int j = 0; j < 1000000; ++j )
                        x = i + j;
        }
}
 
void thread_2()
{
        int x = 0;
 
        for( int i = 0; i < 1000; ++i)
        {
                for( int j = 0; j < 1000000; ++j )
                        x = i + j;
        }
}
 
void thread_test()
{
        boost::progress_timer timer;
 
        boost::thread th( thread_1 );
        //boost::thread th2( thread_2 );
        thread_2();
 
        th.join();
        //th2.join();
}
 
void single()
{
        boost::progress_timer timer;
        thread_1();
        thread_2();
}
 
int main()
{
        thread_test(); // 2.71Sec
        //single();     //5.43sec
        return 0;
}

ってことで速い!マルチコアCPUはもう割と一般的になってきたと言っても過言じゃないのでゲームとかでも使っていきたいと思います(ライブラリがやっててくれた部分以外はシングルスレッドでやってた)。