test-multi-threading.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 //#cmake:add-file ../../../../common/utilities/number/stabilized-value.h
5 
6 #include <thread>
7 #include <chrono>
8 #include "../../../test.h"
9 #include <../common/utilities/number/stabilized-value.h>
10 
11 using namespace utilities::number;
12 // Test group description:
13 // * This tests group verifies stabilized_value class.
14 //
15 // Current test description:
16 // * Verify that the stabilized_value class is thread safe,
17 // We fill the history with the first value and then let 2 thread run.
18 // first thread add 66% of first value and the rest of second value.
19 // second thread sample the 60% stability value each time and verify all equal to first value
20 
21 TEST_CASE( "multi-threading", "[stabilized value]" )
22 {
23  std::atomic< float > inserted_val_1 = { 20.0f };
24  std::atomic< float > inserted_val_2 = { 55.0f };
25  std::vector< float > values_vec;
26  stabilized_value< float > stab_value( 10 );
27 
28  // Fill history with values (> 60% is 'inserted_val_1')
29  std::thread first( [&]() {
30  std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
31  for( int i = 0; i < 1000; i++ )
32  {
33  if( i <= 10 )
34  {
35  stab_value.add( inserted_val_1 );
36  continue;
37  }
38  if( i % 3 )
39  stab_value.add( inserted_val_1 );
40  else
41  stab_value.add( inserted_val_2 );
42  std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
43  }
44  } );
45 
46  // Sample stabilized value
47  std::thread second( [&]() {
48  while( stab_value.empty() )
49  {
50  }
51 
52  for( int i = 0; i < 1000; i++ )
53  {
54  values_vec.push_back( stab_value.get( 0.6f ) );
55  if( i % 10 )
56  std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
57  }
58  } );
59 
60  if( first.joinable() )
61  first.join();
62  if( second.joinable() )
63  second.join();
64 
65  // Verify that all samples show the > 60% number inserted 'inserted_val_1'
66  int count = 0;
67  for( auto val : values_vec )
68  if( val == inserted_val_1 )
69  count++;
70 
71  CHECK( count == 1000 );
72 }
GLuint GLfloat * val
T get(float stabilization_percent=0.75f) const
GLint first
TEST_CASE("multi-threading","[stabilized value]")
GLint GLsizei count
int i
#define CHECK(condition)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11