- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的项目有问题。它抛出错误代码 3。
我只是添加了部分代码,让您看看我做了什么。在 main.cpp 中,我在线程上声明,然后发送到 initRequestThreads(in thread.h) 来创建线程。然后在 main.cpp 中让主进程等待它。
主要.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);
线程池.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
- 这意味着,您尝试加入的线程不存在。
原因是代码中关于如何处理线程 ID 的未定义行为一团糟。
pthread_t *requestersThreads = new pthread_t[Length_Tasks];
这会创建一个包含 N 个线程的数组,然后将指向该数组的指针传递给您在
中的函数initRequestThreads(&requestersThreads);
现在,在您的线程创建循环中,您可以
threads[i] = new pthread_t;
pthread_create(&(*threads[i]), &attr /*... */
在这里你完全弄乱了你的数组并触发了未定义的行为。在你的函数中,threads
不是数组!它是数组的地址。您无法使用 array subscript operator
访问它([]
)。剩下的只是雪上加霜。
如果您正在编写 C++11 及更高版本(在 2017 年应该这样做),您应该使用 C++11 std::thread
.如果您出于某种原因绑定(bind)到 C++2003,您至少应该停止这种糟糕的动态数组业务并将指针传递给它们,而是使用 std::vector<pthread_t>
作为函数的输出参数。
关于c++ - pthread_join 错误代码 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44050496/
如果用户在命令行中仅输入 1 个数字,则该程序可以正常工作。它会分解出主要因素并将它们输出到控制台。 J_10542741@cs3060:~/assn3$ ./assn3 12 12: 2, 2, 3
我使用 POSIX pthread 库编写了以下代码: #include #include void *thread_function(void *arg) { char *code =
当我使用 pthread_join() 时,我不确定它是否位于正确的位置。就像现在一样,它会等待线程退出然后再次迭代循环吗?我想我要问的是我应该将其从双 for 循环中取出并在 pthread_joi
#define SIZE 3 #define MAX_THREADS 9 int main() { ... pthread_t m_threads[MAX_THREADS];
来自https://computing.llnl.gov/tutorials/pthreads/ : A joining thread can match one pthread_join() cal
我找到了这个 Sockets 教程 http://www.binarytides.com/socket-programming-c-linux-tutorial/我在最后一个例子中遇到了麻烦。它是一个
因此,当我运行代码时,我在 pthread_join 处遇到段错误。我的 pthread_join 之后有一条打印语句未运行。有谁知道为什么吗?你能给我一些关于如何解决这个问题的提示或想法吗? 输出打
我一直在使用 pthreads,我使用 for 循环和 ProducerThread[index] 和 conssumerThread[index] 将它们创建到一个数组中,从 0 到用户想要的任意数
我的程序需要创建一些线程,但我被困在 pthread_join 上,因为它总是进入错误情况,因为返回(安全)是 3,而不是 0,我认为这是正确的数字,以防万一一切顺利。 编辑:为了更好地解释自己,错误
我正在使用 C 语言的 pthreads 工作,并且再次遇到了问题。我正在尝试将结果作为函数数组发送到我的主线程。 此代码查找每个工作人员的最大值(1 个工作人员/行)并保存该值的索引。到目前为止,一
不知何故,我的代码返回了段错误。 sockfd 是一个 int 或文件描述符 pthread_t tid[4]; int err; int k = 0; while (k < 4) { err
我对此有疑问,因为在 pthread_join 中获得响应时,程序出现段错误,我不知道如何解决此问题。 这是我的代码: #include #include #include struct mat
我正在研究这个问题,但我不理解其中的一部分。该脚本不是英文的,因此翻译会非常乏味,但基本问题是让线程读取特定的文本文件并找到特定的单词。每个文件都有自己的线程等等。最后两个问题是确保同一文件中出现的各
我在 Windows 上使用 pthreads.h 作为简单的光线跟踪器。主函数似乎没有等待线程完成。当我像这样运行程序时(我现在简化了它,只是测试线程,但它仍然给出错误): typedef stru
你能解释一下为什么下面的 pthread_join 不起作用吗?它会阻塞我的代码。如果我评论这三行,我的代码会执行预期的操作,但显然我不知道线程是否终止(在我的代码中没有问题,但在更大的情况下有问题)
我的代码遇到了问题。以下代码启动 n 个线程,这些线程竞争寻找 n 个不同矩阵的每个对角线的最大值。 #include #include #include #include #include
下面的代码对于第二种情况给出了段错误,但对于第一部分它工作正常。但他们俩都在做同样的事情。这里 pthread_join() 调用不会生成任何错误,但是在打印 pthread_join() 的响应时,
int main() { pthread_t thread; int a = 0; while(a == 0) { accept(...); //acc
以下程序处理nbFiles每个 GROUPSIZE 使用 1 个工作线程的文件文件。不超过MAXNBRTHREADS工作线程是并行运行的。一个watchDog()线程(线程 0)用于管理 PTHREA
我利用 pthreads 构建了这段代码。目标是构建一个数组 X[N][D] 并为其分配随机值。您可以将该数组的元素读取为某些点的系数。 下一步,我尝试计算一个数组 distances[N],它包含最
我是一名优秀的程序员,十分优秀!