When calling thread::Join() getting an error "The inferior stopped because it triggered an exception" in C++

David student 1 Reputation point
2020-08-26T13:39:06.13+00:00

Hello All,

I have a main thread that will process the messages posted to the message queue.

When destructing the resources I am sending the MSG_NONE message to the message queue to process. And then calling the thread .join() like as shown below:

.h file

thread                                         m_nExecuteThread;
shared_ptr<MsgQueue>            m_hMessageQueueObj;

.cpp file

if(m_nExecuteThread.joinable() == true)
    {
        Message* l_hMessage = new Message();
        l_hMessage->m_eEvents = MSG_NONE;
        l_hMessage->m_hStrSoftphone = "None";
        m_hMessageQueueObj->postMsg(l_hMessage);
        **m_nExecuteThread.join();**
    }

void MsgQueue::postMsg(Message* msg)
{
    unique_lock<std::mutex> lck(mutex_);
    msgQueue.push(msg);
    // Notify to work on the message available in the msgQueue
    m_condVar.notify_one();
}


**m_nExecuteThread = thread(&CMainApp::processmessage,this);**

But once posted the message to the message queue, when control reaches to the line m_nExecuteThread.join(); I am getting the below error:

"The inferior stopped because it triggered an exception" read access violation

I am suspecting that

The current thread (m_nExecuteThread) is processing the message from the message queue and the same thread attempted to join that causes the deadlock.

Could someone please help me how to resolve this issue?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,581 questions
{count} votes