Double buffering - switching buffers

Jul 9, 2010 at 9:36am
Hi,

I have written a program which reads data in blocks from a hard disk and processes the blocks looking for certain data.
In order to try and increase the speed I have changed to overlapped I/O, however I am struggling to 'switch' the buffer's after the reads.
So if I have 2 buffers Current and Next i would want to do the following:
Read data into Current
Start reading data into Next
Process data in Current
Check Next has read
Change Next to Current and Vice versa
Read into next
Process Data
......

Thanks in advance for any help

Has anyone got any ideas on how to switch the buffers?
Jul 9, 2010 at 12:26pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
current points to buffer A
next points to buffer B

begin next read into current
begin next read into next

loop
{
	wait for current to become ready
	process current
	if (done)
		break;

	begin next read into current
	swap current and next pointers
}
Jul 9, 2010 at 12:38pm
Thanks KBW, I have the rest of the code set up ready, but if I have:

unsigned char buffer1[4096], buffer2[4096];

How would I change them after so that buffer1 actually points to buffer2 and vice versa?
Would it be buffer2 = &buffer1?
Tried a few things but couldnt get it to work
Last edited on Jul 9, 2010 at 1:23pm
Jul 9, 2010 at 1:25pm
You don't change the buffers at all, you maintain seperate pointers to these buffers.

e.g.
1
2
3
4
    unsigned char buffer1[4096], buffer2[4096];

    unsigned char* current = buffer1;
    unsigned char* next = buffer;


And to swap them:
std::swap(current, next);
Last edited on Jul 9, 2010 at 1:26pm
Jul 9, 2010 at 1:29pm
Thats great thanks!
Topic archived. No new replies allowed.