是否有任何等价物在Linux / Unix下的futex的? [英] Are there any equivalents to the futex in Linux/Unix?

查看:134
本文介绍了是否有任何等价物在Linux / Unix下的futex的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找的东西,可用于的查询的(如选择的kqueue 的epoll 即不是的忙轮询的)的C / C ++。换句话说,我需要阻塞线程,然后在少开销尽可能另一个线程唤醒它。

I'm looking for something could be used for polling (like select, kqueue, epoll i.e. not busy polling) in C/C++. In other word, I need to block a thread, and then wake it up in another thread with as little overhead as possible.

A 互斥 + 条件变量的作品,但有一个很大的开销。 A futex的也能工作,但这是仅适用于Linux(或者也许不是?)。额外的同步是不是只要查询的本身的正常工作,例如需要没有比赛的时候我称之为唤醒在两个线程。

A mutex + condition variable works, but there is a lot of overhead. A futex also works, but that's for Linux only (or maybe not?). Extra synchronization is not required as long as the polling itself works properly, e.g. no race when I call wait and wake in two threads.

编辑:?如果这样的设施中不存在的FreeBSD,如何创建一个用C ++ 11的内置类型和系统调用

If such a "facility" doesn't exist in FreeBSD, how to create one with C++11 built-in types and system calls?

EDIT2:既然这个问题被迁移到的话,我想使其更加通用(不适用于仅FreeBSD的)

Since this question is migrated to SO, I'd like to make it more general (not for FreeBSD only)

推荐答案

您想信号灯和线程之间的信令并不互斥。

You want semaphores and not mutexes for the signaling between the to threads..

http://man7.org/linux/man-pages/ man3 / sem_wait.3.html

信号灯可以像在每次插入一个消息时,信号量的计数器,例如,如果你有一个队列,增加(岗位),和接收器递减(等待)信号量为每封邮件需要的。如果计数器达到零接收器将阻塞,直到一些被张贴。

Semaphores can be used like a counter such as if you have a queue, you increment (post) to the semaphore every time you insert a message, and your receiver decrement (wait) on the semaphore for every message it takes out. If the counter reach zero the receiver will block until something is posted.

因此​​,一个典型的模式是一个互斥体,并像一个信号相结合;

So a typical pattern is to combine a mutex and a semaphore like;

sender:
    mutex.lock
    insert message in shared queue
    mutex.unlock
    semaphore.post

receiver:
    semaphore.wait
    mutex.lock
    dequeue message from shared structure
    mutex.unlock

这篇关于是否有任何等价物在Linux / Unix下的futex的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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