how to make a multithread copy files

后端 未结 3 469
刺人心
刺人心 2021-01-27 16:37

I want to copy many files in one, but using multiThread,supposing that file A is the file in which different threads copy datas, in this case each thread is meant to copy one fi

3条回答
  •  后悔当初
    2021-01-27 17:08

    As it was mentioned already writing to same file from multiple threads is not so good idea.

    If you try doing it in a way that multiple threads share same file handle you end up with big problem of making sure that one thread doesent move file position using Seek command while other one is trying to write some data.

    If you try doing it in a way that each thread creates its own handle to the file then you end up with the problem that OS doesen't generally alow having multiple file handles with writing capability simuntaniously as this can be recipie for disaster (data coruption).

    Now even if you somehow manage to get this working so that each tread is writing in its own section of the file and that they are not messing with each other you will still be losing some performance due to hard drive limitation (HDD head needs to be repositioned into corect place - lots of back and forth movment).

    Hey but you could use miltiple threads to go and prepare the final file inside your memory before it is being written on your hard drive. This can be easily done since memory acces is so fast that you practically don't lose any pefrormance by jumping back and forth. The only problem with this is that you could quickly run out of memory if you are concating several larger files.

    EDIT: BTW if you are interested I could share a code example of two threaded double-buffered file copy example that I made several years ago. Note it does not provide any data verification capabilities as it was only written to test a theory or shoud I say break a theory that it isn't possible to copy a file only with Delphi (without uising file copy API from Windows). When doing file copy on same HDD it is a bit slower than built in Windows routine but when copying from one HDD to another it reaches same speed as windows built in routines.

提交回复
热议问题