Problem Statement :
I have a producer consumer thread sharing a shared data which is nothing but a structure. Consumer thread is waiting on a std::condition variable wait. the producer thread recvs packet writes down shared struture and notify the consumer thread.
challenges : the consumer thread has to process and send response with few milliseconds (say 10-15ms).
Issue - over a period of time , there are few ocassions where the thread itself taking time to wake up from the cv wait and thus not able to response in few ms.
i tried using yield , but that is hogging the cpu , and the requirement is that cpu utilization should be kept as minimum as possible.
I am interested in knowing : 1 - Why is thread taking so much time to wake up ..?? (more than 40 ms in some cases). 2 - Is there any other way to share data between threads without allowing thread to sleep and also utilizing minimum cpu.(tried pipe but givin less performance over current shared memory implementation).
3 - suggest any other design which may not include threads still keeping concerns seperately and acheiving the required latency .. ????
This is the shared data :
class Message
{
typedef std::basic_string<uint8_t> MesgBuffType;
MesgBuffType _buffer;
static uint16_t _data[256];
public :
append(data .....,len ) append methods
}
void ProcessData(Messege msg)
{
std::unique_lock<std::mutex> lock(mutex);
sharedData.set(msg);
lock.unlock();
_syncCondVar.notify_one();
}
void consumeData()
{
std::unique_lock<std::mutex> lock(mutex);
_syncCondVar.wait(lock);
Messege req;
if (sharedData.get(req))
{
lock.unlock();
processRequest(req);
}
}
Added supporting Code.