_;std::condition_variable ypipe_cond_;ypipe_t<int, 100> yqueue;void *yqueue_producer_thread(void *argv ,yqueue本身没有notyfy机制的ypipe_cond_.notify_one();}}std::unique_lock<std::mutex> lock(ypipe_mutex);ypipe_cond ypipe_t用于控制读写位置;这涉及到CAS的问题;读写存在临界点。ypipe_t在yqueue_t的基础上构建一个单写单读的无锁队列。 ypipe_t(const ypipe_t &); const ypipe_t &operator=(const ypipe_t &);};#endif核心要点:(1)T *w:指向第一个未刷新的元素 ypipe_t(const ypipe_t &); const ypipe_t &operator=(const ypipe_t &);};#endif三、总结ypipe_t / yqueue_t
我们再来看 ypipe。 3.1.2 ypipe——yqueue 的封装 yqueue 负责元素内存的分配与释放,入队以及出队列;ypipe 负责 yqueue 读写指针的变化。 ypipe 的定义: 37 // Initialises the pipe. 38 inline ypipe_t() 49 // The destructor doesn't 当c为空时,表示读线程睡眠(只会在读线程中被设置为空) 212 213 // Disable copying of ypipe object. 214 ypipe_t(const ypipe_t &); 215 const ypipe_t &operator=(const ypipe_t &); 3.1.3 ypipe 设计的目的 为了批量读写,即用户可以自主的决定写了多少数据之后开启读 我们可以测试一下结果,对一个数据加 200 万次,分别用环形数组、链表、互斥锁、ypipe 队列分别是什么样的性能。 通过测试发现在一读一写的情况下,ypipe 的优势是非常大的。
form = YPipeWidget() form.signal_valuechanged.connect(valueChanged) form.setWindowTitle("YPipe