stdRefInThread

1 std::thread传入引用值需要使用std::ref

std::ref的说明: Constructs an object of the appropriate reference_wrapper type to hold a reference to elem.
其实主要是,如果要向thread传参的时候,该参数在线程内会被修改,需要用这个ref作为一个wrapper将对象包裹成为一个引用然后传入。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void main() {

while(1){
...<省略>...
if (!initializedFlag) {
initializedFlag = true;
// viewThd = new std::thread(viewThread, camVecToDraw); // wrong way
viewThd = new std::thread(viewThread, std::ref(camVecToDraw)); // right way
}
...<省略>...

addCamToDrawVec(imgData.image, camVecToDraw);
camVecToDraw;
}

viewThd.join();
delete viewThd;
viewThd = nullptr;
}

void viewThread(std::vector<pangolin::OpenGlMatrix>& camVecToDraw) {
while(!shallQuit()) {
// render some cameras according to camVecToDraw
...<省略>...
}
}