Click here to Skip to main content
15,902,492 members
Home / Discussions / C#
   

C#

 
GeneralRe: [C#] Add dll Pin
Dave Kreskowiak25-Feb-07 12:41
mveDave Kreskowiak25-Feb-07 12:41 
AnswerRe: [C#] Add dll Pin
Christian Graus25-Feb-07 11:56
protectorChristian Graus25-Feb-07 11:56 
QuestionHow to Fire TabControl? Pin
Khoramdin25-Feb-07 9:04
Khoramdin25-Feb-07 9:04 
AnswerRe: How to Fire TabControl? Pin
Martin#25-Feb-07 11:10
Martin#25-Feb-07 11:10 
Question[Message Deleted] Pin
Zealous_Me25-Feb-07 8:14
Zealous_Me25-Feb-07 8:14 
AnswerRe: this question twisted me. Pin
Christian Graus25-Feb-07 8:51
protectorChristian Graus25-Feb-07 8:51 
AnswerRe: this question twisted me. Pin
Colin Angus Mackay25-Feb-07 9:01
Colin Angus Mackay25-Feb-07 9:01 
QuestionProducer/Consumer & locking mechanisms... Pin
Shy Agam25-Feb-07 3:31
Shy Agam25-Feb-07 3:31 
Hello experts,

I've written a producer/consumer class which I will be using in my current project.
Here's a snippet of my class:
private Queue<NextDataReply> packs;
private bool productionDone;

private void Enqueue(NextDataReply pack)
{
    lock (packs)
    {
        packs.Enqueue(pack);
        // Notify all threads that a new pack was enqueued
        Monitor.PulseAll(packs);
    }
}

public NextDataReply Dequeue()
{
    lock (packs)
    {
        while (packs.Count == 0 && !productionDone)
            Monitor.Wait(packs);

        // If no more data should be queued for the current item being transfered, no dequeuing is possible
        if (productionDone) return null;

        return packs.Dequeue();
    }
}
AFAIK, this is the accepted and prefered way to go about synchronizing queue access.
However, I can't figure out something...
Consider the following scenario:
The consumer has called Dequeue(), and is now blocking on Monitor.Wait(), waiting for data to be added to the queue.
The producer has now called Enqueue() and has reached the lock statement.

Isn't this a deadlock situation?
The way I see it, the consumer has aquired the lock for the packs object, and calls to lock would block until the consumer has exited the critical section of Dequeue() (which never happens, as it is blocking on Wait()).

Explain it to me please... Smile | :)
AnswerRe: Producer/Consumer & locking mechanisms... Pin
Daniel Grunwald25-Feb-07 6:10
Daniel Grunwald25-Feb-07 6:10 
AnswerRe: Producer/Consumer &amp; locking mechanisms... Pin
S. Senthil Kumar25-Feb-07 6:11
S. Senthil Kumar25-Feb-07 6:11 
AnswerRe: Producer/Consumer & locking mechanisms... Pin
Shy Agam25-Feb-07 7:55
Shy Agam25-Feb-07 7:55 
QuestionUse collection of elements like an indexed array Pin
Monin D.25-Feb-07 2:46
Monin D.25-Feb-07 2:46 
AnswerRe: Use collection of elements like an indexed array Pin
Luc Pattyn25-Feb-07 2:56
sitebuilderLuc Pattyn25-Feb-07 2:56 
AnswerRe: Use collection of elements like an indexed array Pin
Guffa25-Feb-07 2:59
Guffa25-Feb-07 2:59 
GeneralRe: Use collection of elements like an indexed array Pin
Monin D.25-Feb-07 3:05
Monin D.25-Feb-07 3:05 
QuestionCustom DataGrid display Pin
SunsOfFun25-Feb-07 2:22
SunsOfFun25-Feb-07 2:22 
Question[Message Deleted] Pin
Zealous_Me24-Feb-07 22:37
Zealous_Me24-Feb-07 22:37 
GeneralRe: Type Safe Variable Problem Pin
Guffa25-Feb-07 1:45
Guffa25-Feb-07 1:45 
Generalheeello mr. brainy Pin
Zealous_Me25-Feb-07 8:12
Zealous_Me25-Feb-07 8:12 
GeneralRe: heeello mr. brainy Pin
Guffa25-Feb-07 9:34
Guffa25-Feb-07 9:34 
GeneralRe: heeello mr. brainy Pin
Wayne Phipps25-Feb-07 11:29
Wayne Phipps25-Feb-07 11:29 
GeneralRe: Type Safe Variable Problem Pin
Christian Graus25-Feb-07 8:53
protectorChristian Graus25-Feb-07 8:53 
GeneralRe: Type Safe Variable Problem Pin
Dave Kreskowiak25-Feb-07 9:20
mveDave Kreskowiak25-Feb-07 9:20 
GeneralRe: Type Safe Variable Problem Pin
Christian Graus25-Feb-07 9:46
protectorChristian Graus25-Feb-07 9:46 
GeneralRe: Type Safe Variable Problem Pin
Dave Kreskowiak26-Feb-07 12:57
mveDave Kreskowiak26-Feb-07 12:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.