用C多的作家线程安全队列 [英] Multiple-writer thread-safe queue in C

查看:115
本文介绍了用C多的作家线程安全队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作使用pthread的多线程C应用程序。我有一个线程其写入到一个数据库(数据库库是唯一安全的在单个线程中使用),并正在搜集数据,处理它,然后根据需要将结果发送到数据库线程存储多个线程。我见过提​​到,这是可能,使在C多的作家安全的队列,但我看到这仅仅提到的每一个地方说,这是这个例子太复杂,只是证明一个作家安全队列

I am working on a multi-threaded C application using pthreads. I have one thread which writes to a a database (the database library is only safe to be used in a single thread), and several threads which are gathering data, processing it, and then need to send the results to the database thread for storage. I've seen in mentioned that it is "possible" to make a multiple-writer safe queue in C, but every place I see this mentioned simply says that it's "too complicated for this example" and merely demonstrates a single-writer safe queue.

我需要以下几点:


  • 高效插入和删除。我会认为像任何其他队列O(1)入队和出队是可能的。
  • 动态分配的内存,即链接结构。我需要不会对队列的大小任意的限制,所以数组真的不是我要找的。
  • Efficient insertion and removal. I would assume that like any other queue O(1) enqueueing and dequeueing is possible.
  • Dynamically allocated memory, i.e. a linked structure. I need to not have an arbitrary limit on the size of the queue, so an array really isn't what I'm looking for.

编辑:的阅读线程不应该在旋转的空队列,因为有可能是值得分钟时间没有写入,有大量写入短时间。

Reading threads should not spin on an empty queue, since there is likely to be minutes worth of time with no writes, with short bursts of large numbers of writes.

推荐答案

当然,还有无锁队列。基于什么你在评论说,虽然,在这里的表现并不重要,因为你要创建每写一个线程反正。

Sure, there are lockless queues. Based on what you've said in comments, though, performance here is not at all critical, since you're creating a thread per write anyway.

所以,这是一个标准的用例条件变量。让自己包含一个互斥体,条件变量,一个链表(或者,如果你喜欢循环缓冲区)一个结构,以及取消标志:

So, this is a standard use case for a condition variable. Make yourself a struct containing a mutex, a condition variable, a linked list (or circular buffer if you like), and a cancel flag:

write:
    lock the mutex
    (optionally - check the cancel flag to prevent leaks of stuff on the list)
    add the event to the list
    signal the condition variable
    unlock the mutex

read:
   lock the mutex
   while (list is empty AND cancel is false):
       wait on the condition variable with the mutex
   if cancel is false:  // or "if list non-empty", depending on cancel semantics
       remove an event from the list
   unlock the mutex
   return event if we have one, else NULL meaning "cancelled"

cancel:
   lock the mutex
   set the cancel flag
   (optionally - dispose of anything on the list, since the reader will quit)
   signal the condition variable
   unlock the mutex

如果您使用的是与外部节点列表,那么你可能想分配内存互斥锁之外,只是减少其持有的时间。但是,如果你设计的事件与这可能是一种侵入性列表节点最简单的。

If you're using a list with external nodes, then you might want to allocate the memory outside the mutex lock, just to reduce the time its held for. But if you design the events with an intrusive list node that's probably easiest.

编辑:还可以支持多个阅读器(不便携式担保哪一个得到一个特定的事件)如果取消你改变的信号,广播。虽然你不需要它,它并没有真正花费任何事。

you can also support multiple readers (with no portable guarantees for which one gets a given event) if in cancel you change the "signal" to "broadcast". Although you don't need it, it doesn't really cost anything either.

这篇关于用C多的作家线程安全队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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