给定函数永远不会执行,尽管被正确调用并且编译器没有抛出错误

时间:2017-11-02 11:41:03

标签: c++ multithreading function

以下来自长串C ++源代码和头文件的片段(我将很乐意在需要时分享)显示用户定义的函数(或对象?)Read被调用为正在进行的多线程操作的一部分。

作为识别问题的一部分,我在调用函数之前和之后打印了标志:

  

std::cout<<"Before calling\n";

  

std::cout<<"After calling\n";

我还在调用函数后插入了要打印的标志:

  

std::cout<<"Entered the function";

整个应用程序完美编译并且运行顺畅 - 直到调用此函数 Read ,它才会被执行。

在输出中,您会看到“在通话前”和“通话后”,但从不会出现“已输入的功能”。

使用的操作系统是Ubuntu 16.04,带有g ++编译器。

截至目前,我只分享了与问题相关的两个片段 - (因为单个文件大约有1500行代码;我认为它应该足够了,如果不是,请告诉我)

以下是线程的片段,其中函数被称为(在第130行),然后是定义函数的另一个文件。

调用Function的线程:

1 void* StartGenericReadThread(void *ThreadAr)
2   {       //std::cout<<"flag GenericThread\n";
3    //printf("\nSuccess");
4           int PORT = ((ThreadArg*)ThreadAr)->PORT;
5    #ifdef WIN32
6           Sleep(4000);
7    #else
8           sleep(4);
9    #endif     
10          DSDMux *Mux;
11          DSDConfigReader *cfg;
12          SNMPTrap    Trap;
13          DSDMessageSender *DMS;
14          Mux = ((ThreadArg*)ThreadAr)->Mux;
15          cfg = ((ThreadArg*)ThreadAr)->cfg;
16          DMS = ((ThreadArg*)ThreadAr)->MSG;
17          
18          
19      strcpy(DMS->SNMP_DET.DSD_MAN_IP,cfg->DSDSetting.DSD_MANAGER_IP);
20      strcpy(DMS->SNMP_DET.SNMP_MAN_IP,cfg->DSDSetting.SNMP_TRAP_IP);
21          DMS->SNMP_DET.SNMP_MAN_PORT = cfg->DSDSetting.SNMP_TRAP_PORT;
22          DMS->SNMP_DET.DSD_MAN_PORT = 162;
23          
24          if ( checkflag != 1)            //EDITED
25          {std::cout<<"\nDSD Manager IP set to    
26  "<<cfg->DSDSetting.DSD_MANAGER_IP<<"\n";
27           std::cout<<"SNMP TRAP IP set to 
28  "<<cfg->DSDSetting.SNMP_TRAP_IP<<"\n";
29 
30   std::cout<<"SNMP TRAP PORT set to "<<cfg->DSDSetting.SNMP_TRAP_PORT<<"\n";
31           checkflag=1;
32          }
33          
34      try
35      {   //std::cout<<"Entered try\n";
36          bool IPLostErrSet(false),IPGainset(false),FIFOreaderr(false);
37          //DSDCo
38          int nStatus, nLatched,inFifoLoad,MaxBuffRead,lostCount(0);
39          dr = theInput[PORT].SetRxControl(DTAPI_RXCTRL_RCV);
40          
41          if ( DTAPI_OK != dr )                           
42          {   //std::cout<<"\nSetRXControl is not OK";
43          //  return NULL;                                EDITED
44          }
45          
46    #ifdef WIN32
47          while(!_kbhit())
48    #else
49          while(1)
50    #endif
51          {   //EDITED
52              //MaxBuffRead=(inFifoLoad/188)*188;
53              //int DmaSize = ( inFifoLoad > c_ReadBufferSize ) ? c_ReadBufferSize : MaxBuffRead;
54              
55              theInput[PORT].GetFlags(nStatus, nLatched);
56              if ( 0 != (DTAPI_RX_FIFO_OVF & nLatched) ) 
57              {
58                  printf("Fifo overflow for input port %d\t\t\t\t\t[Error]\n",PORT+1);
59                  DMS->SendLog(FIFO_OVERFLOW,PORT+1);
60                  theInput[PORT].ClearFifo();
61                  dr = theInput[PORT].SetRxControl(DTAPI_RXCTRL_RCV);
62                  if ( DTAPI_OK != dr )
63                  {
64                      printf("\nError");
65    
66                  }
67                  else
68                      DMS->SendLog(FIFO_OVERFLOW_CLR,PORT+1);
69              }
70              theInput[PORT].GetFifoLoad(inFifoLoad);
71              inFifoLoad = MINBUFFSIZE + 512;
72              if (inFifoLoad < MINBUFFSIZE+512) 
73              {
74                  lostCount++;
75                  if(lostCount>1000&&!IPLostErrSet)                               
76                  {
77                      lostCount=0;
78                      printf("\nInput lost on port %d\t\t\t\t\t\t[ERROR]",PORT+1);
79                      
80                      DMS->SendLog(INPUT_LOST,PORT+1);
81                      int pid;
82                      /*for(pid=21;pid<8191;pid++)
83                      {
84                          if(cfg->MuxCfg.PORT[PORT].PID[pid].Mux)
85                              DMS->SendLog(PID_REMUX_ERR,pid,PORT+1);
86                      }*/
87                      Mux->PSI.PATdata.Table[PORT].Section[0].no_of_programs = 0;
88                      IPGainset = false;
89                      IPLostErrSet = true;
90                  }
91                  
92    #ifdef WIN32
93                  Sleep(1);
94    #else
95                   usleep(1000);
96    #endif
97              continue;
98              }
99              lostCount = 0;
100             //std::cout<<"\nWill check for IPGAINSET now\n";
101             if(!IPGainset)
102             {   //std::cout<<"IPGAINSET IS FALSE\n";
103                 cfg->OutputConf.Status=true;
104                 printf("\nInput signal detected on port %d\t\t\t\t\t[ OK ]",PORT+1);
105                 
106                 DMS->SendLog(INPUT_GAIN,PORT+1);
107                 /*int pid;
108                 for(pid=21;pid<8191;pid++)
109                 {
110                     if(cfg->MuxCfg.PORT[PORT].PID[pid].Mux)
111                     {
112                         //std::cout<<pid;
113                         DMS->SendLog(PID_REMUX,pid,PORT+1);
114                     }
115                 };*/
116                 IPLostErrSet = false;
117                 IPGainset    = true;
118             }
119             //std::cout<<"IPGAINSET is TRUE\n";
120             //printf("\t%d",inFifoLoad);
121         MaxBuffRead=(inFifoLoad/188)*188;
122             int DmaSize = ( inFifoLoad > c_ReadBufferSize ) ? c_ReadBufferSize : MaxBuffRead;
123             
124             //  fifold=(int)DmaSize;
125             // char data[65424*10];
126     
127     
128             std::cout<<"Before Calling\n";
129                 
130             dr = theInput[PORT].Read(Mux->TSPacket[PORT].data,DmaSize);
131              
132             std::cout<<"After Calling\n";
133 
134             if ( DTAPI_OK != dr )
135             {// std::cout<<"Error in executing Read function\n";
136                 if(!FIFOreaderr)
137                     DMS->SendLog(FIFO_READ_ERR,dr,PORT+1);
138                 
139             }
140             else 
141             {
142                 Mux->ParseAndMuxTS(PORT,DmaSize,cfg,fs);
143    
144             }
145         }
146         return NULL;
147    
148     }
149     catch(...)
150     {
151     //  printf("Error in StartGenericReadThread\n");
152         DMS->SendLog(INTERNAL_ERR,0,0,0,"StartGenericReadThread");
153         return NULL;
154     }
155    }

现在,以下是函数定义的片段(在另一个文件中):

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "DTAPI.h"
#include "DtapiUtility.h"
#include "Dta1xxIoCtl.h"
#include "Dta1xxCodes.h"
#include "Dtu2xxIoCtl.h"
#include "Dtu2xxCodes.h"
#include <iostream>


//+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ TsInpChannel implementation +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

//.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- TsInpChannel::Read -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
//
// Read data bytes from the input channel using DMA transfers.
//

DTAPI_RESULT TsInpChannel::Read(
                            char* pBuffer,      // [in] Pointer to data to be read from Device
                            int NumBytesToRead) // [in] Number of bytes to be read from Device
{   
    std::cout<<"Entered the function\n";

    // Must be attached.
    //if (!m_Attached)                      EDITED
    //  return DTAPI_E_NOT_ATTACHED;        EDITED

    // Validity check on parameters
    if (NumBytesToRead<0 || NumBytesToRead%4!=0)
        return DTAPI_E_INVALID_SIZE;
    if ((atoi)(pBuffer)%4 != 0)
        return DTAPI_E_INVALID_BUF;

    // Avoid strange effects when NumBytesToRead is zero
    if (NumBytesToRead == 0)
        return DTAPI_OK;

    DTAPI_RESULT dr;

    // Processing is dependent on device category
    if (m_HwFuncDesc.m_DvcDesc.m_Category == DTAPI_CAT_PCI) {

        //-.-.-.-.-.-.-.-.-.- Call the appropiate device IoCtl handler -.-.-.-.-.-.-.-.-.-

        DTA1XX_IOCTL_DATA  iod;
        memset(&iod, 0, sizeof(DTA1XX_IOCTL_DATA));
        iod.m_nChIdx                                = GetIndexOnDevice();
        iod.m_Data.m_DataTransDesc.m_pBuffer        = pBuffer;
        iod.m_Data.m_DataTransDesc.m_nNumBytesToTr  = NumBytesToRead;

        dr = ioctl(m_fdDevice, DTA1XX_IOCTL_DMA_USERMEM_READ, &iod);
        if ( dr != DTAPI_OK )
            return dr;
    }
    else if (Category() == DTAPI_CAT_USB) {

        // Read using file IO
        int ret = read(m_fdDevice, pBuffer, NumBytesToRead);
        if ( ret < 0 )
            return DTAPI_E_DEV_DRIVER;
    }
    else                //EDITED
        {   std::cout<<"\n Beginning to read file at /home/rtpl/Desktop/ts.mp4";
            char * storedfilepath = "/home/rtpl/Desktop/ts.mp4";
            int *vedant = open(storedfilepath,O_RDONLY);
            int ret = read(vedant, pBuffer, NumBytestoRead);
        if ( ret < 0)
            std::cout<<"\n File contents cannot be read";                               


        //return DTAPI_E_INTERNAL;          // Unknown device category
        }
    return DTAPI_OK;
}

//.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- TsInpChannel::ReadDirect -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
//
DTAPI_RESULT TsInpChannel::ReadDirect(
    char*  pBuffer,         //  [in] Buffer to store data
    int  NumBytesToRead,    //  [in] Number of bytes to be read
    int&  NumBytesRead)     // [out] Number of bytes read
{
    return DTAPI_E_NOT_SUPPORTED;
}

//-.-.-.-.-.-.-.-.-.-.-.-.-.-.- TsInpChannel::ReadUsingDma -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
//
// Old name of Read. Still here for backward compatibility.
//
DTAPI_RESULT TsInpChannel::ReadUsingDma(
    char*  pBuffer,         // [in] Buffer to store data
    int  NumBytesToRead)        // [in] Number of bytes to be written to PCI card
{
    return Read(pBuffer, NumBytesToRead);
}

0 个答案:

没有答案