ThreadPool的线程创建事件

发布于 2025-01-29 06:34:41 字数 1387 浏览 4 评论 0原文

每当创建新的ThreadPool线程时,是否可以通知通知(例如,通过事件或回调)?
我正在使用正在使用 pjlib pjlib 通过p/indoke库。 This library requires that every thread that uses any of its APIs has to be previously registered via

注册由外部或本机API创建的螺纹到PJLIB。必须在注册线程的上下文中调用此功能。当通过外部函数或API调用创建线程时,必须使用pj_thread_register()将其“注册”到pjlib,以便它可以与Pjlib的框架合作。在注册期间,需要维护一些数据,并且该数据必须在线程的寿命中可用。

当试图调用任何PJLIB函数表格未注册的线程时,库确实会断言消息:

从未知/外部线程调用pjlib。你必须
用pj_thread_register()注册外部线程
在调用任何PJLIB函数之前。

现在,应用程序的业务逻辑使用async等待,并且它是控制台应用程序SynchronizationContext为null,这意味着所有延续都将在threadpool的线程上运行,包括p/newokes打电话给PJSIP API,使调用pj_thread_register()在每次拨打任何PJSIP API之前,都必须确保一个新创建的TreadPool的线程不会尝试使用任何PJSIP API,然后才能使用任何PJSIP API 。

我认为,如果我可以在新创建的线程的上下文中通知我(在创建新的ThreadPool线程的上下文中被调用),我可以使用此回调仅注册新线程。 有没有办法接收此类通知?

另外,我可以使用SynchronizationContext(例如Stephen Cleary's norofollow noreferrer“ “ https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-context-and-console-apps/” rel =“ nofollow noreferrer”> asyncpump )一旦我创建它们),但是这意味着重新设计应用程序,也许是朝着演员模型迈进,是否有其他方法可以面对这种情况?

Is there a way to be notified (e.g. via an event or a callback) whenever a new ThreadPool thread is created?
I'm working on a .net console app that is using pjlib library via P/Invoke. This library requires that every thread that uses any of its APIs has to be previously registered via pj_thread_register API:

Register a thread that was created by external or native API to PJLIB. This function must be called in the context of the thread being registered. When the thread is created by external function or API call, it must be 'registered' to PJLIB using pj_thread_register(), so that it can cooperate with PJLIB's framework. During registration, some data needs to be maintained, and this data must remain available during the thread's lifetime.

When trying to call any pjlib function form an unregistered thread the library will indeed assert with the message:

Calling pjlib from unknown/external thread. You must
register external threads with pj_thread_register()
before calling any pjlib functions.

Now, the application's business logic uses async and await, and being it a console app the SynchronizationContext is null, meaning all continuations will run on ThreadPool's threads, including the P/Invokes calls to pjsip APIs, making necessary to call pj_thread_register() before every call to any of the pjsip APIs, to be sure that a newly created ThreadPool's thread won't try to use any pjsip API before registering itself.

I thought that if I could be notified (with a callback that gets called in the context of the newly created thread) whenever a new threadpool thread is created I could use this callback to register the new thread only once.
Is there a way to receive such a notification?

Alternatively I could use a SynchronizationContext (e.g. Stephen Cleary's AsyncContext or Stephen Toub's AsyncPump) on the main or on some dedicated threads (and register them as soon as I create them), but this would mean to redesign the application, maybe going towards an actor model, is there an alternative approach to face this kind of situations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦忆晨望 2025-02-05 06:34:41

可以使用分析API或ETW检测所有线程池创建事件。我不确定在这些事件的结果上,在该线程的上下文中执行代码有多容易。

我建议您使用Interop层中的[ThreadStatic]静态bool pjinitialized;字段,并在该层的每个调用中添加检查。如果未初始化,则在实际调用之前调用pj_thread_register

It might be possible to use profiling APIs or ETW to detect all thread pool creation events. I'm not sure how easy it would be to execute code in the context of that thread as a result of those events, though.

I would recommend using a [ThreadStatic] static bool PjInitialized; field in your interop layer, and add a check to every call at that layer. If it's not initialized, then call pj_thread_register before the actual call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文