在线程函数中传递整数数组

发布于 2024-10-27 10:59:49 字数 2151 浏览 1 评论 0原文

我正在研究多个生产者和单个消费者问题。我想在线程函数中传递 Thread 像 1,2,3 这样,可以根据这些数字来命名单个线程。

但程序在创建线程时计数 7 后崩溃。我认为问题是由于 变量 nThreadNo; 如果我将计数限制为小于 7,它可以正常工作。但是如果我使计数超过此值,它就会崩溃。

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那一片橙海, 2024-11-03 10:59:49
  1. 您正在将 nThreadNo 的前 20 个字节清零,而不是您应该做的20 * sizeof(int) 字节。
  2. 在此代码中还需要索引其他数组:hWndProducerdwProducerThreadID。其中也有足够的元素吗?
  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?
溺深海 2024-11-03 10:59:49

CreateThread 调用正在传递整数值,但线程函数本身将其视为指向整数的指针。而不是这样:

int *nThreadNo = (int*)n;   

它可能应该是:

int nThreadNo = (int)n;   

编辑:我更仔细地查看了该调用,我确实看到它正在传递一个整数指针。但是,该值是堆栈数据,在线程尝试读取它时可能不存在。所以它可能应该只传递整数值:(void*)(nThreadNo[i])

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])

阪姬 2024-11-03 10:59:49

这条线

if (nThreadNo ==NULL) return;

毫无价值。

现代 C++ 中的 new 运算符在失败时不会返回 NULL,它会抛出 std::badalloc 异常,但即使您使用返回 NULL 的分配器来指示失败,检测到它已经太晚了,您已经将 NULL 指针传递给了 memcpy

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文