- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
全部
我有一个关于 pthread_cond_wait() 的问题。简而言之,我在一个进程中创建了两个 POSIX 线程,如果我执行以下代码,为什么 cpu 利用率满了?
我在上面做实验,如果我把 bool isNodeConnect3 前面的注释去掉,该程序似乎没有问题,CPU 利用率几乎为 0%,换句话说,theads 将进入休眠状态并且不占用 CPU 资源,这就是我想要的。
这是一个数据对齐问题吗?也许吧,但我不这么认为,因为我用“#pragma pack(push,1) ... #pragma (pop)”将我的结构括起来你能给我建议吗??
环境主机操作系统是 win7/intel 64 位, guest 操作系统是 ubuntu 10.04LTS将“处理器核心数:4”提供给 guest 操作系统以下是我的测试代码,您可以通过
构建并运行它gcc -o program1 program1.c -pthread && ./program1获取 CPU 利用率为 25%。结果取决于您的设置。
非常感谢。
代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
#include <stdbool.h>
#pragma pack(push,1)
struct BUFF_TX{
pthread_mutex_t mutex_lock;
pthread_cond_t more;
};
struct AtreeNode{
struct BUFF_TX buff_tx;
bool isNodeConnect;
bool isNodeConnect1;
bool isNodeConnect2;
// bool isNodeConnect3; // important
pthread_t thrd_tx;
};
struct AtreeNode treeNode[2];
int tmp[2];
#pragma (pop)
void Thread_TX(int *nodeIdx)
{
int idx = *nodeIdx;
while(1)
{
printf("Thread %d enter mutex lock...\n", idx);
pthread_mutex_lock(&treeNode[idx].buff_tx.mutex_lock);
while(1)
{
if(idx==0)
{
printf("idx==0 wait...\n");
pthread_cond_wait(&(treeNode[0].buff_tx.more), &treeNode[idx].buff_tx.mutex_lock);
}
else if(idx==1)
{
printf("idx==1 wait...\n");
pthread_cond_wait(&(treeNode[1].buff_tx.more), &treeNode[idx].buff_tx.mutex_lock);
}
else
printf("err\n");
}
pthread_mutex_unlock(&treeNode[idx].buff_tx.mutex_lock);
printf("Thread %d leave mutex lock...\n", idx);
}
}
int main(int argc, char *argv[])
{
int i;
int ret;
tmp[0] = 0;
tmp[1] = 1;
for(i=0; i<2; i++)
{
if(pthread_cond_init(&treeNode[i].buff_tx.more, NULL) != 0)
{
printf("cond %d init fail.\n", i);
exit(EXIT_FAILURE);
}
if(pthread_mutex_init(&treeNode[i].buff_tx.mutex_lock, NULL) != 0)
{
printf("mutex lock %d init fail.\n", i);
exit(EXIT_FAILURE);
}
}
for(i=0; i<2; i++)
{
ret = pthread_create(&treeNode[i].thrd_tx, NULL, (void *)Thread_TX, (void *)(&tmp[i]));
if(ret)
{
printf("pthread_create thrd_tx %d err\n", i);
return false;
}
}
pthread_join(treeNode[0].thrd_tx, NULL);
pthread_join(treeNode[1].thrd_tx, NULL);
exit(EXIT_SUCCESS);
}
最佳答案
删除 #pragma pack(1)
并忘记您曾经听说过它。您的问题是您传递了一个指向 pthread_cond_wait
的无效指针。 pthread_cond_t
有特定的对齐要求,您正在创建不一定符合该要求的对象,因此它们的地址无法传递给 pthread_cond_wait
。另见 https://sourceware.org/bugzilla/show_bug.cgi?id=16549由于未捕获无效指针使用而被移动到针对 GCC 的错误报告中得到解决。
您应该能够通过在 strace -f
下运行您的程序并查看 futex
系统调用失败并显示 EINVAL
或类似的。
关于c - pthread_cond_wait() 不会让 CPU 休眠吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22166474/
很久没看APUE,今天一位朋友问道关于一个mutex的问题,又翻到了以前讨论过的东西,为了不让自己忘记,把曾经的东西总结一下。 先大体看下网上很多地方都有的关于pthread_cond_wait()
为什么在调用 pthread_cond_wait 之前需要锁定互斥锁? 此外,在调用 pthread_cond_signal 之前是否需要锁定(在同一个互斥体上)? ? 谢谢你的帮助。 最佳答案 Wh
从这里:https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal Note that the pthread_cond_wait rout
我正在编写各种代码片段,看看会发生什么。下面的代码旨在延迟所有线程,直到所有线程都到达代码中的某个点,然后使每个线程打印一个独特的数字。由于线程都这样做,因此数字应该以随机顺序出现。 我当前的问题是我
这个问题是关于 llnl 中的 pthread 教程。 。假设有三个线程。 主题 1: pthread_mutex_lock(&mutex) do_something... if condition
因此,pthread_cond_wait()的想法是,它将解锁互斥体并等待条件。 假设您首先手动解锁互斥体,然后等待条件出现。在该时间范围内,在这两个操作之间,您必须假设可能会发生一些不好的事情,另一
我有一个线程 A,其函数在循环中运行,执行某些操作并唤醒另一个线程 B。然后它释放互斥体并继续下一次迭代。线程 B 等待直到收到信号,然后执行某些操作。我的问题是,是否保证 B 在收到信号后会获取互斥
我正在尝试解决 Dining philosophers problem使用 C++。 代码是用 g++ -lpthread 编译的。 整个解决方案在 philosophers github 上.存储库
首先,我的问题是不同的。 在我的场景中,有一个等待线程,它等待条件变量。信号线程信号条件变量。 我的代码是 //Wating thread //Lock the mutex_ //mutex_ is
我正在尝试以我认为应该使用的方式使用 pthread_cond_wait。我在一个等待改变的方法中使用它,当它改变时,它会调用另一个方法来获取数据。如下所示: void waitForSomethin
我的堆栈中有一段代码正在运行,等待 pthread_cond_wait。信号被发送到同一进程,但信号处理程序中没有“pthread_cond_signal”代码来通知 pthread_cond_wai
如果线程调用 pthread_cond_wait(cond_ptr,mutex_ptr) 将返回一个null cond_ptr,是否保证不会睡着? 根据 http://pubs.opengroup.o
关于下面的代码,我的理解是线程1抢锁,检查条件,解锁时钟,将自己置为休眠状态。之后,线程 2 申请相同的锁和增量计数,然后唤醒休眠线程。我的问题是现在的条件还是false,但是唤醒了休眠的线程,会发生
我正在使用 pthread_cond_wait() 但我仍然不确定它是如何工作的。我有更多线程,可以完成这项工作: pthread_mutex_lock(&mutex); while(count()
这个问题是关于 llnl 中的 pthread 教程的.假设有三个线程。 线程 1: pthread_mutex_lock(&mutex) do_something... if condition
bool flag=false; pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond=PTHREAD_COND_I
长话短说 我有一个作业需要实现线程同步。在实现过程中,我担心 pthread_cond_wait() 是否也锁定了互斥锁,如果它被虚假唤醒,而不仅仅是成功唤醒。 任务 该任务是一个工作人员/交付问题,
pthread_cond_wait 是否将调用线程置于等待状态以被 pthread_cond_signal/pthread_cond_broadcast 唤醒,这样它就不会轮询和搅动 CPU? 另外,
我目前正在尝试分析第三方源代码中的一个问题,其中线程(对应于 THREAD-T1 的代码片段)处于无限等待状态。怀疑是线程卡在了pthread_cond_wait。以下是相同的详细信息。 代码说明 T
我有以下代码: typedef struct { ... volatile int i_lines_completed; pthread_mutex_t mutex; q265
我是一名优秀的程序员,十分优秀!