开发者

passing integer array in thread function

I am working on multiple producer and Single consumer problem.I wanted to pass Thread like 1,2,3 in the thread function so that individual thread can be named based on these number.

But the program is crashing after count 7 while creating thread.I think problem is due to variable nThreadNo; if i limit the count less than 7 it works fine.but if i make count more than this it crashes.

void CEvent1Dlg::CreateProducerThreads()
{   

    try
    {
        nThreadNo = new int20]; 
        memset(nThreadNo,0,20);
        if (nThreadNo ==NULL) return;
    }catch(...)
    {
        MessageBox(_T("Memory allocation Failed"),_T("Thread"),1);
        return ;
    }
    int i = 0;
    for ( i = 0;i<20;i++)
    {   
        //nThreadNo = i+1;
        nThreadNo[i] = i+1;
        hWndProducer[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ProducerThrdFunc,(void*)(nThreadNo+i),0,&dwProducerThreadID[i]);      
        if (hWndProducer[i] == NULL) 
        {
           //ErrorHandler(TEXT("CreateThread"));
           ExitProcess(3);
        }
    }   
    //WaitForMultipleObjects(20,hWndProducer,TRUE,INFINITE);            
}

DWORD WINAPI    ProducerThrdFunc ( LPVOID n )
{
    int *nThreadNo = (int*)n;       
    char chThreadNo[33];
    memset(chThreadNo,0,33);

    while(1)
    {
        itoa(*nThreadNo,chThreadNo,10);
        char* pMsg1 = new char[100];
        char* pMsg2 = new char[100];
        memset(pMsg1,0,100);
        memset(pMsg2,0,100);

        strcpy(pMsg1,"Producer ");      
        strcat(pMsg1," Thread No:");        
        strcat(pMsg1,chThreadNo);

        if (stThreadInfoProd.pEventQueue->AddTail(pMsg1)==TRUE)
        {
            strcpy(pMsg2,"Producer ");      
            strcat(pMsg2," Thread No:")开发者_开发技巧;        
            strcat(pMsg2,chThreadNo);
            strcat(pMsg2," Added the Msg");
        }
        else
        {
            strcpy(pMsg2,"Producer ");      
            strcat(pMsg2," Thread No:");        
            strcat(pMsg2,chThreadNo);
            strcat(pMsg2,"failed to Add the Msg");      
        }
        PostMessage(stThreadInfoProd.hWndHandle,UWM_ONUPDATEPRODUCERLIST,(WPARAM)pMsg2,0);
        strcat(pMsg1," Adding Msg:");
        //PostMessage(stThreadInfoProd.hWndHandle,UWM_ONUPDATEPRODUCERLIST,(WPARAM)pMsg2,0);        
        Sleep(3000);
    }   
    return 0;
}


  1. You are zeroing out the first 20 bytes of nThreadNo, not the first 20 * sizeof(int) bytes as you should be doing.
  2. There are other arrays you are indexing into in this code: hWndProducer, dwProducerThreadID. Are there enough elements in those as well?


The CreateThread call is passing the integer value, but the thread function itself is treating it as a pointer to integer. Rather than this:

int *nThreadNo = (int*)n;   

It should probably be:

int nThreadNo = (int)n;   

Edit: I looked more closely at the call and I do see that it is passing an integer pointer. However, that value is stack data, which may not exist by the time the thread tries to read it. So it should probably just pass the integer value: (void*)(nThreadNo[i])


This line

if (nThreadNo ==NULL) return;

is worthless.

The new operator in modern C++ doesn't return NULL on failure, it throws a std::badalloc exception, but even if you were using an allocator that returns NULL to indicate failure, it's too late to detect it, you're already passed the NULL pointer to memcpy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜