Multiple-writer thread-safe queue in C

后端 未结 4 1005
旧巷少年郎
旧巷少年郎 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 03:53

    I'd go for multiple single-writer queues (one per writer thread). Then you can check this for how to get the single reader to read the various queues.

    0 讨论(0)
  • 2020-12-08 03:57

    http://www.liblfds.org

    Lock-free data structure library written in C.

    Has the M&S queue.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 04:02

    If you dont need a lock free queue, then you could just wrap up an existing queue with a lock.

    Mutex myQueueLock;
    Queue myQueue; 
    void mtQueuePush(int value)
    {
        lock(myQueueLock);
        queuePush(myQueue, value);
        unlock(myQueueLock);
    }
    int mtQueueNext()
    {
        lock(myQueueLock);
        int value = queueFront(myQueue);
        queuePop(myQueue);
        unlock(myQueueLock);
        return value;
    }
    

    The only thing after that is to add some sort of handling for mtQueueNext when the queue is empty.

    EDIT: If you have a single reader, single writer lockless queue, you only need to have a lock around mtQueuePush, to prevent multiple simultaneous writers.

    There are a number of single reader/writer lockless queues around, however most of them are implemented as c++ template classes. However do a google search and if need be work out how to rewrite them in plain C.

    0 讨论(0)
提交回复
热议问题