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 fdc86d8..b79b8e7 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/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
index 71bd4bd..fb9c054 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
index 665bd16..4fcfe31 100644 (file)
@@ -8,7 +8,7 @@ using namespace std;
 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();
index 95197c5..aba4ba0 100644 (file)
@@ -8,12 +8,12 @@ using namespace std;
 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;
-       ports.push_back(12346);
+       ports.push_back(12345);
        //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);
-       /*CallBackInterface *cb4 = new CallBack(53);
+       CallBackInterface *cb4 = new CallBack(53);
        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;