|
-
March 20th, 2013, 11:54 AM
#29
Re: Processing Time For PVOID Into Byte Array Conversion
Er... No. queue needs a type of data that is to be stored. PVOID is not a suitable type as this is just a pointer to void.
I've knocked up some code to demonstrate the idea between a producer thread and a consumer thread for you. Its just win32 not MFC but you should get the idea about using a queue and sync between the threads. I don't have a camera to test so I don't know the timings for loss of data - but if this sort of program design still looses data from the camera then you really are in trouble as all the producer is doing is adding blocks of data to the queue.
Hope this helps
Code:
#include <iostream>
#include <queue>
#include <conio.h>
using namespace std;
#include <windows.h>
#include <process.h>
#define BUFSIZE 2048
//Structure with methods for camera data blocks
struct scam {
char* pdata;
scam()
{
pdata = (char*)new char[BUFSIZE];
memset(pdata, 0, BUFSIZE);
}
scam (const char* dat)
{
size_t ll = strlen(dat);
if (ll > BUFSIZE - 1) {
ll = BUFSIZE - 1;
}
pdata = (char*)new char[BUFSIZE];
memset(pdata, 0, BUFSIZE);
memcpy(pdata, dat, ll);
pdata[ll + 1] = 0;
}
scam(const char *dat, int no)
{
pdata = (char*)new char[BUFSIZE];
memcpy(pdata, dat, min(no, BUFSIZE));
}
~scam()
{
delete [] pdata;
}
scam(const scam& scm)
{
pdata = (char*)new char[BUFSIZE];
memcpy(pdata, scm.pdata, BUFSIZE);
}
scam& operator=(const scam& scm)
{
if (this != &scm) {
delete [] pdata;
pdata = (char*)new char[BUFSIZE];
memcpy(pdata, scm.pdata, BUFSIZE);
}
return *this;
}
void SetData(const char *dat, int no)
{
memset(pdata, 0, BUFSIZE);
memcpy(pdata, dat, min(no, BUFSIZE));
}
};
CRITICAL_SECTION ccrit;
HANDLE sync;
queue<scam> camdata;
//CONSUMER
unsigned __stdcall consume(void* pArguments)
{
scam dataOut;
while (true) {
dataOut = "";
WaitForSingleObject(sync, INFINITE);
EnterCriticalSection(&ccrit);
dataOut = camdata.front();
camdata.pop();
if (!camdata.empty()) {
SetEvent(sync);
}
LeaveCriticalSection(&ccrit);
//PROCESS CAMERA DATA BLOCK HERE
cout << dataOut.pdata << endl;
}
return 0;
}
int main()
{
unsigned thid;
sync = CreateEvent(NULL, FALSE, FALSE, "camev");
InitializeCriticalSection(&ccrit);
_beginthreadex(NULL, 0, &consume, NULL, 0, &thid);
//PRODUCER
scam dataIn1;
//test loop - would be 'while there is data from camera'
for (int i = 1; i < 50; i++) {
EnterCriticalSection(&ccrit);
//ADD DATA BLOCK HERE FROM CAMERA
dataIn1.SetData("dfdgrehgfdhghghgdhgdhghngfegfrzz", 32);
camdata.push(dataIn1);
SetEvent(sync);
LeaveCriticalSection(&ccrit);
}
_getch();
DeleteCriticalSection(&ccrit);
return (0);
}
Last edited by 2kaud; March 20th, 2013 at 04:35 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|