i have a problem with using a queue of vector objects. Basically all i do is create a new object, put it in the queue and right after that try to read it.
void ProcessData::getData(CharVectorPtr& buffer, std::mutex* inBufferMutex){
std::vector<char16_t> temp;
printf("processing activ\n");
while(1){
if(buffer->size()>HEADERSIZE){ //check if there is data in the message ( more than just the header )
inBufferMutex->lock();
workBufferMutex->lock();
temp.insert(temp.begin(),buffer->begin(),buffer->end());
workBuffer.push(std::vector<char16_t>(temp)); //copy data from buffer
printf("size %d\n",workBuffer.size());
buffer->clear();
workBufferMutex->unlock();
inBufferMutex->unlock();
threads.push_back(ThreadPtr(new std::thread(&ProcessData::process, this)));
}
}
printf("processing stopped\n");
}
Note: The size changes to 1. So i figured the element was created and added.
The error occures while calling workBuffer.front() .
workBuffer is initialized with
std::queue<std::vector<char16_t>> workBuffer;
All it says is "File: G:\Anwendungen\Visual Studio 2012\VC\include\deque
Line: 331
Expression: deque iterator not dereferencable"
I am at loss here....
PS: The idea is to copy the data out of buffer ( filled from another thread ) into workBuffer before starting to analyze the data, so the buffer can be refilled again.
Each workerthread is then given the workBuffer for dequeing, so that it can start working while the queue can be refilled by the thread running the while(1) loop.
I worked on this again today and did the following:
- removed threaded call to process
- switched from object queue to pointer queue
- tried to call only workBuffer.front() without using the return value
- made a testprogram with just the queue to see if i use it correctly ( seems i do )
Then i tried to change the container of the queue to list.
This led to "Access Violation on reading position 0xFFFFFFFFFFFFFFFFF" on calling workBuffer.size() in line 11.
So i figured something must go wrong initializing the queue and its container, but i cant see what...
char16_t is the same as unsigned short ( 16bit positive number ). But thanks for the note,
adapted the code.
If i push an object, is it automatically duplicated?
If not i would work on temp, which could then be changed in the while loop even before my threaded ProcessData::process() function reads it.
Ok, char16_t is to be interpreted as a character, unsigned short is the number.
But i only need my vector elemets to be 16bit long.
Thanks for that information on .push(), i was not aware of that. I adapted my code.
Buffer is the main buffer wich is filled in another thread. It is defined as : typedef std::shared_ptr<std::vector<char>> CharVectorPtr;
I am aware that i need to do some "casting" on the binary level to get from char to char16_t, but that is another issue.
buffer-> size()
Gives access to the size of the std::vector<char> object.
The threading does not cause a problem here, since this is only a reading access.
Basically what i do is check the size of buffer, and as soon as i notice something is received, i wait for the receiving thread to end with the bufferMutex.lock(), so i can analyze the header and copy all data from one package.
I just changed the queue to an object pointer and specifically initialized it with a new object.
Using that pointer the queue worked.
Dont get why, but it does....