Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

multithreading - Sequential Multi-Threading

I wanna build Multi-Threaded procedure for the following code, but not able to correctly build it. I tried using Semaphore to sequentially complete the code but it only executed the myStream2.CopyFrom(myStream1, StrToInt64('$' + SL1[(i - 1)])); part which comes under else part. The problem presents is that if we restore those streams into output filestream (mystream2) haphazardly, the file will corrupt.

Can anyone help me please.

  for i := 1 to SL1.Count do
  begin
    if Length(SL1[i - 1]) > 12 then
    begin
      TempDir := SrcDir + 'temp_' +
              IntToHex(Random(2147483647) + Random(2147483647), 8) + '';
      ForceDirectories(TempDir);
      for k := 1 to 2 do
      begin
        tempfile := TempDir + IntToHex((i - 1), 8) + '.tmp' + IntToStr(k);
        fs := TFileStream.Create(tempfile, fmCreate or fmShareExclusive);
        fs.CopyFrom(myStream1, StrToInt64('$' + DecodeRefStr(SL1[(i - 1)])[k]));
        fs.Free;
      end;
      ExecAndWait(SrcDir + 'process.exe', Method + ' ' + '"' + TempDir +
                  IntToHex((i - 1), 8) + '.tmp1' + '" "' + TempDir +
                  IntToHex((i - 1), 8) + '.tmp2' + '" "' + TempDir +
                  IntToHex((i - 1), 8) + '.tmp4' + '"', TempDir, 
                  True,  True);
      tempfile := TempDir + IntToHex((i - 1), 8) + '.tmp4';
      fs := TFileStream.Create(tempfile, fmOpenRead);
      myStream2.CopyFrom(fs, 0);
      fs.Free;
      TDirectory.Delete(TempDir, True);
    end
    else
      myStream2.CopyFrom(myStream1, StrToInt64('$' + SL1[(i - 1)]));
    end;
    myStream1.Free;
    myStream2.Free;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...