基于 pthread 的多功能多线程实用程序库
我不想重新发明轮子,而且我正在寻找的东西很可能已经存在于 FOSS 世界中。
我正在寻找一个基于 pthread 的实用程序库,它实现了常用的原语以在线程之间进行通信。
我的主要需求是某种用于固定大小消息的阻塞队列,以及等待数据同时到达多个队列的能力(通常使用带有文件句柄的轮询和选择来执行此操作)。
这样的事情存在吗?
编程语言是 C++,但我对 C 库很满意。操作系统是 Linux,但任何 posix 都可以。
编辑
我不是在寻找 pthread 周围的薄包装器(例如 boost::thread 等)。我已经启动并运行了。我正在寻找更高级别的原语。基本上 java.util.concurrancey 为 java 人员提供了什么。
I don't want to reinvent the wheel, and what I'm looking for most likely already exist in the FOSS world.
I'm looking for a pthread bases utility library that implements often used primitives to do communication between threads.
My main need is some kind of blocking queue for fixed size messages and the ability to wait for data to arrive on multiple queues at the same time (what you usually do using poll and select with file-handles).
Does something like this exist?
Programming language is C++ but I'm fine with a C library. OS is Linux but anything posix will do.
EDIT
I'm not looking for a thin wrapper around pthreads (like boost::thread or so). I already have this up and running. I'm looking for higher level primitives. Basically What java.util.concurrancey offers for the java guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的要求已经融入到POSIX 消息队列中。
您可以相反地执行此操作,而不是使用 select()。您可以使用 mq_notify() 告诉您何时出现,而不是在 select() 中等待是值得阅读的东西。 MQ 为您提供了传递信号或让它们生成新线程来读取队列的选项。
如果您确实打算使用 select(),Linux 会让这变得轻松,因为 mqd_t 类型实际上是一个文件描述符。您可以像 select() 中的任何其他 FD 一样使用从 mq_open() 返回的 mqd_t。
请注意,在 select() 中使用 mqd_t 是不可移植的。理论上你应该能够在其他系统上做类似的事情,但我从未测试过。由于 POSIX MQ 有一个到文件系统条目的路径,您应该能够在该路径上直接执行 open() 操作,并在 select() 中使用返回的文件描述符,将其映射到 mq_open() 中使用的 mqd_t 来确定要读取哪个队列。再说一遍,我从来没有尝试过。
Your requirements are already baked into POSIX Message Queues.
Instead of using select() you can do it in reverse. Rather than waiting in a select() you can use mq_notify() to tell you when there is something to read. MQs give you the option of having a signal delivered or having them spawn a new thread to read the queue.
If you are really intent on using select(), Linux makes this painless since the mqd_t type is actually a file descriptor. You can simply use the mqd_t returned from mq_open() like any other FD in select().
Note that use of a mqd_t in select() is not portable. In theory you should be able to do something similar on other systems but I have never tested it. Since POSIX MQs have a path to an entry to the filesystem you should be able to do a straight open() on the path and use the returned file descriptor in the select(), mapping it to the mqd_t used in mq_open() to determine which queue to read. Again, I have never tried it.
总是有 boost::thread 。
There's always boost::thread.
您可以尝试 OpenMP,尽管我不确定它是否基于 pthread API。
You could try OpenMP, though I'm not sure whether it's based on the pthread API or not.
适用于什么编程语言/环境?
一些选项:
For what programming language / environment?
Some options: