我们有一个 Windows32 应用程序,其中一个线程可以通过执行 SuspendThread/GetThreadContext/ResumeThread 来停止另一个线程以检查其状态 [PC 等]。
if (SuspendThread((HANDLE)hComputeThread[threadId])<0) // freeze thread
ThreadOperationFault("SuspendThread","InterruptGranule");
CONTEXT Context, *pContext;
Context.ContextFlags = (CONTEXT_INTEGER | CONTEXT_CONTROL);
if (!GetThreadContext((HANDLE)hComputeThread[threadId],&Context))
ThreadOperationFault("GetThreadContext","InterruptGranule");
Run Code Online (Sandbox Code Playgroud)
极少数情况下,在多核系统上,GetThreadContext 返回错误代码 5(Windows 系统错误代码“拒绝访问”)。
SuspendThread 文档似乎清楚地表明目标线程已挂起,如果没有返回错误。我们正在检查 SuspendThread 和 ResumeThread 的返回状态;他们从来没有抱怨过。
怎么可能我可以挂起一个线程,但不能访问它的上下文?
这个博客 http://www.dcl.hpi.uni-potsdam.de/research/WRK/2009/01/what-does-suspendthread-really-do/
表明 SuspendThread 在返回时可能已开始暂停另一个线程,但该线程尚未暂停。在这种情况下,我可以看出 GetThreadContext 会有什么问题,但这似乎是定义 SuspendThread 的愚蠢方法。(SuspendThread 的调用如何知道目标线程何时真正挂起?)
编辑:我撒谎了。我说这是针对 Windows 的。
好吧,奇怪的事实是,我在 Windows XP 64 下没有看到这种行为(至少在上周没有,我真的不知道在那之前发生了什么)……但我们一直在测试这个 Windows 应用程序Ubuntu 10.x 上的 Wine。当由于某种原因尝试获取线程状态失败时,GetThreadContext的Wine 源在第 819 行包含拒绝访问的返回响应。我在猜测,但似乎 Wine GetThreadStatus 认为线程可能无法重复访问。为什么在 SuspendThead 超出我的范围之后这会是真的,但有代码。想法?
EDIT2:我又撒谎了。我说我们只看到了 Wine 上的行为。不......我们现在发现了一个 Vista …
我有一个在等待一个线程TcpListener.AcceptTcpClient(),该块,我想在次暂停.
我已经读过Monitor.Wait(...),但我只有使用互斥锁的经验,如果线程等待阻塞方法,它会变得有趣.
现在Thread.Suspend(...)已经过时了,我应该如何暂停线程?
我的目标是相互通信主要进程及其“叉”子进程。通信是通过信号传递完成的。
当第一个孩子在等待 SIGUSR1 信号时卡住等待时,我的问题就出现了。
我不知道为什么它会卡在这一点上。甚至,如果我通过控制台发送信号,该子进程似乎没有注意到。
有人可以帮我吗?
代码来了
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
int N = 5;
int _pipe[2];
pid_t children[5];
void main(){
pid_t parent_pid;
pid_t pid;
int i = 0;
sigset_t set;
sigfillset(&set);
parent_pid = getpid();
fprintf(stderr,"I am main process, here comes my pid %u\n",getpid());
if (0>pipe(_pipe)) fprintf(stderr,"Error when creating pipe");
//Start creating child processes
while (i < N){
pid = fork();
if (pid == 0){
close(_pipe[1]);
break;
}
else{
fprintf(stderr,"Created child with pid %u\n",pid);
children[i] = …Run Code Online (Sandbox Code Playgroud) 众所周知,Linux 内核支持待机、挂起到内存、挂起到磁盘。然而,我发现Android不支持suspend-to-disk或hibernate,尽管它的内核源自Linux。
现在,我想在 Android 上启用它,但不知道如何操作。我将基于Andorid 4.1和Linux 3.0.8来完成我的工作。
您愿意提供任何有用的信息吗?以前有人这样做过吗?谢谢。
我有一种情况,我的应用程序有时会在暂停/恢复过程中崩溃.场景如下所示:
Taking the app dump file and examining it using WinDBG shows a call stack like …
突然发现 suspend 函数的递归调用比调用同一个函数但没有suspend修饰符要花费更多的时间,所以请考虑下面的代码片段(基本的斐波那契数列计算):
suspend fun asyncFibonacci(n: Int): Long = when {
n <= -2 -> asyncFibonacci(n + 2) - asyncFibonacci(n + 1)
n == -1 -> 1
n == 0 -> 0
n == 1 -> 1
n >= 2 -> asyncFibonacci(n - 1) + asyncFibonacci(n - 2)
else -> throw IllegalArgumentException()
}
Run Code Online (Sandbox Code Playgroud)
如果我调用此函数并使用以下代码测量其执行时间:
fun main(args: Array<String>) {
val totalElapsedTime = measureTimeMillis {
val nFibonacci = 40
val deferredFirstResult: Deferred<Long> = async {
asyncProfile("fibonacci") { asyncFibonacci(nFibonacci) } as …Run Code Online (Sandbox Code Playgroud) 我对以下代码发生的事情感到困惑。task.yield 是一个从 a 到 b 的哈希映射,而 store.put 是一个挂起函数,它接受一个 a 和一个 b。第一种遍历地图的方法没有问题,第二种方法也是如此。第三种方式对我来说是最自然的迭代方式,也是我最初编写的方式,导致 kotlin 抱怨挂起函数只能在协程主体内调用。我猜这与地图上的 forEaching 如何工作有关(可能与列表相反?)但我真的不明白问题是什么。
launch{
// Kotlin is perfectly happy with this
for(elt in task.yield.keys){
store.put(elt,task.yield[elt]!!)
}
// and this
task.yield.keys.forEach {
store.put(it,task.yield[it]!!)
}
// This makes kotlin sad. I'm not sure why
task.yield.forEach { t, u ->
store.put(t, u)
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我刚刚注意到 forEach 列表是一个内联函数,而我尝试使用的地图不是。我猜是这个问题。
我们有一个基于 .NET Framework 4.8 构建的 WPF 应用程序,但最近我们在任务栏中看到了这个(在 Windows 10 x64 上运行):
将鼠标悬停在 WPF 客户端上时,工具提示显示“此 UWP 进程已暂停,以提高系统性能”。我们的应用程序在再次执行一些工作之前也挂起了一段时间。这怎么可能?我们不是在 .NET Core 3.0 上运行,没有桌面桥,根本没有 UWP 集成......这只是一个普通的 WPF 应用程序。
我正在尝试测试以下 LocalDataSource 函数NameLocalData.methodThatFreezes函数,但它冻结了。我该如何解决这个问题?或者我如何以另一种方式测试它?
要测试的类
class NameLocalData(private val roomDatabase: RoomDatabase) : NameLocalDataSource {
override suspend fun methodThatFreezes(someParameter: Something): Something {
roomDatabase.withTransaction {
try {
// calling room DAO methods here
} catch(e: SQLiteConstraintException) {
// ...
}
return something
}
}
}
Run Code Online (Sandbox Code Playgroud)
测试班
@MediumTest
@RunWith(AndroidJUnit4::class)
class NameLocalDataTest {
private lateinit var nameLocalData: NameLocalData
// creates a Room database in memory
@get:Rule
var roomDatabaseRule = RoomDatabaseRule()
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Before
fun setup() = runBlockingTest {
initializesSomeData()
nameLocalData = …Run Code Online (Sandbox Code Playgroud) 我是《绿箭侠》的新手,并尝试建立其效果系统如何工作的思维模型;特别是它如何利用 Kotlin 的suspend系统。我非常模糊的理解如下;如果有人可以确认、澄清或纠正它,那就太好了:
由于 Kotlin 不支持更高种类的类型,因此将应用程序和 monad 实现为类型类非常麻烦。相反,Arrow 从 Kotlin 的挂起机制提供的延续原语中派生出所有 Arrow 的单子类型的单子功能(绑定和返回)。这是正确的吗?特别是,短路行为(例如, fornullable或either)以某种方式实现为定界延续。我不太明白 Kotlin 挂起机制的哪个特定功能在这里发挥作用。
如果上述内容大致正确,那么我有两个后续问题:我应该如何包含非 IO 单子操作的范围?举一个简单的对象构造和验证示例:
suspend fun mkMessage(msgType: String, appRef: String, pId: String): Message? = nullable {
val type = MessageType.mkMessageType(msgType).bind()
val ref = ApplRefe.mkAppRef((appRef)).bind()
val id = Id.mkId(pId).bind()
Message(type, ref, id)
}
Run Code Online (Sandbox Code Playgroud)
在 Haskell 的 do 表示法中,这将是
mkMessage :: String -> String -> String -> Maybe Message
mkMessage msgType appRef pId = do
type <- mkMessageType msgType
ref <- …Run Code Online (Sandbox Code Playgroud)