Delphi中多线程访问同一个TStringList的问题
时间:2011-12-07
来源:互联网
作者: xiaoyue_a 发布时间: 2011-12-07
你不用tstinglist,用开辟内存的方法,然后按比例分给5个线程应该就可以了。
作者: sundayzhao 发布时间: 2011-12-07
作者: xiaoyue_a 发布时间: 2011-12-07
作者: kiboisme 发布时间: 2011-12-07
作者: funxu 发布时间: 2011-12-07
i := m_startlineno;
cindex := 0;
while i <= m_endlineno do
begin
cstr := Frm3d.gridfilelist[i];
cstr := Trim(cstr);
while cstr <> '' do
begin
cpos := pos(splitor, cstr);
if cpos = 0 then
cpartstr := cstr
else
cpartstr := copy(cstr, 1, cpos - 1);
cvalue := StrToFloatDef(cpartstr,0) ;
Frm3d.arraylist[m_arrayindex,cindex] := cvalue;
cstr := copy(cstr, Length(cpartstr) + Length(splitor) + 1, Length(cstr) - Length(splitor) -1);
cstr := TrimLeft(cstr);
Inc(cindex);
end; // while
Inc(i);
end; //while;
Frm3d.threadstatus[m_arrayindex] := 1;//用来标记线程是否结束
作者: xiaoyue_a 发布时间: 2011-12-07
m_arrayindex to m_endlineno
如果线程序同时执行,每个线程都是从0 to m_endlineno,全部重复执行了
修改一下即可
while True do begin
i := InterlockedIncrement(m_startlineno);
if i > m_endlineno then Break;
//Inc(i); 这行去掉
end; //while;
而你的cindex没看出它的具体作用,因此这样改写是否正确,需要测试,
作者: kiboisme 发布时间: 2011-12-07
作者: sz_haitao 发布时间: 2011-12-07
作者: kiboisme 发布时间: 2011-12-07
作者: xiaoyue_a 发布时间: 2011-12-07
需要看一下,你规划线程范围以及创建线程那里的代码了.
作者: kiboisme 发布时间: 2011-12-07
gridfilelist := TStringList.Create;
gridfilelist.LoadFromFile(datafile);
linecount := gridfilelist.Count;
threadlinecount := Trunc(linecount / 5);
lastthreadlinecount := linecount - threadlinecount * 4;
threadstatus[0] := 0;
threadstatus[1] := 0;
threadstatus[2] := 0;
threadstatus[3] := 0;
threadstatus[4] := 0;
btime := Now;
if threadlinecount >= lastthreadlinecount then
SetLength(arraylist,5, threadlinecount * 8)
else
SetLength(arraylist,5, lastthreadlinecount * 8);
TReadTStringList.Create(0,0,threadlinecount);
TReadTStringList.Create(1,threadlinecount+1,2 * threadlinecount);
TReadTStringList.Create(2,2 * threadlinecount + 1, 3* threadlinecount);
TReadTStringList.Create(3,3 * threadlinecount + 1, 4* threadlinecount);
TReadTStringList.Create(4,4 * threadlinecount + 1, linecount-1);
endthreadcount := 0;
while endthreadcount < 5 do
begin
endthreadcount := 0;
for i := 0 to 4 do
begin
if threadstatus[i] = 1 then
endthreadcount := endthreadcount + 1;
end;
Application.ProcessMessages;
end;
gridfilelist.Free;
ctime := Now;
Showmessage(FloatToStr( MilliSecondsBetween(btime, ctime) / 1000));
作者: xiaoyue_a 发布时间: 2011-12-07
第一个参数是标记第几个线程,第二参数就是m_startlineno,第三个参数是m_endlineno
作者: xiaoyue_a 发布时间: 2011-12-07
Application.ProcessMessages;
end;
顺便提醒一下,你的线程属于纯计算线程,中间没有需要等待的代码执行,因此线程数量最好和CPU的核一样多,才能发挥最大效率
作者: kiboisme 发布时间: 2011-12-07
作者: xiaoyue_a 发布时间: 2011-12-07
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28