要寫一個平行化的程式通常多半免不了需要一個 thread-safe 的 container,很不巧的是 STL 並沒有提供一個 thread-safe 的 container。雖然通常純讀不修改多半是可以 thread-safe,但如果要允許同時可讀又可寫就需要自己想辦法了。
這篇就是自己想辦法用 STL 的 container 生了一個 thread-safe 的 bounded queue。用 STL container 當基底純粹是因為我懶得自己做記憶體管理 XD
enum Type {Int, Double};
struct Data {
Type type;
union {
int i;
double d;
};
};
int main()
{
Data data;
data.type = Int;
data.i = 3;
data.type = Double;
data.d = 3.14;
return ;
}