能求到office max的reciept吗?# PennySaver - 省钱一族
y*k
1 楼
【 以下文字转载自 Programming 讨论区 】
发信人: ystdpk (ystdpk), 信区: Programming
标 题: c++ thread 求助
发信站: BBS 未名空间站 (Fri Jan 31 21:12:20 2014, 美东)
有一个50 million 行的文本。每行四列数字。
要求每行求一个特殊函数的值,把此值输出为第五列。次函数的求值所花的时间可能很
快,如果四个数比较小, 也可能要花到10几到100倍的时间如果数比较大。
想用36个boost thread。
有没有人能提供一个boost thread 的代码。谢谢了。
我写了一个下面的,但是cpu浪费比较严重,有的thread 结束早,有的结束慢。而且不
知道2000行一个thread是不是好的选择。
int batchSize = 2000;
int num_of_lines = 50100100;
bool end_of_file = false;
int pstart = 0;
boost::thread_group io;
while( ! end_of_file )
{
boost::thread_group g;
vector> out;
out.resize(num_of_threads);
for( int i = 0; i < num_of_threads; )
{
if(num_of_lines - pstart < BATCHMAX)
{
batchSize = num_of_lines - pstart;
boost::thread *tp = new boost::thread( worker, batchSize
, boost::ref(out[i]) );
g.add_thread(tp);
pstart = pstart + batchSize;
i++;
end_of_file = true;
break;
}
else
{
boost::thread *tp = new boost::thread( worker, batchSize
, boost::ref(out[i]) );
g.add_thread(tp);
pstart = pstart + batchSize;
i++;
}
}
g.join_all();
io.join_all();
boost::thread *tprint = new boost::thread( appendToFile, boost::ref(File
), out);
io.add_thread(tprint);
}
发信人: ystdpk (ystdpk), 信区: Programming
标 题: c++ thread 求助
发信站: BBS 未名空间站 (Fri Jan 31 21:12:20 2014, 美东)
有一个50 million 行的文本。每行四列数字。
要求每行求一个特殊函数的值,把此值输出为第五列。次函数的求值所花的时间可能很
快,如果四个数比较小, 也可能要花到10几到100倍的时间如果数比较大。
想用36个boost thread。
有没有人能提供一个boost thread 的代码。谢谢了。
我写了一个下面的,但是cpu浪费比较严重,有的thread 结束早,有的结束慢。而且不
知道2000行一个thread是不是好的选择。
int batchSize = 2000;
int num_of_lines = 50100100;
bool end_of_file = false;
int pstart = 0;
boost::thread_group io;
while( ! end_of_file )
{
boost::thread_group g;
vector
out.resize(num_of_threads);
for( int i = 0; i < num_of_threads; )
{
if(num_of_lines - pstart < BATCHMAX)
{
batchSize = num_of_lines - pstart;
boost::thread *tp = new boost::thread( worker, batchSize
, boost::ref(out[i]) );
g.add_thread(tp);
pstart = pstart + batchSize;
i++;
end_of_file = true;
break;
}
else
{
boost::thread *tp = new boost::thread( worker, batchSize
, boost::ref(out[i]) );
g.add_thread(tp);
pstart = pstart + batchSize;
i++;
}
}
g.join_all();
io.join_all();
boost::thread *tprint = new boost::thread( appendToFile, boost::ref(File
), out);
io.add_thread(tprint);
}