You know what....it works much better. I have another thread question though.
My application right now is using like 85% of the CPU. I know it's because my messager thread. The messager thread basically has a queue of outgoing messages and if a message needs to be sent, it sends it. Then it checks to see if it needs to read data from the Aardvark (I2C). If it does, it reads it then passes it to the Decoder.
Here's how it looks. It works, just not very efficieintly. Any ideas?
Messager::~Messager()
{
//Destructor
mutex.lock();
waitCondition.wakeOne();
mutex.unlock();
wait();
}
void Messager::run()
{
do
{
if(!packetQueue.isEmpty())
{
if(mutex.tryLock())
{
Packet sendPacket = packetQueue.dequeue();
UINT8 address = addressQueue.dequeue();
UPDATE type = typeQueue.dequeue();
mutex.unlock();
if(SendMessage(sendPacket, address))
emit PacketSent(true, type);
else
emit PacketSent(false, type);
}
}
msleep(pollRate);
if(ReadDevice1)
ReadMessage(D1_ADDRESS);
if(ReadDevice2)
ReadMessage(D2_ADDRESS);
if(ReadDevice3)
ReadMessage(D3_ADDRESS);
if(ReadDevice4)
ReadMessage(D4_ADDRESS);
if (ReadDevice5)
ReadMessage(D5_ADDRESS);
if(ReadDevice6)
ReadMessage(D6_ADDRESS);
if(abort)
stopped = true;
}while(!stopped);
if(stopped)
quit();
else
exec();
}
void Messager::SendPacket(Packet myPacket, UINT8 address, UPDATE Type)
{
packetQueue.enqueue(myPacket);
addressQueue.enqueue(address);
typeQueue.enqueue(Type);
}
bool Messager::Config(int port, int bitrate)
{
//Configure I2C device
}
bool Messager::SendMessage(Packet outgoingPacket, UINT8 addr)
{
//Write packet to I2C bus
}
bool Messager::ReadMessage(UINT8 address)
{
//Read data from I2C bus, emit PacketRecieved signal to Decoder
}
void Messager::EnableDevice1Status(bool Enabled)
{
mutex.lock();
ReadDevice1 = Enabled;
mutex.unlock();
}
Messager::~Messager()
{
//Destructor
mutex.lock();
waitCondition.wakeOne();
mutex.unlock();
wait();
}
void Messager::run()
{
do
{
if(!packetQueue.isEmpty())
{
if(mutex.tryLock())
{
Packet sendPacket = packetQueue.dequeue();
UINT8 address = addressQueue.dequeue();
UPDATE type = typeQueue.dequeue();
mutex.unlock();
if(SendMessage(sendPacket, address))
emit PacketSent(true, type);
else
emit PacketSent(false, type);
}
}
msleep(pollRate);
if(ReadDevice1)
ReadMessage(D1_ADDRESS);
if(ReadDevice2)
ReadMessage(D2_ADDRESS);
if(ReadDevice3)
ReadMessage(D3_ADDRESS);
if(ReadDevice4)
ReadMessage(D4_ADDRESS);
if (ReadDevice5)
ReadMessage(D5_ADDRESS);
if(ReadDevice6)
ReadMessage(D6_ADDRESS);
if(abort)
stopped = true;
}while(!stopped);
if(stopped)
quit();
else
exec();
}
void Messager::SendPacket(Packet myPacket, UINT8 address, UPDATE Type)
{
QMutexLocker locker(&mutex);
packetQueue.enqueue(myPacket);
addressQueue.enqueue(address);
typeQueue.enqueue(Type);
}
bool Messager::Config(int port, int bitrate)
{
//Configure I2C device
}
bool Messager::SendMessage(Packet outgoingPacket, UINT8 addr)
{
//Write packet to I2C bus
}
bool Messager::ReadMessage(UINT8 address)
{
//Read data from I2C bus, emit PacketRecieved signal to Decoder
}
void Messager::EnableDevice1Status(bool Enabled)
{
mutex.lock();
ReadDevice1 = Enabled;
mutex.unlock();
}
To copy to clipboard, switch view to plain text mode
It needs to read only when told to and send any message that gets put into the queue. I'm still new to threads. I'd really appreciate any ideas.
If this is confusing, I can attempt to elaborate better.
Thanks,
Rob
Bookmarks