如果我知道实际的数字,我该如何构建一个ThreadId? [英] How can I build a ThreadId given that I know the actual number?

查看:125
本文介绍了如果我知道实际的数字,我该如何构建一个ThreadId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GHCi中调试或玩游戏时,我碰巧知道实际的 ThreadId 数字(例如使用 Debug)。 Trace ),但这就是我的全部。



问题是所有线程API,例如 killThread 需要 ThreadId 而不是 Int



我试过Hoogle,但空出来了。有没有办法做到这一点?我主要关心调试,所以我不介意它是否是一个讨厌的黑客攻击,或者它是否通过GHC专用的库。

>

你不行。 ThreadId 是抽象的。您拥有的 Int 实际上只不过是一个计数器( $ b

  32 static StgThreadID next_thread_id = 1; 
...
59 StgTSO *
60 createThread(能力*上限,W_大小)
61 {
62 StgTSO * tso;
...
126 ACQUIRE_LOCK(& sched_mutex);
127 tso-> id = next_thread_id ++; //我们有互斥锁
...
130 RELEASE_LOCK(& sched_mutex);
...
136}
...
161 int
162 rts_getThreadId(StgPtr tso)
163 {
164 return(( StgTSO *)tso) - > id;
165}

它是 rts_getThreadId ThreadId Show 实例中调用。没有映射回实际的TSO。如果你想知道什么 ThreadId 属于 Int ,你需要自己跟踪它们。例如,您可以解析 Int 并填充 Map


It often happens to me when debugging or playing around in GHCi that I happen to know the actual ThreadId number (for example from using Debug.Trace), but that's all I have.

The problem is that all thread APIs, such as killThread require a ThreadId and not an Int.

I've tried Hoogle but came out empty. Is there a way to do this? I'm concerned mostly with debugging, so I don't mind if it's a nasty hack or if it's through a GHC-only library.

解决方案

You can't. ThreadId is abstract. The Int you have is actually nothing more than a counter (source):

32  static StgThreadID next_thread_id = 1;
...
59  StgTSO *
60  createThread(Capability *cap, W_ size)
61  {
62      StgTSO *tso;
...
126     ACQUIRE_LOCK(&sched_mutex);
127     tso->id = next_thread_id++;  // while we have the mutex
...
130     RELEASE_LOCK(&sched_mutex);
...
136 }
...
161 int
162 rts_getThreadId(StgPtr tso) 
163 {
164   return ((StgTSO *)tso)->id;
165 }

It's rts_getThreadId that gets called in ThreadId's Show instance. There's no mapping back to the actual TSO. If you want to know what ThreadId belongs to what Int, you need to keep track of them yourself. You could, for example, parse the Int and fill a Map.

这篇关于如果我知道实际的数字,我该如何构建一个ThreadId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆