Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在多线程应用程序中处理消息后返回Ptread_wait_____________________________ 我的应用程序有2个线程。 线程1,它在端口上保持对消息的轮询。线程1的代码如下所示 //创建线程的构造函数。 clRecvNanoMsgthr_C++_Pthreads - Fatal编程技术网

C++ 在多线程应用程序中处理消息后返回Ptread_wait_____________________________ 我的应用程序有2个线程。 线程1,它在端口上保持对消息的轮询。线程1的代码如下所示 //创建线程的构造函数。 clRecvNanoMsgthr

C++ 在多线程应用程序中处理消息后返回Ptread_wait_____________________________ 我的应用程序有2个线程。 线程1,它在端口上保持对消息的轮询。线程1的代码如下所示 //创建线程的构造函数。 clRecvNanoMsgthr,c++,pthreads,C++,Pthreads,在多线程应用程序中处理消息后返回Ptread_wait_____________________________ 我的应用程序有2个线程。 线程1,它在端口上保持对消息的轮询。线程1的代码如下所示 //创建线程的构造函数。 clRecvNanoMsgthread::clRecvNanoMsgthread() { pthread_attr_t attr; pthread_attr_init(&attr); int error=pthread_create(&_thHandle,NULL,&(clR

在多线程应用程序中处理消息后返回Ptread_wait_____________________________
我的应用程序有2个线程。
线程1,它在端口上保持对消息的轮询。线程1的代码如下所示
//创建线程的构造函数。
clRecvNanoMsgthread::clRecvNanoMsgthread()
{
pthread_attr_t attr;
pthread_attr_init(&attr);
int error=pthread_create(&_thHandle,NULL,&(clRecvNanoMsgthread::thStartFunction),NULL);
}
//线程函数
void*clRecvNanoMsgthread::thStartFunction(void*arg)
{
clrecvnanomg*p_instance=clrecvnanomg::getInstance();
if(OSAL_NULL!=p_实例)
{
p_实例->vrecievinloop();
}
}
//从vReceiveLoop调用function,从这里调用线程2。
void clRecvNanoMsg::processRecvMsg(无符号字符*缓冲区){
args recvBufferData;
int-MSGID;
if(NULL!=缓冲区){
memcpy(&recvBufferData.MsgID,(缓冲区+索引\u原始\u MsgID),数据大小\u两个字节);
memcpy(&recvBufferData.DataLength,(缓冲区+索引\u原始长度),数据大小\u两个字节);
recvBufferData.DevID=buffer[INDEX_DEV_ID];
recvBufferData.DevNr=缓冲区[索引开发编号];
memcpy(recvBufferData.data和缓冲区[INDEX_RAW_data],recvBufferData.DataLength);
recvBufferData.DataLength=ntohs(recvBufferData.DataLength);
coutDevNr=nanoData.DevNr;
memcpy(&data->data,&nanoData.data,sizeof(nanoData.data));
pthread_mutex_unlock(&_thMutexLock);

请帮助我们帮助您:(1)清晰地格式化您的问题文本和代码;(2)将您的问题简化为一个问题,并将其呈现出来(而不是从一个更大的程序中进行各种各样的摘录).clrecvnanomg是一个通过端口在nn_recv上轮询的线程。收到消息后,它将和事件发送到clNanoMsgCoreThread线程,并再次开始轮询。clNanoMsgCoreThread线程将从队列中取出缓冲区,并将DBUS消息作为IPC发送。问题是,在返回调用funct后,收到的第一个消息将通过DBUS发送给客户端ion clNanoMsgCoreThread::ThStartRotine在再次使用pthred_cond_wait时,它在那里崩溃。请检查并给我一些提示…我被这个问题困住了..谢谢
    My application has 2 threads. 
    Thread 1, which keeps polling on port for message. code for Thread 1 as below

    //constructor where thread is getting created.

        clRecvNanoMsgthread::clRecvNanoMsgthread()
         {
            pthread_attr_t attr;
            pthread_attr_init(&attr);

            int error = pthread_create(&_thHandle, NULL,&(clRecvNanoMsgthread::thStartFunction), NULL);
        }
    //Thread Function

        void* clRecvNanoMsgthread::thStartFunction(void *arg) 
        {
                 clRecvNanoMsg* p_instance = clRecvNanoMsg::getInstance();
                 if(OSAL_NULL != p_instance)
                 {
                        p_instance->vRecieveinLoop();        
                 }
        }
    //From vReceiveLoop below fucntion is called from where Thread 2 will be called.

        void clRecvNanoMsg::processRecvMsg(unsigned char* buffer){
            args recvBufferData;
            int MSGID;
            if (NULL != buffer) {
                    memcpy(&recvBufferData.MsgID, (buffer + INDEX_RAW_MSGID), DATA_SIZE_TWO_BYTE);
                memcpy(&recvBufferData.DataLength, (buffer + INDEX_RAW_LENGTH), DATA_SIZE_TWO_BYTE);
                recvBufferData.DevID = buffer[INDEX_DEV_ID];
                recvBufferData.DevNr = buffer[INDEX_DEV_NUMBER];
                memcpy(recvBufferData.data, &buffer[INDEX_RAW_DATA], recvBufferData.DataLength);
                recvBufferData.DataLength = ntohs(recvBufferData.DataLength);
                cout<<"Msg received"<<recvBufferData.MsgID<<"\n";
                ETG_TRACE_USR4(("Msg received MsgID is %d", recvBufferData.MsgID )); 
                recvBufferData.MsgID = ntohs(recvBufferData.MsgID);
                MSGID = recvBufferData.MsgID;
                //_recv_socket->freeBuffer(buffer);

    // this will add the buffer into thread 2 queue
                **clReceiverThread::getInstance()->setNanoMsgEvent(recvBufferData);** 
                Sleep(50);
            }

         }
    //Thread 2 class which will have the queue and would wait for signal. code below.

    //Called by clients for sending data to the thread, event called by Thread 1

        tBool clNanoMsgCoreThread::setNanoMsgEvent(args nanoData)

        {
            //m_mutex.Lock();
            pthread_mutex_lock(&_thMutexLock);

            // make sure that the thread is running 
            if( !getThreadRunStatus() )
            {
                pthread_mutex_unlock(&_thMutexLock);
                checkThreadRunStatus(_thIdleTime*2); // wait two idle cycles for it to start

                pthread_mutex_lock(&_thMutexLock);
            }
            if( !getThreadRunStatus() ) // if it is not running return FALSE;
            {
                pthread_mutex_unlock(&_thMutexLock);
                return FALSE;
            }
            args *data ;
            data = new args;
            data->MsgID =   nanoData.MsgID;
            data->DataLength = nanoData.DataLength;
            data->DevID = nanoData.DevID;
            data->DevNr = nanoData.DevNr;
            memcpy(&data->data, &nanoData.data, sizeof( nanoData.data));

            pthread_mutex_unlock(&_thMutexLock);

            cout << "reached gggg "<< data->MsgID<<" \n";

            if (!addNanoMsgToQueue(data))
            {
                return FALSE;
            }
            **pthread_cond_signal(&_thWaitCond);**

            return TRUE;
        }

    //Thread 2 start function

        VOIDPTR clNanoMsgCoreThread::thStartRoutine( VOIDPTR NanoPacket )

        {
            pthread_mutex_lock(&_thMutexLock);

            setThreadState(enmThStateWaiting);
            setThreadRunStatus(TRUE);

            pthread_mutex_unlock(&_thMutexLock);

            while( TRUE )
            {
                pthread_mutex_lock(&_thMutexLock);
                cout<<"reached thStartRoutine before thread wait condition\n";

                //Wait for the message through signal
                **if (0 != pthread_cond_wait(&_thWaitCond, &_thMutexLock))**
                {
                cout<<"pthread_cond_wait failed\n";
                    break;
                }

                pthread_mutex_unlock(&_thMutexLock);
                //This is where queue will be accessed
                if( ! processNanoMsg() )
                {
                    break;
                }
            }

            pthread_mutex_lock(&_thMutexLock);
            setThreadState(enmThStateDown);
            setThreadRunStatus(FALSE);
            pthread_mutex_unlock(&_thMutexLock);

            return (VOIDPTR)0;
        }
        tBool clNanoMsgCoreThread::processNanoMsg()
        {
            pthread_mutex_lock(&_thMutexLock);
            setThreadState(enmThStateBusy);
            if( !getThreadRunStatus() )
            {
                setThreadState(enmThStateShuttingDown);

                pthread_mutex_unlock(&_thMutexLock);
                return FALSE;
            }

            pthread_mutex_unlock(&_thMutexLock);

            if( !isQueueEmpty() )
            {
                while( !isQueueEmpty() )
                {

                    removeNanoMsgFromQueue();
                    cout<<" Msg ID in ProcessNanoMsg"<<_thNanoMsgCurrentContainer->MsgID<<"\n";
                    if( !_nanoMsgCallBack(_thNanoMsgCurrentContainer) )
                    {
                        pthread_mutex_lock(&_thMutexLock);
                        _thNanoMsgCurrentContainer = NULL;
                        setThreadState(enmThStateShuttingDown);
                        pthread_mutex_unlock(&_thMutexLock);
                        return FALSE;
                    }
                }

                pthread_mutex_lock(&_thMutexLock);
                _thNanoMsgCurrentContainer = NULL;
                setThreadState(enmThStateWaiting);
            }
            pthread_mutex_unlock(&_thMutexLock);
            cout << " reached  yyyy \n";
            return TRUE;
        }

    //After processing the queue calling a call back function

        tBool clReceiverThread::_nanoMsgCallBack( args* data )
        {
            if( data != OSAL_NULL)
            {
                IDMappingInstance = clMsgIDMapping::getInstance();
                if (OSAL_NULL != IDMappingInstance)
                {

                    //get the Service Instance 
                    IBackendInstance = IDMappingInstance->getInstanceForMsgID(data->MsgID);
                    if (OSAL_NULL != IBackendInstance)
                    {
                        //get the callback function pointer
                        FUNCPTR func = IDMappingInstance->getFunctionPtr(data->MsgID);

                        //Do the function Callback
                        if (OSAL_NULL != func)
                          (IBackendInstance->*func)(data);
                        else
                         cout << "func NULL \n"; 

                    }
                    else
                    {
                      cout << " IBackendInstance is NULL\n";
                    }

                }
                else
                {
                  cout << " IDMappingInstance is NULL\n";
                }


            }
            else
            {
            cout << " data is NULL\n";

            }
            return true;
        }