[英]Intel TBB, parallel_pipeline, error when I pass const char* between stages
我有以下管道:
parallel_pipeline(20,
make_filter<void,const char*>(
filter::serial,
[&](flow_control& fc)->const char*
{
getline(fe,orden_fichero);
if(fe.eof())
{
fc.stop(); // stop processing
return nullptr;
}
else
{
return orden_fichero.c_str();
}
}
) &
make_filter<const char*,void>(
filter::parallel,
[&](const char* p){
string orden(p);
}
)
);
以前,當我使用string而不是const char*
,並且第一個返回結果是orden_fichero
而沒有轉換為const char*
,該程序運行良好,但速度很慢。 我已經看到使用const char*
可以改善程序的性能,但是通過這種方式,某些管線在管道的第二階段沒有被正確接收(有些管線缺少字符),例如在10M中行文件,對應於orden字符串的26行,大小為0,9,大小在2到35之間,其余所有字符均正確接收(order變量具有36個或更多字符)
我如何才能正確地從firts管道的第二個介紹中獲取const char *?
有某種方法可以讀取Intel TBB中的文件,以便在每一行上並行執行操作,並將結果以比我嘗試執行的更好的方式保存到向量中?
問題是您首先返回的是orden_fichero
的副本(我猜是std :: string)。 使用當前代碼,您將返回一個指向orden_fichero
的內部數據的orden_fichero
,因此,當線程1(第一個管道階段)從文件中讀取一行並將其存儲在orden_fichero
中時,從線程2(第二個線程)讀取的數據已損壞。管道階段)。
如何正確地返回如圖C字符串的一些方法在這里 (尤其是我建議第2版)。 但是同樣,這將分配大量內存,並且很可能幾乎與返回std :: string一樣低效。
您可以嘗試在parallel_pipeline
之外創建一個新字符串,將orden_fichero的c_str
復制到該字符串中,然后將指針返回到該字符串。 但是我不確定這是否可以正常工作...
std::string another_string;
parallel_pipeline(20,
make_filter<void,const char*>(
filter::serial,
[&](flow_control& fc)->const char*
{
getline(fe,orden_fichero);
if(fe.eof())
{
fc.stop(); // stop processing
return nullptr;
}
else
{
another_string = orden_fichero;
return another_string.c_str();
}
}
) &
make_filter<const char*,void>(
filter::parallel,
[&](const char* p){
string orden(p);
}
)
);
因此,在通過getline
數據讀取到orden_fichero
同時,可以安全地使用another_string
。 但是正如我所說,我不確定這是否會正常工作,因為在將const char* p
復制到orden
之前,第二個管道階段的線程可能會被搶占,而第一階段的線程可能會覆蓋another_string
並使orden
p
。 您可以嘗試使用互斥鎖保護another_string
,但是我想這會再次放慢速度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.