[C++] call_onceを使ってある処理を一回だけ実行する

初期化処理などある処理を一度だけ呼び出して以降は呼び出さなくするやり方

以下の例ではSomeFunc()という関数の呼び出しの際に最初の一度だけinit()という関数を呼び出したい場合の例です。

[cpp]
#include <iostream>
#include <mutex>

void init (const int num) {

std::cout << "init(" << std::to_string(num) << ") called" << std::endl;
}

void SomeFunc() {

static std::once_flag flag;
std::call_once(flag, init, 10);

std::cout << "SomeFunc() called" << std::endl;
}

int main() {

SomeFunc();
SomeFunc();
SomeFunc();
}
[/cpp]

SomeFunc() 関数にstaticな変数flagを持たせて、call_onceでinit()関数を呼び出すかどうかのチェックをしています。
実行結果は以下の通り

init(10) called
SomeFunc() called
SomeFunc() called
SomeFunc() called

SomeFunc()は3回呼ばれていますが、SomeFunc()の中でcall_once()経由で呼び出されるinit()は一度しか呼び出されていないのが分かります。

[C++] Visual Studioでコンパイル時間を表示する

Visual Studioでビルドした際にコンパイルやリンクなどの各処理時間とトータルのビルド時間を出力するための設定です。

  1. TOOLS → Options → Projects and Solutions → VC++ Project Settingsに移動
  2. Building TimingをYesに変更(デフォルトはNo)
  3. OK

これでビルドを実行したさいに各ビルド処理毎の処理時間とトータル時間が表示されるようになります。

1>------ Build started: Project: MyProject, Configuration: Release Win32 ------
1>  MyProject.cpp
1>  Generating code
1>  1 of 2048 functions ( 0.0%) were compiled, the rest were copied from previous compilation.
1>    0 functions were new in current compilation
1>    0 functions had inline decision re-evaluated but remain unchanged
1>  Finished generating code
1>  MyProject.vcxproj -> C:\Users\kazuyoshi_aizawa\documents\visual studio 2015\Projects\MyProject\Release\MyProject.exe
1>  MyProject.vcxproj -> C:\Users\kazuyoshi_aizawa\documents\visual studio 2015\Projects\MyProject\Release\MyProject.pdb (Full PDB)
1>
1>Project Performance Summary:
1>     6281 ms  C:\Users\kazuyoshi_aizawa\documents\visual studio 2015\Projects\MyProject\MyProject\MyProject.vcxproj   1 calls
1>               6281 ms  Build  #ビルド時間                1 calls
1>
1>Target Performance Summary:
1>        0 ms  AfterBuildCompileEvent                     1 calls
   ...略...
1>        3 ms  PrepareForBuild                            1 calls
1>        4 ms  FinalizeBuildStatus                        1 calls
1>        8 ms  InitializeBuildStatus                      1 calls
1>       12 ms  SetCABuildNativeEnvironmentVariables       1 calls
1>     2473 ms  Link  #リンク時間                           1 calls
1>     3751 ms  ClCompile  #コンパイル'時間                  1 calls
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

[C++] プリコンパイルヘッダでビルド時間を短縮する

C++で開発するときにBoostを利用することも多いとおもうのですけど、Boostを利用するとビルド時間がかかるようになって大変です。何十ものソースコードで構成されたプロジェクトなんかで再ビルドしたときなんかはそのビルド時間に辟易してしまいます。

そこで役に立つのがプリコンパイ済みヘッダ(precommpiled header)です。プリコンパイル済みヘッダを利用するとコンパイル時間を大幅に短くできます。

Visual Studio 2015で新規のC++プロジェクトを作成した場合、デフォルトでプリコンパイル済みヘッダを利用するように設定されています。もし設定されているかどうか不安な場合には以下で確認できます。

プロジェクトの設定確認

  1. プロジェクト名を右クリックしPropertiesを選択、Project Property Pageを表示
  2. Configuration Property → C/C++ → Precompiled Header
  3. Precompile Header が “Use (/Yu)“となっているのを確認。
  4. Precompile Header File は “stdafx.h”(デフォル)
  5. Precompiled Header Ouput File は”$(IntDir)$(TargetName).pch”(デフォルト)

プレコンパイル用のファイルのプロパティの確認

こちらもプロジェクト作成時に自動的に作成されているはずですが念のため確認します。

  1. Header Filesに stdafx.h というヘッダファイルがあるのを確認する。なければ追加。この時点では空ファイルでもいい。
  2. Source Filesに stdafx.cppというソースファイルがあるのを確認。なければ追加。内容は以下のとおり。
    #include "stdafx.h"
  3. stdafx.cppを右クリックしてのPropertiesを選択しプロパティウィンドを開く
  4. Configuration Property → C/C++ → Precompiled Header
  5. Precompile Header が “Create (/Yc)“となっているのを確認。上の設定と違うので注意。

プレコンパイルするヘッダの指定

とりあえずプロジェクト側の設定は終わりました。(デフォルトで設定されてますが)
実際に以下のようなBoostをつかったコードをコンパイルしてみることにします。

[cpp title=”MyProject.cpp”]
#include "stdafx.h"
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;string&amp;gt;
#include &amp;lt;boost/thread/future.hpp&amp;gt;

void printHelloTask() {
std::cout &amp;lt;&amp;lt; "Hello" &amp;lt;&amp;lt; std::endl;
}

int main() {

boost::unique_future&amp;lt;void&amp;gt; f1 =
boost::async(boost::launch::async, printHelloTask);
}
[/cpp]

これをこのままコンパイルすると3秒かかります。

1>------ Build started: Project: MyProject, Configuration: Release Win32 ------
1>  MyProject.cpp
1>  Generating code
...
1>     3722 ms  ClCompile                                  1 calls
1>     3941 ms  Link                                       1 calls
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

※Visual Studioでビルド時間を表示するように変更するにはVisual Studioコンパイル時間を表示するを参照してください。

プレコンパイルするヘッダを指定する

時間がかかるヘッダをstdafx.hヘッダに追加します。ソースファイルのstdafx.cppの方ではないのでお間違いなく。
ここでは<boost/thread/future.hpp>を指定します。

[cpp title=”stdafx.h”]

#include <boost/thread/future.hpp>

[/cpp]

ソース・ファイルのプロパティの確認

実際のプログラムを記載したソースファイルを作成すれば自動的に上で上記で作成されたプレコンパイル済みヘッダを利用することになっているはずですが、念のためソース・ファイルのプロパティを確認してプレコンパイル済みヘッダを使うようになっているか確認してみましょう。

  1. ソースファイル(MyProject.cpp)を右クリックしてのPropertiesを選択しプロパティウィンドを開く
  2. Configuration Property → C/C++ → Precompiled Header
  3. Precompile Header が “Use (/Yu)“となっているのを確認。こちらはまた Use です。

 

これで準備が終わったので再度コンパイルしてみます。

 

1>------ Build started: Project: MyProject, Configuration: Release Win32 ------
1>  MyProject.cpp
1>  Generating code
...
1>      593 ms  ClCompile                                  1 calls
1>     1903 ms  Link                                       1 calls
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

4秒近くかかっていたものが600ms程度(6倍)になりました! ソースファイルが多くなればなるほど効果が大きいのがおわかりになると思います。
ちなみにプレコンパイルヘッダ指定後最初のコンパイル時にはstdafx.cppがビルドされプレコンパイルヘッダ自身(*.pch)が作成されるのでこれには時間がかかります。以降のコンパイル時にはプレコンパイル済みヘッダが使われるのでソース・ファイル(MyProject.cpp)のコンパイルは早くなります。

[C++] std::sortに渡すソート条件を指定する比較関数 (Compare Function)

自分が作ったクラスをベクターなどに格納しそれらを指定の任意の条件でソートしたい場合があると思います。 std::sortはこのソートの条件に使う比較関数オブジェクトを受け取ってそれに従ってソートしてくれます。

比較関数は二つのオブジェクトを引数にとり最初に引数が2番めの引数より小さい時にtrueを返すようにします。(昇順でソートする場合)

文字列の長さ順位ソートする場合の比較関数

[cpp]
bool comp(const std::string& lh, const std::string& rh) {
// 文字列lhの長さが文字列rhより短かったらtrueを返す
return lh.length() < rh.length();
}
[/cpp]

関数ではなくoperator()を実装したクラスとして宣言し、そのオブジェクトを渡すこともできます。

operator()をもったクラスとして実装する場合

[cpp]

class Comp {
public:
bool operator() (const std::string& lh, const std::string& rh) {</pre>

// 文字列lhの長さが文字列rhより短かったらtrueを返す
return lh.length() < rh.length();
}
};

[/cpp]

文字列のリストを長さ順でソートする例

[cpp]

#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

bool comp(const std::string& lh, const std::string& rh) {

return lh.length() < rh.length();
}

class Comp {
public:
bool operator() (const std::string& lh, const std::string& rh) {

return lh.length() < rh.length();
}
};

int main () {

std::vector<std::string> strs {
"clock",
"factory",
"industrialization",
"skyscraper",
"sun"
};

// 二つの引数をうけとりboolを返す関数を渡す場合
std::sort(strs.begin(), strs.end(), comp);
for(const std::string& str : strs) {

std::cout << str << std::endl;
}

std::cout << std::endl;

// operator() を持ったクラスのオブジェクトを渡す場合
std::sort(strs.begin(), strs.end(), Comp());
for(const std::string& str : strs) {

std::cout << str << std::endl;
}
}

[/cpp]

実行結果

sun
clock
factory
skyscraper
industrialization

sun
clock
factory
skyscraper
industrialization

ラムダ式でソートの条件を指定する場合

[cpp]
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main()
{
std::vector < std::string> words{ "12345", "12", "123456", "123" };

std::sort(words.begin(), words.end(), [](const std::string& lh, const std::string& rh) {
return rh.length() > lh.length(); });

for (const std::string str : words)
{
std::cout << str << std::endl;
}
}
[/cpp]

実行結果

12
123
12345
123456

[C++] boost::asyncを使って引数や戻り値のあるタスクを実行する

boost::asyncを使って非同期のタスクを実行することができるのだけど、引数や戻り値の有り無し、タスクがメンバ関数だった時の構文を忘れがちなのでまとめておく

[toc]

引数も戻り値もない場合

[cpp]
#include <stdio.h>
#include <string>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>

// 引数も戻り値もないタスク
void printHelloTask()
{
std::cout << "Hello" << std::endl;
}

int main ()
{
//タスク投入
boost::unique_future<void> f1 =
boost::async(boost::launch::async, printHelloTask);
//タスク完了待ち
f1.wait();
}
[/cpp]

boost::asyncのコンストラクタの第一引数はローンチポリシーで、boost::launch::asyncを渡した場合にはスレッドが作られてそこでタスクが実行されます。
第二引数は実行するタスクを渡します。
実行するタスクに引数も戻り値もない場合は単純です。

戻り値がある場合

[cpp]
#include <stdio.h>
#include <string>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>

// 戻り値があるタスク
std::string returnStringTask()
{
return "Good Morning";
}

int main ()
{
//タスク投入
boost::unique_future<std::string> f3 =
boost::async(
boost::launch::async,
returnStringTask);
//タスク完了待ち
f3.wait();
//タスクの戻り値を取得
std::cout << f3.get() << std::endl;

return 0;
}
[/cpp]

戻り値がある場合もほぼ同じです。
ただboost::asyncの戻り値のfutureのテンプレート引数にreturnStringTaskの戻り値のstd::stringを指定します。

引数がある場合

[cpp]
#include <stdio.h>
#include <string>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>

// 引数があるタスク
void printStringTask(std::string str)
{
std::cout << str << std::endl;
}

int main ()
{
//タスク投入
boost::unique_future<void> f2 =
boost::async(
boost::launch::async,
boost::bind(printStringTask, "Hey"));
//タスク完了待ち
f2.wait();

return 0;
}
[/cpp]

引数がある場合少しめんどくさくなります。
boost::asyncにはタスクの関数しか渡せませんが同時に引数の文字列”Hey”を渡さなければならないのでboost::bind()を利用して関数と引数をバインドしています。

boost::bind(printStringTask, "Hey"));

引数も戻り値もあるクラスのメンバ関数を呼び出す場合

[cpp]
#include <stdio.h>
#include <string>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>

class Print {
private:
const std::string name;
public:
Print(const std::string& name) : name(name){}

//タスク投入を行う関数
void run ()
{
//タスク投入
boost::unique_future<std::string> f4 =
boost::async(
boost::launch::async,
boost::bind(
&Print::returnStringTask,
this, "Good Evening"));
//タスク完了待ち
f4.wait();
//タスクの戻り値を取得
std::cout << f4.get() << std::endl;
}

std::string returnStringTask (std::string message)
{
return message + " " + name;
}
};

int main ()
{
Print print("John");
print.run();

return 0;
}
[/cpp]

もっとも面倒くさいけどでもよく使うパターンです。
基本的には戻り値があるパターン、引数があるパターンの複合ですが、関数名にクラス名を付けてPrint::returnStringTaskとしてアドレスを渡しています。
そして、関数の第一引数としてオブジェクトのthisポインターを渡しています。

boost::bind(&Print::returnStringTask, this, "Good Evening"));