0

私は学校のプロジェクトに取り組んでいますが、クラスでスレッドを機能させるのに問題があります。スレッドが作業を終了したときにポインタを true に設定するスレッドを持つ 1 つのクラス (ダウンロード) とatomic<bool>、独自のスレッドで実行されている別のクラスがあり、ダウンロードのステータスが完了したことを確認すると、削除する必要があります。そのリストからそれを。クラスが現在のように設定されていると、ポインターのいずれかのストアは、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

私が間違っていることを見つけることができるより多くの経験を持つ人はいますか?

4

1 に答える 1