Quantcast
Channel: Active questions tagged consumer - Stack Overflow
Viewing all articles
Browse latest Browse all 91

Producer consumer deadlocking in golang

$
0
0

These are my global variables. I do initialize taskCond in my main function.

var (    doneFlag  bool    taskMutex sync.Mutex    taskCond  *sync.Cond)

My producer code looks like this -

func producer(decoder *json.Decoder, taskQueue *LockFreeQueue) {    for {        // process tasks and create task variable        taskMutex.Lock()        taskQueue.Enqueue(task)        taskCond.Signal()        taskMutex.Unlock()    }}

My consumer code looks like this -

func consumer(id int, taskQueue *LockFreeQueue, f feed.Feed, encoder *json.Encoder, wg *sync.WaitGroup) {    for {        taskMutex.Lock()        for taskQueue.isEmpty() == true && !doneFlag {            // Wait for a new task or termination signal            taskCond.Wait()        }        if doneFlag {            taskMutex.Unlock()            wg.Done()            return        }        // Dequeue a task        task := taskQueue.Dequeue()        if task.Command == "DONE" {            doneFlag = true            taskCond.Broadcast()            taskMutex.Unlock()            wg.Done()            return        }        taskMutex.Unlock()        // Process the task    }}

This code deadlocks. I get the error fatal error: all goroutines are asleep - deadlock!

I don't understand why it's happening. The consumer threads are started before the producer thread ( I don't launch a new thread it's the main thread ) so initially they are all asleep. When the p thread enqueues a task to the queue, it signals one of the c threads that there are tasks available, the c thread then breaks out of the for loop, and checks if it got woken because one c thread got a special "DONE" job (this job indicates the threads can now return), that's not the case so it carries on, dequeues a task, checks if it received the special job, if it did, it makes the done flag true, broadcasts and returns. If not, it processes the job as usual. This whole process takes place in a for loop, as we don't know how many jobs are available so the threads cycle among the jobs.

It would be very helpful to know where the program is deadlocking and also if someone corrects my explanation in the previous paragraph.


Viewing all articles
Browse latest Browse all 91

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>