On Wed, Jul 3, 2019 at 10:11 AM Sean Conner wrote:
It was thus said that the Great Egor Skriptunoff once stated:
> Another possible situation - you are reading multi-volume archive.
> Only one volume file is opened at any given time.
> To switch a volume, you close the current volume file and open the next
> volume file.
> Single to-be-closed variable is enough to guarantee that the last opened
> volume file will be closed on eviction.
The following worked:
for i = 1 , 10 do
local <toclose> f = io.open(string.format("volume.%d",i),"w")
f:write(tostring(i),"\n")
end
Only one file is open at a time. So there is a way to kind of re-use a
closed variable.
Now, the following kept the files open unil the end:
local function process(i)
if i < 10 do
local <toclose> f = io.open(string.format("volume.%d",i),"w")
return process(i+1)
end
end
This is OK while you can organize your code in the following loop:
while (file_j_exists) do
process_next_file(j)
j = j + 1
end
But sometimes the logic is complex enough so that you can't simply write the function "process next file".
It might happen that your code has to be a consumer: it needs switching volumes on-the-fly without interrupting main execution stream.
It's a "producer-consumer problem".