简体   繁体   English

pthread_join错误代码3

[英]pthread_join error code 3

i have problem in my project. 我的项目有问题。 it throws me error code 3. 它会向我抛出错误代码3。

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. 在main.cpp中,我在线程上声明了该线程,然后将其发送到initRequestThreads(在thread.h中)以创建线程。 then in main.cpp i let the main process to wait for it. 然后在main.cpp中,我让主进程等待它。

main.cpp 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 线程池

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

} }

The error code you are getting is ESRCH - which means, thread you are trying to join doesn't exist. 您得到的错误代码是ESRCH这意味着您尝试加入的线程不存在。

And the reason for that is the terrible mess of undefined behavior in your code in regards to how you handle thread ids. 这样做的原因是代码中关于如何处理线程ID的未定义行为非常糟糕。

pthread_t *requestersThreads = new pthread_t[Length_Tasks];

This creates an array of N threads, and than you are passing a pointer to this array to your function in 这将创建一个由N个线程组成的数组,然后您将指向该数组的指针传递给函数

initRequestThreads(&requestersThreads);

Now, in your thread creation loop, you do 现在,在您的线程创建循环中,

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

Here you completely mess up your array and trigger undefined behavior. 在这里,您完全弄乱了数组并触发了未定义的行为。 In your function, threads is not an array! 在您的函数中, threads不是数组! It is an address of the array. 它是数组的地址。 You can't access it with array subscript operator ( [] ). 您无法使用array subscript operator[] )访问它。 And the rest is just adding insult to an injury already happened here. 剩下的只是对这里已经发生的伤害的侮辱。

If you are writing C++11 and above (as you should in 2017) you should use C++11 std::thread . 如果您正在编写C ++ 11及更高版本(如您在2017年所做的那样),则应使用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. 如果由于某种原因绑定到C ++ 2003,则至少应停止动态数组的这一糟糕事务,并传递指向它们的指针,而应将std::vector<pthread_t>用作函数的输出参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM