From: rtrimana Date: Mon, 7 Nov 2016 18:14:29 +0000 (-0800) Subject: Testing for callbacks thread safety in C++ X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iot2.git;a=commitdiff_plain;h=0f814d4848c4455287059efd6ed6d8a9cbb5503f Testing for callbacks thread safety in C++ --- diff --git a/iotjava/Makefile b/iotjava/Makefile index fdc86d8..b79b8e7 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -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 diff --git a/iotjava/iotrmi/C++/sample/TestClass.hpp b/iotjava/iotrmi/C++/sample/TestClass.hpp index 71bd4bd..fb9c054 100644 --- a/iotjava/iotrmi/C++/sample/TestClass.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass.hpp @@ -2,6 +2,8 @@ #define _TESTCLASS_HPP__ #include +#include +#include #include "TestClassInterface.hpp" #include "StructC.hpp" @@ -26,6 +28,9 @@ class TestClass : public TestClassInterface { void handleStruct(vector vecData); vector handleEnum(vector vecEn); + void thread1(); + void thread2(); + private: int intA; float floatB; @@ -151,15 +156,39 @@ vector TestClass::handleEnum(vector 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 diff --git a/iotjava/iotrmi/C++/sample/TestClass_Skeleton.cpp b/iotjava/iotrmi/C++/sample/TestClass_Skeleton.cpp index 665bd16..4fcfe31 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Skeleton.cpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Skeleton.cpp @@ -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(); diff --git a/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp b/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp index 95197c5..aba4ba0 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp @@ -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 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 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 dataset;