Re: Best solution for thread safe data in loop.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Mon, 30 Jun 2008 02:01:01 -0700, Fabian
<Fabian@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hi,

the image acquisition software I am writing has a recording loop. For
hardware synchronity and speed reasons I want to avoid mutexes etc. as much
as possible. Still parameters (see the info variable in the very much
simplified pseudo code example below) must be changeable during runtime
without crashing the program. I can think of two solutions for which I don't
know the performance penalties:

- Make a copy of the info struct at the beginning of every loop run.
- Using win32-API event handles to block the access to the info variable.

More important than the actual speed is a constant execution time to
minimize jitter. It may well be a few milliseconds slower if it's
reproducible.

Can you tell me which solution is less time consuming? Do you have an
alternative suggestion? Should I - if I went for the struct-copy-solution -
just put it on the stack at the beginning of the loop à la

struct info2 = info;

or use a (auto)pointer and allocating it anew every run?

<simplified (peudo) code snippet>

struct UnmanagedBinAndRoiInfo;

UnmanagedBinAndRoiInfo info;
int bytesPerPixel = 2; // depends on hardware initialization.

void getBuffer(void** pImageData)
{
// done with different hardware APIs
}

void copyBuffer(const void* pInput, const int Size, void* pOutput)
{
// done with Intel IPP library
}

void freeImageData(const void* pInput)
{
// done with Intel IPP library
}

while (!stopped)
{
void* pImageData;
void* pStartForCopy;
void* pCopiedData;

// record image
getBuffer(&pImageData)
// calculate pointer to start pixel
pStartForCopy = pImageData +
(info.Top * info.Width + info.Left) * bytesPerPixel ;
int arraySize = (info.Width + info.Height)* bytesPerPixel ;

// copy a selected sub region out of full frame image data
copyBuffer(pStartForCopy, arraySize, pCopiedData);

// save TIFF image from sub region data (using libTIFF)
...

// display sub region image on screen
...
}

</simplified (peudo) code snippet>

Where the UnmanagedBinAndRoiInfo struct is defined as follows:

<snippet 2>
struct UnmanagedBinAndRoiInfo
{
bool RoiEnabled;
unsigned int Top;
unsigned int Left;
unsigned int Width;
unsigned int Height;

bool SoftwareBinEnabled;
unsigned int SoftwareBinFactor;
bool HardwareBinEnabled;
unsigned int HardwareBinFactor;

bool ZoomEnabled;
double ZoomFactor;
};
</snippet 2>

Thanks a lot in advance,

Fabian


I'm not sure if this is a good solution however I was wondering about
your general design. Apparently you have a very tight thread running
doing the copying however would the thread not be well served by
having a message loop as well, that way you could post the changes to
the thread without needing copy them every loop iteration and without
having any thread problems.If the values do not change so often this
may be a better approach.

Just my 2c without knowing all your parameters :).
.



Relevant Pages