我改變了BFS :: directory_iterator隊列以一個std ::串隊列,並且令人驚訝地解決了這個問題。boost scoped_lock。這個鎖嗎?
嗨,我有一種直覺,認爲我做錯了事。
我已經實現(或試圖)線程池模式。
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來控制POP操作的文件名,並且如果我推2個文件(文件1)和(文件2) ,並使用2個線程,我得到兩個「文件2」。
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)));
}
}
};
代碼似乎罰款。您的程序以何種方式表現不正確? – avakar 2009-09-23 19:18:28
您發佈的代碼的確看起來不錯。如果你問:「這是否會使用更窄範圍的鎖(在'if'中)?」,請澄清一下。 – Novelocrat 2009-09-23 19:24:37
鎖定真的看起來不錯 - 你是否正確地得到了「'file1'」和「'file2'」,如果你像上面那樣填充隊列,但是讀取它是非線程的(即按順序)? – hjhill 2009-09-23 20:49:01