title: C++11 atomics, store only affects most recently created object
body: <p>I'm working on a school project and I'm having trouble getting threading working with my classes. I have one class (Download) that has a thread that sets a pointer to an <code>atomic&lt;bool&gt;</code> true when the thread finishes work, and another class that is running on its own thread and when that one sees the status of a Download is complete, it should remove it from its list. With the class set up as it is now, any stores on any of my pointers to <code>atomic&lt;bool&gt;</code> will update the most recently created object only, so that only the end of the list is getting set to complete. Here's the code...</p>
  
  <pre><code>class Download
  {
  private:
      Show *showPtr;
      int season;
      int episode;
      int downloadTime;
      atomic&lt;bool&gt; complete;
      thread downloadThread;
      // Edit: Hans Passant's solution to compiler error problems -
      Download(const Download&amp;);
      Download&amp; operator=(const Download&amp;);
  public:
      Download(Show * showPtr);
      string status(bool &amp;complete);
      bool operator==(const Download &amp;other) const;
      friend void blockThenSetComplete(Download *dl);
  };
  
  
  Download::Download(Show * showPtr)
      : showPtr(showPtr)
  {
      complete.store(false);
      // Randomly pick a download time from 20 - 30 seconds.
      downloadTime = rand() % 11 + 20;
      // Track which episode this thread is downloading.
      season = showPtr-&gt;getNumberDownloaded() / showPtr-&gt;getNumberOfEpisodesPerSeason() + 1;
      episode = showPtr-&gt;getNumberDownloaded() - showPtr-&gt;getNumberOfEpisodesPerSeason() * (season - 1) + 1;
      showPtr-&gt;incrementDownloaded();
      // Download the episode and return control.
      downloadThread = thread(blockThenSetComplete, this);
  }
  
  string Download::status(bool &amp; complete)
  {
      complete = this-&gt;complete.load();
      stringstream ss;
      ss &lt;&lt; showPtr-&gt;getTitle() &lt;&lt; " S" &lt;&lt; season &lt;&lt; "E" &lt;&lt; episode;
      return ss.str();
  }
  
  void blockThenSetComplete(Download *dl)
  {
      this_thread::sleep_for(chrono::seconds(dl-&gt;downloadTime));
      dl-&gt;complete.store(true);
  }
  
  bool Download::operator==(const Download &amp;other) const
  {
      if (other.showPtr == showPtr &amp;&amp;
          other.season == season &amp;&amp;
          other.episode == episode)
          return true;
      else
          return false;
  }
  </code></pre>
  
  <p>And the download manager...</p>
  
  <pre><code>// Ran as a thread, will fire up or tear down downloads as appropriate.
  void downloadManager(Model *model)
  {
      while(true)
      {
          bool callNotify = false;
          // monitoring shows
          if (model-&gt;shows.size() != 0)
          {
              // connections available
              if (model-&gt;account.getTotalConnectionCount() &gt; model-&gt;account.getTotalActiveCount())
              {
                  // check each show
                  for (list&lt;Show&gt;::iterator showIt = model-&gt;shows.begin(); showIt != model-&gt;shows.end(); showIt++)
                  {
                      // find a show that needs a download
                      if (~((*showIt).allDownloading()))
                      {
                          // must check connections again
                          if (model-&gt;account.getTotalConnectionCount() &gt; model-&gt;account.getTotalActiveCount())
                          {
                              // Reserve connection and add to list.
                              model-&gt;account.reserveConnection();
                              model-&gt;downloads.push_back(Download(&amp;(*showIt)));
                              callNotify = true;
                          }
                      }
                  }
              }
          }
          // monitoring downloads
          if (model-&gt;downloads.size() != 0)
          {
              // check each download
              for (list&lt;Download&gt;::iterator downIt = model-&gt;downloads.begin(); downIt != model-&gt;downloads.end(); downIt++)
              {
                  // find a finished download
                  bool finished = false;
                  (*downIt).status(finished);
                  if (finished)
                  {
                      // Remove from list, release connection, break as iterators are now invalid
                      model-&gt;downloads.remove(*downIt);
                      model-&gt;account.releaseConnection();
                      callNotify = true;
                      break;
                  }
              }
          }
  
          if (callNotify)
              model-&gt;notify();
          // Check periodically.
          this_thread::sleep_for(chrono::seconds(10));
      }
  }
  </code></pre>
  
  <p>I know there's no destructor for the pointers, that was getting called immediately after the thread was set off for some reason and making the program bail. The reason I'm using pointers is I can't seem to have threads or atomics as data members. Now that might be the real source of my problem. If I go through and change the pointers to instances of those classes, I get these errors:</p>
  
  <pre><code>Error   3   error C2248: 'std::atomic&lt;bool&gt;::atomic' : cannot access private member declared in class 'std::atomic&lt;bool&gt;'   e:\users\robert\documents\visual studio 2012\projects\pvrsim\pvrsim\download.h  36  1   PVRsim
  Error   4   error C2248: 'std::thread::thread' : cannot access private member declared in class 'std::thread'   e:\users\robert\documents\visual studio 2012\projects\pvrsim\pvrsim\download.h  36  1   PVRsim
  </code></pre>
  
  <p>ANyone with more experience able to pick out what I'm doing wrong?</p>