Libevent 多线程支持

发布于 2025-01-02 22:01:26 字数 276 浏览 2 评论 0原文

我有一些关于 libevent2 及其多线程支持的问题。

libevent支持多线程吗? 我想要实现的是这样的:

  1. 在单个线程中创建一个 event_base 。
    • 在此单个线程中设置事件并将它们关联到事件库。还为每个观察到的事件注册回调。
  2. 一旦观察到的事件发生,就在其他(工作)线程中执行注册的回调。

是否有可能用 libevent 做这样的事情?或者还有其他方法来支持多核吗?

非常感谢

i have a few questions regarding libevent2 and its multithread support.

Does libevent support multiple Threads?
What i would like to achieve is something like this:

  1. Create an event_base in a single thread.
    • In this single Thread setup events and associate them to the event base. Also register Callbacks for each observed event.
  2. As soon as an observed event occurs, execute the registered callback in some other (worker)thread.

Is it possible to do s.th like this with libevent? Or are there any other approaches to support multiple cores?

Thank you very much

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

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

发布评论

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

评论(2

自此以后,行同陌路 2025-01-09 22:01:26

如果添加 evthread_use_pthreads();,则必须具有 -levent_pthreads

示例:

gcc chat.c -o chat -levent -lpthread -levent_pthreads

并且:

gt; ls /usr/lib/libevent*.a
/usr/lib/libevent.a  /usr/lib/libevent_core.a  /usr/lib/libevent_extra.a  /usr/lib/libevent_openssl.a  /usr/lib/libevent_pthreads.a

If you add evthread_use_pthreads(); you must have -levent_pthreads

Example:

gcc chat.c -o chat -levent -lpthread -levent_pthreads

and:

gt; ls /usr/lib/libevent*.a
/usr/lib/libevent.a  /usr/lib/libevent_core.a  /usr/lib/libevent_extra.a  /usr/lib/libevent_openssl.a  /usr/lib/libevent_pthreads.a
笑忘罢 2025-01-09 22:01:26

您需要一些线程池支持。从 2.0.x 开始,Libevent 目前还没有内置其中之一,但将来可能会内置。

您可能需要研究一些建议的扩展。 Mark Ellzey 有一个名为“libevthr”的库,他将其用于 libevhtp 中的线程池。您可以在 libevhtp 存储库中找到它。 Mark Heily 有一个建议的补丁,添加 EV_PARALLEL 标志,让 Libevent 使用 libpthread_workqueue。 它出现在此处的 libevent-users 邮件列表中。

如果两者都没有对于这些适合您的工作,您可以通过选择您喜欢的任何工作队列实现来自己完成,并编写一个 Libevent 回调来自动将您的实际回调排队到另一个线程。

You would need some support for thread pooling. As of 2.0.x Libevent doesn't have one of these built in right now, but it might in the future.

There have been a few proposed extensions you might want to look into. Mark Ellzey has a library called "libevthr" that he uses for thread pools in libevhtp. You can find it in the libevhtp repository. Mark Heily has a proposed patch to add an EV_PARALLEL flag to let Libevent use libpthread_workqueue. It appeared on the libevent-users mailing list here.

If neither of those works out for you, you could do it yourself by picking any work-queue implementation that you like, and writing a Libevent callback to automatically queue your real callback for another thread.

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