pthread_join错误代码3 [英] pthread_join error code 3

查看:184
本文介绍了pthread_join错误代码3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有问题. 它会向我抛出错误代码3.

i have problem in my project. it throws me error code 3.

我只添加了部分代码,让您看到我做了什么. 在main.cpp中,我在线程上声明了该线程,然后将其发送到initRequestThreads(在thread.h中)以创建线程.然后在main.cpp中,我让主进程等待它.

I just add part of my code to let you see what I did. in main.cpp I declared on the threads then I send to initRequestThreads(in thread.h) to create the threads. then in main.cpp i let the main process to wait for it.

main.cpp

pthread_t *requestersThreads = new pthread_t[Length_Tasks];
requestsPool->initRequestThreads(&requestersThreads);
void*  status;


// wait for all requests threads
for(t=0; t<Length_Tasks; t++) {
    rc = pthread_join(requestersThreads[t], &status);
    if (rc) {
        cout<<"ERROR; return code from pthread_join() is "<< rc <<endl;
        exit(-1);
    }
    cout<<"Main: completed join with REQUEST thread " << t <<" having a status of "<<(long)status<<endl;
}

// wait for all resolvers threads
for(t=0; t<resolveThreadsAmount; t++) {
    rc = pthread_join(reoslveThreads[t], &status);
    if (rc) {
        cout<<"ERROR; return code from pthread_join() is "<< rc <<endl;
        exit(-1);
    }
    cout<<"Main: completed join with RESOLVER thread " << t <<" having a status of "<<(long)status<<endl;
}


delete[] tasks;
delete[] TaskQueueRequests;
delete[] TaskQueueResolves; 
//delete[] requestersThreads;
//delete[] reoslveThreads;

pthread_mutex_destroy(&TaskQueueResolves_lock);
pthread_cond_destroy(&TaskQueueResolves_cond);

ThreadPool.h

ThreadPool.h

        void initRequestThreads(pthread_t **threads)
    {

        // add the attribute join for the threads
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

        int rc;

        cout << "DEBUG "<< __LINE__<<": numOfThreads:"<<numOfThreadsRequests<<endl;
        for(long i = 0; i < numOfThreadsRequests; i++)
        {   
            threads[i] =  new pthread_t;
            rc = pthread_create(&(*threads[i]), &attr, ThreadPool::OperationRequestThread, (void *)this);  // create thread that get all the thread pool object(this) and preform OperationRequest function
            if(rc)
            {
                cout <<"creating Request thread failed! Error code returned is:"+rc<<endl;
                exit(-1);
            }
            cout << "DEBUG "<< __LINE__<<": Creating Request Thread #" << i+1 << "!\n";
        }

        pthread_attr_destroy(&attr);

}

推荐答案

您收到的错误代码是ESRCH-这意味着您尝试加入的线程不存在.

The error code you are getting is ESRCH - which means, thread you are trying to join doesn't exist.

原因是代码中关于如何处理线程ID的未定义行为造成了严重混乱.

And the reason for that is the terrible mess of undefined behavior in your code in regards to how you handle thread ids.

pthread_t *requestersThreads = new pthread_t[Length_Tasks];

这将创建一个由N个线程组成的数组,然后您将在

This creates an array of N threads, and than you are passing a pointer to this array to your function in

initRequestThreads(&requestersThreads);

现在,在线程创建循环中,您要做

Now, in your thread creation loop, you do

threads[i] =  new pthread_t;
pthread_create(&(*threads[i]), &attr /*... */

在这里,您完全弄乱了阵列并触发了未定义的行为.在您的函数中,threads不是数组!它是数组的地址.您无法使用array subscript operator([])访问它.剩下的只是对这里已经发生的伤害加了侮辱.

Here you completely mess up your array and trigger undefined behavior. In your function, threads is not an array! It is an address of the array. You can't access it with array subscript operator ([]). And the rest is just adding insult to an injury already happened here.

如果您正在编写C ++ 11及更高版本(如您在2017年所做的那样),则应使用C ++ 11 std::thread.如果由于某种原因而绑定到C ++ 2003,则至少应停止动态数组的这一糟糕事务,并传递指向它们的指针,而应使用std::vector<pthread_t>作为函数的输出参数.

If you are writing C++11 and above (as you should in 2017) you should use C++11 std::thread. If you for some reason are bound to C++2003, you should at least stop this terrible business of dynamic arrays and passing a pointer to those, and instead use std::vector<pthread_t> as an output parameter to your function.

这篇关于pthread_join错误代码3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆