- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经创建了一个 ThreadPool 类和一个名为 execute_thread_helper()
的 void 函数,该函数在 void* execute_thread(void* arg)
中调用(它是给定的函数这样的线程:ret = pthread_create(&workers[i], NULL, execute_thread, (void*)this);
)
void ThreadPool::execute_thread_helper()
{
Task task;
pthread_mutex_lock(&mutex);
while(TaskList.empty()) // Previously "if"
{
cout << "Thread #" << pthread_self() << " is blocked. "<< endl;
pthread_cond_wait(&conditionVar, &mutex);
}
task = TaskList.front();
TaskList.pop();
cout << "Thread #" << pthread_self() << " going to run the function. "<< endl;
threadFunction(task);
pthread_mutex_unlock(&mutex);
}
任务以这种方式添加到任务队列中 -
void ThreadPool::add_task(Task newTask)
{
pthread_mutex_lock(&mutex);
TaskList.push(newTask);
pthread_cond_signal(&conditionVar);
pthread_mutex_unlock(&mutex);
}
据我所知,一旦创建线程 - 它就会尝试运行 execute_thread
。然后,给定一个空队列,我希望 pthread_cond_wait
将线程“置于”休眠状态(并对所有创建的线程执行此操作),直到它被 pthread_cond_signal
在 添加任务
。
好吧..我尝试在单个线程上检查程序,并得到了这个结果(我没有add_task
。只是尝试创建池)-
Thread #139859560904448 is blocked.
Thread #139859560904448 going to run the function.
in map() key is and value is 0
我不明白线程是如何通过 if 语句的,如果它之前被搁置的话。
尝试创建 3 个线程池时的输出 -
Thread #140013458028288 is blocked.
Thread #140013458028288 going to run the function.
in map() key is and value is 0
Thread #140013458028288 going to run the function.
in map() key is and value is 0
Thread #140013458028288 going to run the function.
in map() key is and value is 0
为什么其他 2 个线程没有被搁置?
编辑
感谢 SergeyA,将 if 与 while 切换,确实有所帮助。但是,仍然尝试创建 3 个线程池,结果是 -
Thread #139916558706432 is blocked.
Thread #139916558706432 is blocked.
Thread #139916558706432 is blocked.
Thread #139916558706432 is blocked.
Thread #139916558706432 is blocked.
Thread #139916558706432 is blocked.
Thread #139916558706432 is blocked.
Thread #139916558706432 is blocked.
为什么没有创建其他线程?它们不是都应该被创建、同时运行并交替打印它们被阻止吗?
最佳答案
条件变量容易出现所谓的 *spurios 唤醒。这意味着代码已解锁,但条件并没有真正改变,也没有发出信号。
这就是为什么您总是必须在循环中调用wait
函数,并在每次唤醒后检查条件。
关于c++ - pthread_cond_wait 和 pthread_mutex_lock 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36747518/
很久没看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
我是一名优秀的程序员,十分优秀!