Hi all.
I have weired problem with signals sending between threads.
1) I have a thread. In the run() function I create a signals reciever object and connect signals to it:
void LogServiceThread::run()
{
// Create worker.
try
{
m_impl->m_worker.reset(new LogWorker(m_impl->m_fileName));
}
catch (...)
{
// Ignore all exceptions. If something gone wrong with worker - continue work silently.
}
// Connect signals to worker.
if (!m_impl->m_worker.isNull())
{
connect(this,
SIGNAL(writeAsynchSignal
(QString)), m_impl
->m_worker.
data(),
SLOT(writeMessage
(QString)), Qt
::QueuedConnection);
}
// Run event loop.
exec();
}
void LogServiceThread::run()
{
// Create worker.
try
{
m_impl->m_worker.reset(new LogWorker(m_impl->m_fileName));
}
catch (...)
{
// Ignore all exceptions. If something gone wrong with worker - continue work silently.
}
// Connect signals to worker.
if (!m_impl->m_worker.isNull())
{
connect(this, SIGNAL(writeAsynchSignal(QString)), m_impl->m_worker.data(), SLOT(writeMessage(QString)), Qt::QueuedConnection);
}
// Run event loop.
exec();
}
To copy to clipboard, switch view to plain text mode
2) Now I try to send som signals to it by main thread
void LogServiceThread
::writeAsynch(const QString & string
) {
emit writeAsynchSignal(string);
}
void LogServiceThread::writeAsynch(const QString & string)
{
emit writeAsynchSignal(string);
}
To copy to clipboard, switch view to plain text mode
So basically, this is an asynchronous thread safe logger. And I got weired problem: sometimes SOME log messages are missing. Some signals are emited but never come to slot! Sometimes I got all messages during run, some times only few.
What I am missing?
Thanks.
Bookmarks