Sending jobs to a std::thread - multithreading

I am quite new to std::thread and I quickly realized that creating them is quite costly at least on my computer running W7.
So I decided to create my threads and send jobs to it using that piece of sample code: http://en.cppreference.com/w/cpp/thread/condition_variable
My code runs well no crash however I didn't notice much performance increase so I measured the difference between the time the job finishes and the time the job is detected finished by the main thread ( see WaitUntilJobFinished() ) I noticed that on some rare occasions the time difference was over 2 milliseconds
Does anyone see anything wrong with the code?
Code:
class CJobParameters
{
public:
};
typedef void (*CJobFunc)( const CJobParameters * );
class CThread
{
public:
void Start();
void WaitUntilJobDone();
void StartJob( CJobFunc inJobFunc, const CJobParameters * inJobParameters );
std::thread m_stdThread;
CJobFunc m_jobFunc = nullptr;
const CJobParameters * m_jobParameters = nullptr;
//std::atomic<bool> m_jobDone = true;
std::mutex m_mutex;
std::condition_variable m_cv;
__int64 m_jobDoneAt = 0;
__int64 m_threadJoinedAt = 0;
__int64 m_lostTime = 0;
};
class CThreads
{
public:
static void Start();
static CThread threadArray[ JOB_COUNT ];
};
void ThreadMain( CThread * inThread )
{
while ( true )
{
std::unique_lock<std::mutex> lk( inThread->m_mutex );
inThread->m_cv.wait(lk, [ inThread ]{return inThread->m_jobParameters != nullptr;});
if ( inThread->m_jobFunc )
{
(*inThread->m_jobFunc)( inThread->m_jobParameters );
inThread->m_jobFunc = nullptr;
inThread->m_jobParameters = nullptr;
inThread->m_jobDoneAt = COSToolbox::QuerySystemTime2();
}
lk.unlock();
inThread->m_cv.notify_one();
std::this_thread::sleep_for( std::chrono::nanoseconds(0) );
}
}
void CThread::StartJob( CJobFunc inJobFunc, const CJobParameters * inJobParameters )
{
std::lock_guard<std::mutex> lk( m_mutex );
m_jobFunc = inJobFunc;
m_jobParameters = inJobParameters;
m_cv.notify_one();
}
void CThread::Start()
{
m_stdThread = std::thread( ThreadMain, this );
}
void CThread::WaitUntilJobDone()
{
std::unique_lock<std::mutex> lk( m_mutex );
m_cv.wait(lk, [ this ]{return this->m_jobParameters == nullptr;});
m_threadJoinedAt = COSToolbox::QuerySystemTime2();
m_lostTime = m_threadJoinedAt - m_jobDoneAt;
LOG_INFO( "Thread joined with %f ms lost", (Float32)m_lostTime / 1000 );
}
CThread CThreads::threadArray[ JOB_COUNT ];
void CThreads::Start()
{
for ( Int32 i = 0; i < JOB_COUNT; ++i )
{
threadArray[i].Start();
}
}
void MyJobFunc( const CJobParameters * jobParameters )
{
// do job here
}
void main()
{
CThreads::Start();
while(true)
{
CJobParameters jobParametersArray[ JOB_COUNT ];
for ( Int32 i = 0; i < JOB_COUNT; ++i )
{
CThread & thread = CThreads::threadArray[ i ];
CJobParameters& jobParameters = jobParametersArray[ i ];
jobParameters.m_ // Fill in params
thread.StartJob( &MyJobFunc, &jobParameters );
}
for ( Int32 i = 0; i < JOB_COUNT; ++i )
{
CThread & thread = CThreads::threadArray[ i ];
// Prints 2 ms sometimes whith i = 0
thread.WaitUntilJobDone();
}
}
}

Two things:
You are yielding your processor time unconditionally, and on some older versions of windows, you yield the entire process, not just thread:
std::this_thread::sleep_for( std::chrono::nanoseconds(0) );
this yield is unnecessary. I suspect the reason you're doing it is that without it you were getting a spin loop, resulting from the fact that you are both reading and writing to a single condition variable.
You need two condition variables, one for work pending and one for work done. Typically the listener will pass the condition variable or a struct containing it as a parameter to the thread function, allowing you to pass a single condition variable from your dispatcher.

Related

Arduino String.toInt() function wrongly converts to uint32_t

I have a webserver running on an ESP32-Wrover_E.
A user's session id is stored in cookie format.
This session ID is coming from esp_random() function which returns an uint32_t type integer. It is being set on log in. When a user goes to any page or reloads it, the http route will check the session ID from the cookie. Sometimes it is matching, other times not.
I have created a wokwi sketch to represent the problem.
toInt() function often times does not convert a String to uint32_t properly.
#include <Arduino.h>
void setup() {
Serial.begin(115200);
String user_session = "2227761735"; // <-- This number comes from esp_random();
uint32_t sessionID = user_session.toInt();
Serial.printf("String session: %s, uint32_t session: %lu\n",user_session,sessionID);
}
void loop() {
vTaskDelay(1);
}
Here is a link to wokwi where you can see it yourself: https://wokwi.com/projects/342242993672028755
Here is the esp_random() function documentation:
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html
How can i convert a String to uint32_t?
EDIT:
If you run this code at wokwi you can see that there are errors in the conversion:
void setup() {
Serial.begin(115200);
String user_session = "2227761735";
uint32_t sessionID = user_session.toInt();
Serial.printf("String session: %s, uint32_t session: %lu\n", user_session, sessionID);
}
boolean isConvertSuccess(uint32_t originalInteger, uint32_t convertedInteger) {
if ( originalInteger == convertedInteger ) {
return true;
}
return false;
}
long lastTestMS = 0;
void testSessionStringToInt() {
if ( millis() - lastTestMS >= 2000 ) {
lastTestMS = millis();
uint32_t newSessionID = esp_random();
Serial.println("\n**********");
Serial.printf("newSessionID: %lu\n", newSessionID);
String sessionComingFromA_Cookie = String(newSessionID);
Serial.printf("sessionComingFromA_Cookie: %s\n", sessionComingFromA_Cookie);
uint32_t convertedIntFromCookieString = sessionComingFromA_Cookie.toInt();
Serial.printf("convertedIntFromCookieString: %lu\n", convertedIntFromCookieString);
Serial.printf(
"Conversion was: %s\n",
isConvertSuccess(newSessionID, convertedIntFromCookieString) ? "success" : "failed"
);
Serial.println("**********");
}
}
void loop() {
delay(10);
testSessionStringToInt();
}
Thanks to #hcheung i just had to convert the uint32_t esp_random() to long and use long everywhere. Here is a link to the corrected wokwi ( where every uint32_t became a long, including the printf("%lu") to printf("%ld") since its not an unsigned ): https://wokwi.com/projects/342242993672028755
And here is the corrected code:
void setup() {
Serial.begin(115200);
String user_session = "2227761735";
long sessionID = (long)user_session.toInt();
Serial.printf("String session: %s, uint32_t session: %ld\n", user_session, sessionID);
}
boolean isConvertSuccess(long originalInteger, long convertedInteger) {
if ( originalInteger == convertedInteger ) {
return true;
}
return false;
}
long lastTestMS = 0;
void testSessionStringToInt() {
if ( millis() - lastTestMS >= 2000 ) {
lastTestMS = millis();
long newSessionID = (long)esp_random();
Serial.println("\n**********");
Serial.printf("newSessionID: %ld\n", newSessionID);
String sessionComingFromA_Cookie = String(newSessionID);
Serial.printf("sessionComingFromA_Cookie: %s\n", sessionComingFromA_Cookie);
long convertedIntFromCookieString = sessionComingFromA_Cookie.toInt();
Serial.printf("convertedIntFromCookieString: %ld\n", convertedIntFromCookieString);
Serial.printf(
"Conversion was: %s\n",
isConvertSuccess(newSessionID, convertedIntFromCookieString) ? "success" : "failed"
);
Serial.println("**********");
}
}
void loop() {
delay(10);
testSessionStringToInt();
}

Multiple consumer and producers, how do I unlock consumer thread

I am trying to make a multiple producer and consumer program. The producers produce random numbers and insert them into a shared queue(shared memory) and the consumers print out the numbers. The user calls the program with the following arguments: number of producer threads, number of consumer threads and the size of the shared data.
Right now it just produces one producer(it seems) and just stops. I wanted to see if I can get some help figuring out how to unlock the consumers.
This is the Queue header
class SyncQueue
{
public:
SyncQueue(int sizeMax);
void enqueue(int value);
int dequeue();
private:
int MaxSize, front, rear, itemcounter;
std::vector<int> QueueElements;
std::mutex mutex;
//Condition variables for full and empty checks
std::condition_variable NotFull;
std::condition_variable NotEmpty;
};
This is the Queue functions
SyncQueue::SyncQueue(int sizeMax)
{
front = 0;
rear = 0;
MaxSize = sizeMax;
itemcounter = 0;
QueueElements.reserve(MaxSize);
}
void SyncQueue::enqueue(int value)
{
std::unique_lock<std::mutex> lock(mutex);
NotFull.wait(lock , [this](){return itemcounter != MaxSize; });
QueueElements[rear] = value;
rear = (rear + 1) % MaxSize;
++itemcounter;
NotEmpty.notify_all();
}
int SyncQueue::dequeue()
{
std::unique_lock<std::mutex> lock(mutex);
NotEmpty.wait(lock, [this](){return itemcounter != 0; });
int number = QueueElements[front];
front = (front + 1) % MaxSize;
--itemcounter;
NotFull.notify_all();
return number;
}
This is main where I create the threads
std::vector<std::thread> producers(producerThreadCount);
std::vector<std::thread> consumers(consumerThreadCount);
SyncQueue queue(size);
//Build producer threads
for (int i = 0; i < producerThreadCount; i++)
{
producers[i] = std::thread(produceThread, i,std::ref(ProducerMutex), std::ref(queue), 200);
}
//Build consumers
for (int i = 0; i < consumerThreadCount; i++)
{
consumers[i] = std::thread(consumeThread, i, std::ref(ConsumerMutex), std::ref(queue), 400);
}
These are the produce and consume threads
void produceThread(int threadId, std::mutex &ProducerMutex, SyncQueue &sharedQueue, int time)
{
while (true)
{
int value = RandomNumberGenerator(std::ref(ProducerMutex));
sharedQueue.enqueue(value);
std::this_thread::sleep_for(std::chrono::milliseconds(time));
}
}
void consumeThread(int threadId, std::mutex &ConsumerMutex, SyncQueue &sharedQueue, int time)
{
while (true)
{
std::lock_guard<std::mutex> lock(ConsumerMutex);
int value;
std::cout << "Thread:" << threadId << " consumes:" <<sharedQueue.dequeue() << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(time));
}
}

Implementing boost::barrier in C++11

I've been trying to get a project rid of every boost reference and switch to pure C++11.
At one point, thread workers are created which wait for a barrier to give the 'go' command, do the work (spread through the N threads) and synchronize when all of them finish. The basic idea is that the main loop gives the go order (boost::barrier .wait()) and waits for the result with the same function.
I had implemented in a different project a custom made Barrier based on the Boost version and everything worked perfectly. Implementation is as follows:
Barrier.h:
class Barrier {
public:
Barrier(unsigned int n);
void Wait(void);
private:
std::mutex counterMutex;
std::mutex waitMutex;
unsigned int expectedN;
unsigned int currentN;
};
Barrier.cpp
Barrier::Barrier(unsigned int n) {
expectedN = n;
currentN = expectedN;
}
void Barrier::Wait(void) {
counterMutex.lock();
// If we're the first thread, we want an extra lock at our disposal
if (currentN == expectedN) {
waitMutex.lock();
}
// Decrease thread counter
--currentN;
if (currentN == 0) {
currentN = expectedN;
waitMutex.unlock();
currentN = expectedN;
counterMutex.unlock();
} else {
counterMutex.unlock();
waitMutex.lock();
waitMutex.unlock();
}
}
This code has been used on iOS and Android's NDK without any problems, but when trying it on a Visual Studio 2013 project it seems only a thread which locked a mutex can unlock it (assertion: unlock of unowned mutex).
Is there any non-spinning (blocking, such as this one) version of barrier that I can use that works for C++11? I've only been able to find barriers which used busy-waiting which is something I would like to prevent (unless there is really no reason for it).
class Barrier {
public:
explicit Barrier(std::size_t iCount) :
mThreshold(iCount),
mCount(iCount),
mGeneration(0) {
}
void Wait() {
std::unique_lock<std::mutex> lLock{mMutex};
auto lGen = mGeneration;
if (!--mCount) {
mGeneration++;
mCount = mThreshold;
mCond.notify_all();
} else {
mCond.wait(lLock, [this, lGen] { return lGen != mGeneration; });
}
}
private:
std::mutex mMutex;
std::condition_variable mCond;
std::size_t mThreshold;
std::size_t mCount;
std::size_t mGeneration;
};
Use a std::condition_variable instead of a std::mutex to block all threads until the last one reaches the barrier.
class Barrier
{
private:
std::mutex _mutex;
std::condition_variable _cv;
std::size_t _count;
public:
explicit Barrier(std::size_t count) : _count(count) { }
void Wait()
{
std::unique_lock<std::mutex> lock(_mutex);
if (--_count == 0) {
_cv.notify_all();
} else {
_cv.wait(lock, [this] { return _count == 0; });
}
}
};
Here's my version of the accepted answer above with Auto reset behavior for repetitive use; this was achieved by counting up and down alternately.
/**
* #brief Represents a CPU thread barrier
* #note The barrier automatically resets after all threads are synced
*/
class Barrier
{
private:
std::mutex m_mutex;
std::condition_variable m_cv;
size_t m_count;
const size_t m_initial;
enum State : unsigned char {
Up, Down
};
State m_state;
public:
explicit Barrier(std::size_t count) : m_count{ count }, m_initial{ count }, m_state{ State::Down } { }
/// Blocks until all N threads reach here
void Sync()
{
std::unique_lock<std::mutex> lock{ m_mutex };
if (m_state == State::Down)
{
// Counting down the number of syncing threads
if (--m_count == 0) {
m_state = State::Up;
m_cv.notify_all();
}
else {
m_cv.wait(lock, [this] { return m_state == State::Up; });
}
}
else // (m_state == State::Up)
{
// Counting back up for Auto reset
if (++m_count == m_initial) {
m_state = State::Down;
m_cv.notify_all();
}
else {
m_cv.wait(lock, [this] { return m_state == State::Down; });
}
}
}
};
Seem all above answers don't work in the case the barrier is placed too near
Example: Each thread run the while loop look like this:
while (true)
{
threadBarrier->Synch();
// do heavy computation
threadBarrier->Synch();
// small external calculations like timing, loop count, etc, ...
}
And here is the solution using STL:
class ThreadBarrier
{
public:
int m_threadCount = 0;
int m_currentThreadCount = 0;
std::mutex m_mutex;
std::condition_variable m_cv;
public:
inline ThreadBarrier(int threadCount)
{
m_threadCount = threadCount;
};
public:
inline void Synch()
{
bool wait = false;
m_mutex.lock();
m_currentThreadCount = (m_currentThreadCount + 1) % m_threadCount;
wait = (m_currentThreadCount != 0);
m_mutex.unlock();
if (wait)
{
std::unique_lock<std::mutex> lk(m_mutex);
m_cv.wait(lk);
}
else
{
m_cv.notify_all();
}
};
};
And the solution for Windows:
class ThreadBarrier
{
public:
SYNCHRONIZATION_BARRIER m_barrier;
public:
inline ThreadBarrier(int threadCount)
{
InitializeSynchronizationBarrier(
&m_barrier,
threadCount,
8000);
};
public:
inline void Synch()
{
EnterSynchronizationBarrier(
&m_barrier,
0);
};
};

Copying objects in C++/CLI and message passing in multithreading

I'm trying to transfer a command line code that I have to a more visual program with a
GUI to enable easier use. The original code was in C++, so I'm using Visual C++ that is
available in Visual Studio Express 2012, but I have problems understanding the "new"
managed C++/CLI way of handling objects. Being new to CLI and managed C++, I was wondering
if someone can explain what I am doing wrong, and why it doesn't work. Now here is a
description of the code and the problem.
The program is essentially an optimization program:
There are multiple boxes (modes) in a system, each mode, depending on its type has a
few numerical coefficients that control its behavior and the way it responds to outside
excitation.
The program asks the user to specify the number of boxes and the type of each box.
Then tries to find the numerical coefficients that minimize the difference between
the system response with those obtained experimentally.
So, the UI has means for user to open the experimental result files, specify the number
of modes, and specify the type of each mode. Then, the user can initiate the processing
function by clicking on a start button, that initiates a background worker.
Following the example given in MSDN, I created a class that performs the work:
ref class curveFit
{
public: ref class CurrentState{
public:
int percentage;
int iterationNo;
int stage;
bool done;
multimode systemModel;
};
public:
int modes;
int returncode;
array<double> ^expExcitations;
array<double> ^expResults;
multimode systemModel;
private:
void fcn(int, int, double*, double*, int*);
double totalError(std::vector<double> &);
public:
delegate void fcndelegate(int, int, double*, double*, int*);
public:
curveFit(void);
curveFit^ fit(System::ComponentModel::BackgroundWorker^, System::ComponentModel::DoWorkEventArgs^, Options^);
};
multimode is just a container class: a list of different boxes.
ref class multimode
{
private:
Collections::Generic::List<genericBoxModel ^>^ models;
int modes;
public:
multimode(void);
multimode(const multimode%);
int modeNo(void);
void Add(genericBoxModel^);
void Clear();
genericBoxModel^ operator[](int);
multimode% operator=(const multimode%);
double result(double);
bool isValid();
std::vector<double> MapData();
void MapData(std::vector<double> &);
};
multimode::multimode(void)
{
models = gcnew Collections::Generic::List<genericBoxModel ^>();
modes = 0;
}
multimode::multimode(const multimode% rhs)
{
models = gcnew Collections::Generic::List<genericBoxModel ^>();
for(int ind = 0; ind < rhs.modes; ind++)
models->Add(rhs.models[ind]);
modes = rhs.modes;
}
int multimode::modeNo(void)
{
return modes;
}
void multimode::Add(genericBoxModel^ model)
{
models->Add(model);
modes++;
}
void multimode::Clear()
{
models->Clear();
modes = 0;
}
genericBoxModel^ multimode::operator[](int ind)
{
return models[ind];
}
multimode% multimode::operator=(const multimode% rhs)
{
models->Clear();
for(int ind = 0; ind < rhs.modes; ind++)
models->Add(rhs.models[ind]);
modes = rhs.modes;
return *this;
}
double multimode::result(double excitation)
{
double temp = 0.0;
for(int ind = 0; ind < modes; ind++)
temp += models[ind]->result(excitation);
return temp;
}
bool multimode::isValid()
{
bool isvalid = true;
if(modes < 1)
return false;
for(int ind = 0; ind < modes; ind++)
isvalid = (isvalid && models[ind]->isValid());
return isvalid;
}
std::vector<double> multimode::fullMap()
{
//Map the model coefficients to a vector of doubles
...
}
void multimode::fullMap(std::vector<double> &data)
{
//Map a vector of doubles to the model coefficients
...
}
and genericBoxModel is an abstract class that all box models are based on.
The curvefit::fit function does the optimization based on the options passed to it:
curveFit^ curveFit::fit(System::ComponentModel::BackgroundWorker^ worker, System::ComponentModel::DoWorkEventArgs^ e, Options^ opts)
{
fcndelegate^ del = gcnew fcndelegate(this, &curveFit::fcn);
std::vector<double> data;
CurrentState^ state = gcnew CurrentState;
state->done = false;
state->stage = 0;
state->percentage = 0;
state->systemModel = systemModel;
worker->ReportProgress(state->percentage, state);
switch(opts->optimizationMethod)
{
case 0:
while(iterationNo < maxIterations)
{
data = systemModel.MapData();
OptimizationMethod0::step(some_parameters, data, (optmethods::costfunction)Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(del).ToPointer());
systemModel.MapData(data);
iterationNo++;
state->percentage = 0;
state->systemModel = systemModel;
worker->ReportProgress(state->percentage, state);
}
...
}
}
I'm passing the system model inside the state so that I can display the results of the
latest step on the screen, which doesn't work, but that is another question :-)
The start button calls the curvefit::fit function after initializing the system model:
private: System::Void btnStart_Click(System::Object^ sender, System::EventArgs^ e) {
systemModel.Clear();
for(int mode = 0; mode < modes; mode++)
{
switch(model)
{
case 0:
systemModel.Add(gcnew model0);
systemModel[mode]->coefficients[0] = 100.0 / double(mode + 1);
...
break;
...
}
}
btnStart->Enabled = false;
stStatusText->Text = "Calculating!";
Application::UseWaitCursor = true;
curveFit^ cf = gcnew curveFit;
fitCurve->RunWorkerAsync(cf);
}
private: System::Void fitCurve_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
System::ComponentModel::BackgroundWorker^ worker;
worker = dynamic_cast<System::ComponentModel::BackgroundWorker^>(sender);
curveFit^ cf = safe_cast<curveFit^>(e->Argument);
cf->expExcitations = gcnew array<double>(expExcitations.Count);
expExcitations.CopyTo(cf->expExcitations);
cf->expResults = gcnew array<double>(expResults.Count);
expResults.CopyTo(cf->expResults);
cf->systemModel = systemModel;
cf->modes = modes;
e->Result = cf->fit(worker, e, options);
}
This works perfectly! But, in order to make the optimization process faster and more
successful, I wanted to use the results of previous optimizations as the initial guess
for the next run (if possible):
multimode oldmodel(systemModel);
systemModel.Clear();
for(int mode = 0; mode < modes; mode++)
{
switch(model)
{
case 0:
if(mode < oldmodel.modeNo() && oldmodel.isValid() && (oldmodel[mode]->model == 0))
systemModel.Add(oldmodel[mode]);
else
{
systemModel.Add(gcnew model0);
systemModel[mode]->coefficients[0] = 100.0 / double(mode + 1);
...
}
break;
...
Now, my problem is, after this change, it seems that the messages don't get passed
correctly: the first time the start button is clicked everything functions as it should,
but from then on, if the statement systemModel.Add(oldmodel[mode]); gets executed,
results remain the same as the initial guesses, and don't get updated after the fit
function is called.
So, why should these two lines(Add(oldmodel[mode]) and Add(gcnew model0)) give
such different results?

boost::shared_array assignment crashes application(VC++ 2010)

Modified the below circular queue code for my app.
This queue can hold 32 elements max and I have declared the elements as a structure array inside the class. For adding an element to the queue you have to call CreateElement() functions, which checks for a free element and returns an index. When I reuse an element after processing the following line in the CreateElement functions crashes
boost::shared_array<char> tData(new char[bufferSize]);
m_QueueStructure[queueElems].data = tData;
As per documentation, the assignment operator is supposed to destroy the earlier object and assign the new one. Why is it crashing? Can someone tell me where am I screwing?
#include "boost/thread/condition.hpp"
#include "boost/smart_ptr/shared_array.hpp"
#include <queue>
#define MAX_QUEUE_ELEMENTS 32
typedef struct queue_elem
{
bool inUse;
int index;
int packetType;
unsigned long compressedLength;
unsigned long uncompressedLength;
boost::shared_array<char> data;
}Data;
class CQueue
{
private:
int m_CurrentElementsOfQueue;
std::queue<Data> the_queue;
mutable boost::mutex the_mutex;
boost::condition_variable the_condition_variable;
Data m_QueueStructure[MAX_QUEUE_ELEMENTS];
public:
CQueue()
{
m_CurrentElementsOfQueue = 0;
for(int i = 0; i < MAX_QUEUE_ELEMENTS; i++)
{
m_QueueStructure[i].inUse = false;
m_QueueStructure[i].index = i;
}
}
~CQueue()
{
for(int i = 0; i < m_CurrentElementsOfQueue; i++)
{
int index = wait_and_pop();
Data& popped_value = m_QueueStructure[index];
popped_value.inUse = false;
}
m_CurrentElementsOfQueue = 0;
}
void push(Data const& data)
{
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(data);
lock.unlock();
the_condition_variable.notify_one();
}
bool empty() const
{
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.empty();
}
bool try_pop(Data& popped_value)
{
boost::mutex::scoped_lock lock(the_mutex);
if(the_queue.empty())
{
return false;
}
popped_value=the_queue.front();
the_queue.pop();
return true;
}
int wait_and_pop()
{
boost::mutex::scoped_lock lock(the_mutex);
while(the_queue.empty())
{
the_condition_variable.wait(lock);
}
Data& popped_value=the_queue.front();
the_queue.pop();
return popped_value.index;
}
int CreateElement(int bufferSize, unsigned long _compressedLength,
unsigned long _uncompressedLength, int _packetType) /* Send data length for this function */
{
int queueElems = 0;
if(m_CurrentElementsOfQueue == 32)
{
CCommonException ex(QERROR, QUEUE_FULL, "Circular Buffer Queue is full");
throw ex;
}
for(queueElems = 0; queueElems < MAX_QUEUE_ELEMENTS; queueElems++)
{
if(m_QueueStructure[queueElems].inUse == false)
break;
}
boost::shared_array<char> tData(new char[bufferSize]);
m_QueueStructure[queueElems].data = tData;
m_QueueStructure[queueElems].inUse = true;
m_QueueStructure[queueElems].compressedLength = _compressedLength;
m_QueueStructure[queueElems].uncompressedLength = _uncompressedLength;
m_QueueStructure[queueElems].packetType = _packetType;
m_CurrentElementsOfQueue++;
return queueElems;
}
Data& GetElement(int index)
{
Data& DataElement = m_QueueStructure[index];
return DataElement;
}
void ClearElementIndex(Data& delValue)
{
m_CurrentElementsOfQueue--;
delValue.inUse = false;
}
};
for(queueElems = 0; queueElems < MAX_QUEUE_ELEMENTS; queueElems++) after looping queueElems has value 32 but in your m_QueueStructure only 32 elements so you trying to access m_QueueStructure[queueElems].data to 33rd element. That the problem.
EDIT: try use m_QueueStructure[queueElems].data.reset(new char[bufferSize]);
Solved the problem. Two changes I did. In the wait_and_pop function, I was returning an index rather than a Data&. When I returned Data&, that solved the assignment problem. Another crash was happening due to a memset of a shared_array.get(). Lesson learnt, never memset a shared_array or a shared_ptr.

Resources