- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个小程序作为测试,如下所示:
#include<pthread.h>
#include<unistd.h>
#include<cassert>
#include<iostream>
using namespace std;
pthread_mutex_t mt;
pthread_t tid[2];
char* msg[]={"thread1","thread2"};
void* tf(void*arg){
pthread_mutex_lock(&mt);
cout<<(char*)arg<<endl;
return NULL;
}
int main(){
assert(0==pthread_mutex_init(&mt,NULL));
cout<<"step1"<<endl;
pthread_create(&tid[0],NULL,tf,msg[0]);
pthread_create(&tid[1],NULL,tf,msg[1]);
pthread_join(tid[0],NULL);
cout<<"step4"<<endl;
pthread_join(tid[1],NULL);
return 0;
}
我在 mac 上运行它并打印:
step1
thread1
step4
然后它挂起,不再运行。代码哪里出错了?非常感谢。
最佳答案
对于 pthread_mutex_lock
:
The mutex object referenced by mutex shall be locked by calling
pthread_mutex_lock()
. If the mutex is already locked, the calling thread shall block until the mutex becomes available.
线程 2 永远等待 mt
解锁,因为您从未解锁它而线程 1 锁定了它,您应该在 tf
结束时解锁:
void* tf(void*arg){
pthread_mutex_lock(&mt);
cout<<(char*)arg<<endl;
pthread_mutex_unlock(&mt);
return NULL;
}
旁注:如果您可以访问 C++11,请考虑使用 std::mutex
& std::lock_guard
和 std::thread
代替。
关于c++ - pthread_join() 好像导致我的程序挂了,哪里出错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44780320/
如果用户在命令行中仅输入 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],它包含最
我是一名优秀的程序员,十分优秀!