Fixed simpleclient crash caused by curResource being altered
authorSudarshan Prasad <sudarshan.prasad@intel.com>
Thu, 8 Jan 2015 17:28:09 +0000 (09:28 -0800)
committerSudarshan Prasad <sudarshan.prasad@intel.com>
Thu, 8 Jan 2015 17:32:36 +0000 (09:32 -0800)
The foundResource function is called asynchronously, and was written
in a way that was not thread safe. This fix adds a lock and exit logic
so that it works prooperly and only uses the first found curResource
that matches its requirements.

NOTE: This change (author: Erich Keane) is from 01.org.

Change-Id: I984ce1623ff491713ecf5d621f1283ebad33bfef
Signed-Off-by:Sudarshan Prasad<sudarshan.prasad@intel.com>

resource/examples/simpleclient.cpp

index 85c0dc1..5361ce5 100644 (file)
@@ -33,6 +33,7 @@ using namespace OC;
 
 std::shared_ptr<OCResource> curResource;
 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+std::mutex curResourceLock;
 
 class Light
 {
@@ -272,9 +273,11 @@ void getLightRepresentation(std::shared_ptr<OCResource> resource)
 // Callback to found resources
 void foundResource(std::shared_ptr<OCResource> resource)
 {
+    std::lock_guard<std::mutex> lock(curResourceLock);
     if(curResource)
     {
         std::cout << "Found another resource, ignoring"<<std::endl;
+        return;
     }
 
     std::string resourceURI;