我正在做一个学校项目,但在我的课程中使用线程时遇到了麻烦。我有一个类(下载),它有一个线程,atomic<bool>
当线程完成工作时将指针设置为 true,另一个类在自己的线程上运行,当该类看到下载的状态已完成时,它应该删除它从它的列表中。像现在这样设置类后,我指向的任何指针上的任何存储都atomic<bool>
将仅更新最近创建的对象,以便仅将列表的末尾设置为完成。这是代码...
class Download
{
private:
Show *showPtr;
int season;
int episode;
int downloadTime;
atomic<bool> complete;
thread downloadThread;
// Edit: Hans Passant's solution to compiler error problems -
Download(const Download&);
Download& operator=(const Download&);
public:
Download(Show * showPtr);
string status(bool &complete);
bool operator==(const Download &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->getNumberDownloaded() / showPtr->getNumberOfEpisodesPerSeason() + 1;
episode = showPtr->getNumberDownloaded() - showPtr->getNumberOfEpisodesPerSeason() * (season - 1) + 1;
showPtr->incrementDownloaded();
// Download the episode and return control.
downloadThread = thread(blockThenSetComplete, this);
}
string Download::status(bool & complete)
{
complete = this->complete.load();
stringstream ss;
ss << showPtr->getTitle() << " S" << season << "E" << episode;
return ss.str();
}
void blockThenSetComplete(Download *dl)
{
this_thread::sleep_for(chrono::seconds(dl->downloadTime));
dl->complete.store(true);
}
bool Download::operator==(const Download &other) const
{
if (other.showPtr == showPtr &&
other.season == season &&
other.episode == episode)
return true;
else
return false;
}
还有下载管理器...
// 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->shows.size() != 0)
{
// connections available
if (model->account.getTotalConnectionCount() > model->account.getTotalActiveCount())
{
// check each show
for (list<Show>::iterator showIt = model->shows.begin(); showIt != model->shows.end(); showIt++)
{
// find a show that needs a download
if (~((*showIt).allDownloading()))
{
// must check connections again
if (model->account.getTotalConnectionCount() > model->account.getTotalActiveCount())
{
// Reserve connection and add to list.
model->account.reserveConnection();
model->downloads.push_back(Download(&(*showIt)));
callNotify = true;
}
}
}
}
}
// monitoring downloads
if (model->downloads.size() != 0)
{
// check each download
for (list<Download>::iterator downIt = model->downloads.begin(); downIt != model->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->downloads.remove(*downIt);
model->account.releaseConnection();
callNotify = true;
break;
}
}
}
if (callNotify)
model->notify();
// Check periodically.
this_thread::sleep_for(chrono::seconds(10));
}
}
我知道指针没有析构函数,它在线程因某种原因被启动并让程序保释后立即被调用。我使用指针的原因是我似乎无法将线程或原子作为数据成员。现在这可能是我问题的真正根源。如果我通过并更改指向这些类的实例的指针,我会收到以下错误:
Error 3 error C2248: 'std::atomic<bool>::atomic' : cannot access private member declared in class 'std::atomic<bool>' 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
任何有更多经验的人能够找出我做错了什么?