I currently have two threads running in my program:
-
Main thread – Grabs image from a webcam, stored in a CVD image. Does processing on this image.
-
Server thread – sends the full image data stored in the above CVD image to its clients using named pipes.
When I run my program it works for a very short while before crashing with the following exception:
0xC000005: Access violation reading location 0x0000000
Which I assume is because my server thread is attempting to access the image at the same time as the main thread.
I haven’t done any concurrent programming before (this is my first time) but I have a vague idea about how to solve it at the moment.
My plan is to have some sort of lock that prevents access to the image from the main thread when the server is preparing to send it to the client. However I realised there might be a problem where the server thread constantly keeps the resource as the client is constantly requesting a new frame. So I am thinking to only respond to the client whenever a new frame is grabbed from the webcam to avoid the blocking issue above.
To sum this up:
Main thread:
1. If Image is available
then - Lock image, copy over new data from webcam, release image
else - goto 1
2. Do processing
Server:
1. Receive request for new frame from client
2. If (haven't sent the current frame yet)
then - Lock CVD image access, send over frame, release image.
else - wait until new image available.
3. goto 1
My question is, would this be a suitable solution? and what do I need in order to implement this? i.e. how do I stop execution of certain parts of my code whilst another thread is executing a part of its own code.
Some more info:
- I am using VS2010 C++
- The client is in C# and there is only 1 client.
- I am accessing the image data from the CVD image using image[x][y] which returns a byte value representing the intensity of the image.
- There is a copyTo() function available with the CVD image. It seems to do a memory copy of the image to create a new object with the same data. Would this be useful?
- I cannot run the program in debug mode because I am working off an existing codebase with no debug mode set.
I would use a circular buffer so I could be reading one frame while writing a different one to clients, assuming you don’t want to drop frames.
Look at http://msdn.microsoft.com/en-us/library/windows/desktop/ms682530(v=vs.85).aspx for info on Windows Critical Sections.
Finally, if you have the existing code, why can’t you turn debug info on and rebuild? Otherwise you’re shooting in the dark trying to find the cause of this crash.