By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

how to make a code thread safe?

P: n/a
Hi,

How do I make this code thread safe?
Does returning std::string cause problems of data-race in a multithreading code? Why?

Expand|Select|Wrap|Line Numbers
  1. #include<map>
  2. #include<string>
  3. #include <exception>
  4. #include <stdexcept> 
  5.  
  6.  
  7. class SSTable {
  8.  private:
  9.     typedef std::map<std::string, std::string> true_love;
  10.     true_love lovers;
  11.  
  12.  public:
  13.    class Exception : public std::exception {
  14.     private:
  15.         std::string love_declaration;
  16.  
  17.     public:    
  18.         Exception(const std::string& reason):love_declaration((std::string) "Exception: " + reason + " does not exist"){ };
  19.         virtual const char* what() const throw(){
  20.             return love_declaration.c_str();
  21.         }
  22.         virtual ~Exception() throw(){}; // specify destructor, otherwise it destructor throws anything
  23.   };
  24.  
  25.   SSTable();
  26.  
  27.   // Returns true if the table contains an entry for "key"
  28.   bool contains(const std::string& key);
  29.  
  30.   // Inserts an entry into the table that maps "key" to "value"
  31.   // If there is already an entry for "key", an Exception is thrown
  32.   void put(const std::string& key, const std::string& value);
  33.  
  34.   // Retrieves the value associated with the give key.
  35.   // If no entry to "key" exists in the table, an Exception is thrown
  36.   std::string get(const std::string& key);
  37.  
  38.   // Removes the entry for "key" from the table
  39.   // If no such entry exists, an exception is thrown*/
  40.   void remove(const std::string& key);
  41. };
  42.  
  43.  
  44.  
  45. #include "sstable.h"
  46. #include<iostream>
  47.  
  48. SSTable::SSTable() {
  49.     true_love secret_lovers;
  50.     secret_lovers = lovers;
  51. }
  52.  
  53. bool SSTable::contains(const std::string& key){
  54.  
  55.     if(lovers.find(key) == lovers.end()) { //not found
  56.         std::cout<<"false"<<std::endl; 
  57.         return false;
  58.         }
  59.     else {
  60.         std::cout<<"true"<<std::endl; 
  61.         return true;
  62.         }
  63. }
  64. void SSTable::put(const std::string& key, const std::string& value){
  65.     lovers.insert(make_pair(key, value));
  66. }
  67.  
  68. std::string SSTable::get(const std::string& key){
  69.     if(lovers.find(key) == lovers.end()){ //if not found, throw exception
  70.         try{
  71.             throw Exception(key);
  72.             }
  73.         catch (Exception& e){ // catch by reference
  74.             std::cout <<e.what() << std::endl; 
  75.         }
  76.     }    
  77.     else{
  78.         std::cout<<lovers[key]<<std::endl;
  79.         return lovers[key];
  80.         }
  81. }
  82.  
  83. void SSTable::remove(const std::string& key){
  84.     lovers.erase(key);
  85. }
  86.  
  87.  
Oct 23 '10 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Nothing in the STL is thread safe since multithreading varies by operating system.

You would need to establish a critical section and do your work in there.

Any use of data between two threads (like doing a return or something like var++) is not thread safe.

Are you using Windows? If so, there is a complete discussion of threading issues in Windows via C/C++ by Jeffrey Richter.
Oct 23 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.