Multiple-writer thread-safe queue in C

后端 未结 4 1014
旧巷少年郎
旧巷少年郎 2020-12-08 03:21

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 threa

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 04:00

    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.

    Edit: 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.

提交回复
热议问题