@@ -1226,28 +1226,13 @@ class KDTreeBaseClass
1226
1226
1227
1227
node->node_type .sub .divfeat = cutfeat;
1228
1228
1229
- std::future<NodePtr> left_future, right_future;
1230
-
1231
- BoundingBox left_bbox (bbox);
1232
- left_bbox[cutfeat].high = cutval;
1233
- if (++thread_count < n_thread_build_)
1234
- {
1235
- left_future = std::async (
1236
- std::launch::async, &KDTreeBaseClass::divideTreeConcurrent,
1237
- this , std::ref (obj), left, left + idx, std::ref (left_bbox),
1238
- std::ref (thread_count), std::ref (mutex));
1239
- }
1240
- else
1241
- {
1242
- --thread_count;
1243
- node->child1 = this ->divideTreeConcurrent (
1244
- obj, left, left + idx, left_bbox, thread_count, mutex);
1245
- }
1229
+ std::future<NodePtr> right_future;
1246
1230
1247
1231
BoundingBox right_bbox (bbox);
1248
1232
right_bbox[cutfeat].low = cutval;
1249
1233
if (++thread_count < n_thread_build_)
1250
1234
{
1235
+ // Concurrent right sub-tree
1251
1236
right_future = std::async (
1252
1237
std::launch::async, &KDTreeBaseClass::divideTreeConcurrent,
1253
1238
this , std::ref (obj), left + idx, right,
@@ -1257,20 +1242,24 @@ class KDTreeBaseClass
1257
1242
else
1258
1243
{
1259
1244
--thread_count;
1260
- node->child2 = this ->divideTreeConcurrent (
1261
- obj, left + idx, right, right_bbox, thread_count, mutex);
1262
1245
}
1263
1246
1264
- if (left_future. valid ())
1265
- {
1266
- node->child1 = left_future. get ();
1267
- -- thread_count;
1268
- }
1247
+ BoundingBox left_bbox (bbox);
1248
+ left_bbox[cutfeat]. high = cutval;
1249
+ node->child1 = this -> divideTreeConcurrent (
1250
+ obj, left, left + idx, left_bbox, thread_count, mutex) ;
1251
+
1269
1252
if (right_future.valid ())
1270
1253
{
1254
+ // Block and wait for concurrent right sub-tree
1271
1255
node->child2 = right_future.get ();
1272
1256
--thread_count;
1273
1257
}
1258
+ else
1259
+ {
1260
+ node->child2 = this ->divideTreeConcurrent (
1261
+ obj, left + idx, right, right_bbox, thread_count, mutex);
1262
+ }
1274
1263
1275
1264
node->node_type .sub .divlow = left_bbox[cutfeat].high ;
1276
1265
node->node_type .sub .divhigh = right_bbox[cutfeat].low ;
0 commit comments