Testing for callbacks thread safety in C++
authorrtrimana <rtrimana@uci.edu>
Mon, 7 Nov 2016 18:14:29 +0000 (10:14 -0800)
committerrtrimana <rtrimana@uci.edu>
Mon, 7 Nov 2016 18:14:29 +0000 (10:14 -0800)
iotjava/Makefile
iotjava/iotrmi/C++/sample/TestClass.hpp
iotjava/iotrmi/C++/sample/TestClass_Skeleton.cpp
iotjava/iotrmi/C++/sample/TestClass_Stub.cpp

index fdc86d8299bf7c151d658cb6b82cd94d43596f59..b79b8e7bd3a5098efcdfb4dd71a46def4125e514 100644 (file)
@@ -46,8 +46,8 @@ rmi:
 #      $(G++) iotrmi/C++/sample/CallBack_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_Stub.out --std=c++11
 #      $(G++) iotrmi/C++/sample/CallBack_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_Skeleton.out --std=c++11
        #$(G++) iotrmi/C++/sample/TestClass.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass.out --std=c++11
 #      $(G++) iotrmi/C++/sample/CallBack_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_Stub.out --std=c++11
 #      $(G++) iotrmi/C++/sample/CallBack_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_Skeleton.out --std=c++11
        #$(G++) iotrmi/C++/sample/TestClass.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass.out --std=c++11
-       $(G++) iotrmi/C++/sample/TestClass_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Stub.out --std=c++11 -pthread
-       $(G++) iotrmi/C++/sample/TestClass_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Skeleton.out --std=c++11 -pthread
+       $(G++) iotrmi/C++/sample/TestClass_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Stub.out --std=c++11 -pthread -pg
+       $(G++) iotrmi/C++/sample/TestClass_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Skeleton.out --std=c++11 -pthread -pg
        #$(G++) iotrmi/C++/sample/Test.cpp -o ../bin/iotrmi/C++/sample/Test.out --std=c++11 -lpthread
        #$(G++) iotrmi/C++/sample/Test2.cpp -o ../bin/iotrmi/C++/sample/Test2.out --std=c++11 -pthread -pg
 #      $(G++) iotrmi/C++/sample/StructC.cpp -o ../bin/iotrmi/C++/sample/StructC.out --std=c++11
        #$(G++) iotrmi/C++/sample/Test.cpp -o ../bin/iotrmi/C++/sample/Test.out --std=c++11 -lpthread
        #$(G++) iotrmi/C++/sample/Test2.cpp -o ../bin/iotrmi/C++/sample/Test2.out --std=c++11 -pthread -pg
 #      $(G++) iotrmi/C++/sample/StructC.cpp -o ../bin/iotrmi/C++/sample/StructC.out --std=c++11
index 71bd4bd79e96b1f2dd667731b33731b6b0d3ac2b..fb9c05406561d329c9b16987d226c34b4c2897aa 100644 (file)
@@ -2,6 +2,8 @@
 #define _TESTCLASS_HPP__
 
 #include <iostream>
 #define _TESTCLASS_HPP__
 
 #include <iostream>
+#include <thread>
+#include <chrono>
 #include "TestClassInterface.hpp"
 #include "StructC.hpp"
 
 #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                            handleStruct(vector<data> vecData);
                vector<EnumC>           handleEnum(vector<EnumC> vecEn);
 
+               void                            thread1();
+               void                            thread2();
+
        private:                
                int                                                     intA;
                float                                           floatB;
        private:                
                int                                                     intA;
                float                                           floatB;
@@ -151,15 +156,39 @@ vector<EnumC> TestClass::handleEnum(vector<EnumC> vecEn) {
 //     return cb.printInt();
 //}
 
 //     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 TestClass::callBack() {
 
-       int sum = 0;
+       /*int sum = 0;
        for (CallBackInterface* cb : cbvec) {
                sum = sum + cb->printInt();
        }
 
        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
 }
 
 #endif
index 665bd16faad67c7369a6b9542962307c21545cf9..4fcfe314ba75034868a79065b32b4051f23cbc81 100644 (file)
@@ -8,7 +8,7 @@ using namespace std;
 int main(int argc, char *argv[])
 {
 
 int main(int argc, char *argv[])
 {
 
-       int port = 5011;
+       int port = 5010;
        TestClassInterface *tc = new TestClass(3, 5.0, "7911");
        TestClass_Skeleton *tcSkel = new TestClass_Skeleton(tc, port);
        //tcSkel->waitRequestInvokeMethod();
        TestClassInterface *tc = new TestClass(3, 5.0, "7911");
        TestClass_Skeleton *tcSkel = new TestClass_Skeleton(tc, port);
        //tcSkel->waitRequestInvokeMethod();
index 95197c55223257506f5fa08408c541bffd86e28b..aba4ba01dd365c26710a6b444907c8184e5833b4 100644 (file)
@@ -8,12 +8,12 @@ using namespace std;
 int main(int argc, char *argv[])
 {
 
 int main(int argc, char *argv[])
 {
 
-       int port = 5011;
+       int port = 5010;
        const char* address = "localhost";
        int rev = 0;
        bool bResult = false;
        vector<int> ports;
        const char* address = "localhost";
        int rev = 0;
        bool bResult = false;
        vector<int> ports;
-       ports.push_back(12346);
+       ports.push_back(12345);
        //ports.push_back(13234);
 
        TestClassInterface *tcStub = new TestClass_Stub(port, address, rev, &bResult, ports);
        //ports.push_back(13234);
 
        TestClassInterface *tcStub = new TestClass_Stub(port, address, rev, &bResult, ports);
@@ -38,14 +38,14 @@ int main(int argc, char *argv[])
        cb.push_back(cb2);
        cb.push_back(cb3);
        tcStub->registerCallback(cb);
        cb.push_back(cb2);
        cb.push_back(cb3);
        tcStub->registerCallback(cb);
-       /*CallBackInterface *cb4 = new CallBack(53);
+       CallBackInterface *cb4 = new CallBack(53);
        CallBackInterface *cb5 = new CallBack(63);
        CallBackInterface *cb6 = new CallBack(73);
        vector<CallBackInterface*> cbsec;
        CallBackInterface *cb5 = new CallBack(63);
        CallBackInterface *cb6 = new CallBack(73);
        vector<CallBackInterface*> cbsec;
-       cb.push_back(cb4);
-       cb.push_back(cb5);
-       cb.push_back(cb6);
-       tcStub->registerCallback(cbsec);*/
+       cbsec.push_back(cb4);
+       cbsec.push_back(cb5);
+       cbsec.push_back(cb6);
+       tcStub->registerCallback(cbsec);
        cout << "Return value from callback: " << tcStub->callBack() << endl;
 
        vector<data> dataset;
        cout << "Return value from callback: " << tcStub->callBack() << endl;
 
        vector<data> dataset;