Testing for callbacks thread safety in C++
[iot2.git] / iotjava / iotrmi / C++ / sample / TestClass.hpp
index 71bd4bd79e96b1f2dd667731b33731b6b0d3ac2b..fb9c05406561d329c9b16987d226c34b4c2897aa 100644 (file)
@@ -2,6 +2,8 @@
 #define _TESTCLASS_HPP__
 
 #include <iostream>
+#include <thread>
+#include <chrono>
 #include "TestClassInterface.hpp"
 #include "StructC.hpp"
 
@@ -26,6 +28,9 @@ class TestClass : public TestClassInterface {
                void                            handleStruct(vector<data> vecData);
                vector<EnumC>           handleEnum(vector<EnumC> vecEn);
 
+               void                            thread1();
+               void                            thread2();
+
        private:                
                int                                                     intA;
                float                                           floatB;
@@ -151,15 +156,39 @@ vector<EnumC> TestClass::handleEnum(vector<EnumC> vecEn) {
 //     return cb.printInt();
 //}
 
+void TestClass::thread1() {
+
+       CallBackInterface* cb = cbvec[0];
+       for(int i = 0; i < 10; i++) {
+               cb->printInt();
+               this_thread::sleep_for(chrono::seconds(1));
+       }       
+}
+
+void TestClass::thread2() {
+
+       CallBackInterface* cb = cbvec[1];
+       for(int i = 0; i < 10; i++) {
+               cb->printInt();
+               this_thread::sleep_for(chrono::seconds(1));
+       }       
+}
 
 int TestClass::callBack() {
 
-       int sum = 0;
+       /*int sum = 0;
        for (CallBackInterface* cb : cbvec) {
                sum = sum + cb->printInt();
        }
 
-       return sum;
+       return sum;*/
+       thread th1 (&TestClass::thread1, this);
+       thread th2 (&TestClass::thread2, this);
+
+       th1.join();
+       th2.join();
+
+       return 1;
 }
 
 #endif