- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我需要启动一堆线程,并想优雅地将它们关闭。
我正在尝试使用 pthread_cond_signal
/pthread_cond_wait
来实现此目的,但遇到了问题。
这是我的代码。首先是 thread_main
static void *thrmain( void * arg )
{
// acquire references to the cond var, mutex, finished flag and
// message queue
.....
while( true )
{
pthread_mutex_lock( &lock );
if ( msq.empty() )
{
// no messages so wait for one.
pthread_cond_wait( &cnd, &lock );
}
// are we finished.
if ( finished )
{
// finished so unlock the mutex and get out of here
pthread_mutex_unlock( &lock );
break;
}
if ( !msg.empty() )
{
// retrieve msg
....
// finished with lock
pthread_mutex_unlock( &lock );
// perform action based on msg
// outside of lock to avoid deadlock
}
else
{
// nothing to do so we're
// finished with the lock.
pthread_mutex_unlock( &lock );
}
}
return 0;
}
现在,这一切看起来都不错(无论如何对我来说)。
所以为了拆除线程我有这个方法
void teardown()
{
// set the global finished var
pthread_mutex_lock( &lock );
finished = true;
pthread_mutex_unlock( &lock );
// loop over the threads, signalling them
for ( int i = 0 ; i < threads.size() ; ++i )
{
// send a signal per thread to wake it up
// and get it to check it's finished flag
pthread_cond_signal( &cnd );
}
// need to loop over the threads and join them.
for ( int i = 0 ; i < threads.size() ; ++i )
{
pthread_join( threads[ i ].tid, NULL );
}
}
现在我知道 pthread_cond_signal
不能保证唤醒哪个线程,所以我不能发信号并加入同一个循环。然而,这就是一切出错的地方。 pthread_cond_signal
如果没有线程在等待,则什么都不做,因此可能某些线程不会收到信号,因此不知道退出。
我该如何克服这个问题。
M.
***** 更新 *******请不要发布我应该使用 pthread_cond_broadcast 因为这表现出完全相同的行为。它只会唤醒实际上正在等待 cond var 的线程。任何在此期间正在处理并稍后返回等待的线程都将错过信号并且将被遗忘
最佳答案
首先,你必须改变你的谓词
if ( msq.empty() ) {
// no messages so wait for one.
pthread_cond_wait( &cnd, &lock );
}
到
while ( msq.empty() ) {
// no messages so wait for one.
pthread_cond_wait( &cnd, &lock );
}
那是 pthreads 的事,你必须防范 spurious wakeups .
现在你可以把它改成
while ( msq.empty() && !finished) {
// no messages so wait for one.
pthread_cond_wait( &cnd, &lock );
}
因为在那次检查之后,你已经测试是否完成并退出,如果是的话,您所要做的就是向所有线程发送信号。
因此,在您的拆解函数中,将循环替换为:
pthread_cond_broadcast(&cond);
这应该确保所有线程都被唤醒,并将看到 finished
设置为 true
并退出。
即使您的线程没有卡在 pthread_cond_wait
中,这也是安全的。如果线程正在处理一条消息,它们不会收到唤醒信号,但它们会完成该处理,再次进入循环并看到 finished == false
并退出。
另一种常见的模式是注入(inject)毒消息。毒消息只是您的线程可以识别的特殊消息,表示“停止”,您可以在队列中放置与线程数量一样多的这些消息。
关于c++ - pthread_cond_signal 的优雅线程终止证明有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2317972/
如果我终止应用程序,我在尝试保持我的功能运行时卡住了。 是否可以在应用程序未运行时保持核心位置(地理围栏/地理定位)和核心蓝牙运行?如果可能如何解决我的问题?我已经检查了背景模式,并实现了核心定位方法
该程序要求用户输入一个数字,然后从列表中返回详细信息。我该怎么做? do { Scanner in = new Scanner(System.in);
我正在开发一个内部分发的 iOS 应用程序(即,没有应用程序商店),我希望能够以恒定的 10 分钟间隔报告设备的位置。 无论如何,我在我的 plist 中包含了 location 作为字段 UIBac
我的 mongodb 服务器突然收到信号 15(终止)。我不知道为什么 mongodb 崩溃了。以下是日志消息。 Mon Jun 27 07:33:31.701 [signalProcessingTh
我按顺序运行了一堆malloc,并且每次都检查以确保它是成功的。像这样: typedef struct { int *aray; char *string; } mystruct; m
这个问题已经有答案了: How to stop a running pthread thread? (4 个回答) 已关闭 8 年前。 可以使用 pthread_join() 停止线程。但让我们想象一
#include #include #include struct node{ char data; int p; struct node *ptr; }; struct node *st
这个问题已经有答案了: Why should I use a semicolon after every function in javascript? (9 个回答) 已关闭 8 年前。 好吧,我问
我有一个启动多个工作线程的函数。每个工作线程都由一个对象封装,该对象的析构函数将尝试加入线程,即调用if (thrd_.joinable()) thrd_.join();。但是,每个 worker 必
我正在实现一个应用程序,当用户摇动手机时,该应用程序会监听并采取行动。 所以我实现了以下服务: public class ShakeMonitorService extends Service {
我在使用 Xcode 时遇到问题,其中弹出错误“Source Kit Service Terminated”,并且所有语法突出显示和代码完成在 Swift 中都消失了。我怎样才能解决这个问题? 这是一
我想为我的控制台应用程序安全退出,该应用程序将使用单声道在 linux 上运行,但我找不到解决方案来检测信号是否发送到它或用户是否按下了 ctrl+c。 在 Windows 上有内核函数 SetCon
关键: pthread_cancel函数发送终止信号pthread_setcancelstate函数设置终止方式pthread_testcancel函数取消线程(另一功能是:设置取消点) 1 线程取消
下面的程序在不同的选项级别下有不同的行为。当我用 -O3 编译它时,它永远不会终止。当我用 -O0 编译它时,它总是很快就会终止。 #include #include void *f(void *
我有 3 个节点的 K8S 集群,我创建了 3 个副本 pod,应用程序 app1 在所有 pod 上运行,我通过运行 service yaml 文件建立了服务,我可以看到通过运行 kubectl g
我打算使用 nginx 来代理 websocket。在执行 nginx reload/HUP 时,我知道 nginx 等待旧的工作进程停止处理所有请求。然而,在 websocket 连接中,这可能不会
在 Ubuntu 9.10 上使用 PVM 3.4.5-12(使用 apt-get 时的 PVM 包) 添加主机后程序终止。 laptop> pvm pvm> add bowtie-slave add
我编写了一个应用程序来从 iPhone 录制视频。它工作正常,但有一个大问题。当 AVCaptureSession 开始运行并且用户尝试从其库(iPod)播放音频时。此操作将使 AVCaptureSe
我将如何使用NSRunningApplication?我有与启动应用程序相反的东西: [[NSWorkspace sharedWorkspace] launchApplication:appName]
我正在使用 NSTask 执行一系列长时间运行的命令,如下所示: commandToRun = @"command 1;command2"; NSArray *arguments = [NSArray
我是一名优秀的程序员,十分优秀!