解決した
bfs::directory_iterator キューを std::string キューに変更したところ、驚くべきことに問題が解決しました。
こんにちは、私は自分が間違ったことをしているという直感を持っています。
スレッド プール パターンを実装しました (または試みました)。
N 個のスレッドがキューから読み取られましたが、問題が発生しています。これが私が得たものです:
//inside a while loop
bool isEmpty;
bfs::directory_iterator elem;
{
boost::mutex::scoped_lock lock(this->queue_mutex);
isEmpty = this->input_queue.isEmpty();
if (!isEmpty){
elem= *(this->input_queue.pop());
}
else{
continue;
}
}
scoped_lock は引き続き if の本体内で機能しますか? 私はそれがないと信じ始めています(多くのテストを実行した後)。そうでない場合、これを行うスコープ付きの方法はありますか(つまり、明示的なロック解除方法ではありません)
前もって感謝します。
アップデート
要素をキューに追加するコードは次のようになります
//launches the above code, passing a reference to mutex and queue.
Threads threads(queue,queue_mutex);
for (bfs::directory_iterator dir_it:every file in directory){
boost::mutex::scoped_lock lock(queue_mutex);
queue.push(dir_it);
}
ポップされたファイル名を制御するために cout を配置します。2 つのファイル (file1) と (file2) をプッシュし、2 つのスレッドを使用すると、両方の "file2" が取得されます。
class Threads{
boost::thread::thread_group group;
Thread (N){
//also asigns a reference to a queue and a mutex.
for(i 1..N){
//loop is posted above.
group.add(new boost::thread(boost::bind(&loop,this)));
}
}
};