From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jonathan Larmour To: "Trenton D. Adams" Cc: 'eCos mailing list' Subject: Re: [ECOS] Condition Variables recap Date: Tue, 24 Jul 2001 12:48:00 -0000 Message-id: <3B5DD101.F9561D7E@redhat.com> References: <003801c11478$2961f080$090110ac@TRENT> X-SW-Source: 2001-07/msg00759.html "Trenton D. Adams" wrote: > > > > > "Trenton D. Adams" wrote: > > > > > > Consumer Thread > > > Each consumer thread starts in a loop and checks the condition > > variable > > > by calling cyg_cond_wait (). > > > They will either be suspended, or one will succeed. > > > After the producer thread calls cyg_cond_broadcast (), ONE > consumer > > > thread will lock the mutex, and begin reading data. > > > Now this thread should call cyg_mutex_unlock () which will cause > > another > > > ONE thread to lock it. > > > > > > Now I have a question. What happens to the cyg_cond_wait () call > in > > the > > > OTHER consumer threads when cyg_cond_broadcast () is called? Do > they > > > simply get suspended, after that other ONE consumer thread locks > the > > > mutex, and until the mutex is available to ONE of them again? > > > > Yes. They will all have been woken up by the broadcast, and then > each go > > through locking and unlocking the mutex. By that point the condition > may > > no > > longer be true of course, i.e. there may be no more waiting data. I > > would > > imagine cyg_cond_signal() would be more appropriate (although > there's > > still > > a chance more than one thread is woken up). > > > > Why would cyg_cond_signal () be more appropriate? Shouldn't only one thread need to process the arrived data? With _broadcast(), all of them will wake up. But still, I don't know your application, so it's up to you. If it works for you, whatever :). > Is there any way that two of the threads could accidentally lock a mutex > at the exact same time and not even know it? No. Unless you misuse the API (e.g. by calling cyg_mutex_trylock() and don't check the return code). Jifl -- Red Hat, Rustat House, Clifton Road, Cambridge, UK. Tel: +44 (1223) 271062 Maybe this world is another planet's Hell -Aldous Huxley || Opinions==mine Come to the Red Hat TechWorld open source conference in Brussels! Keynotes, techie talks and exhibitions http://www.redhat-techworld.com/