Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Semaphore zum zaehlen

1 view
Skip to first unread message

Curtis Newton

unread,
Feb 16, 2012, 9:45:24 AM2/16/12
to
Hallo,

ich wundere mich, warum

HANDLE sem_write = CreateSemaphore(NULL, 3, 3, NULL);

WaitForSingleObject(sem_write, 0L);
WaitForSingleObject(sem_write, 0L);
WaitForSingleObject(sem_write, 0L);
WaitForSingleObject(sem_write, 0L);
WaitForSingleObject(sem_write, 0L);
WaitForSingleObject(sem_write, 0L);

durchläuft, ohne beim vierten Aufruf von WaitForSingleObject zu blocken.
Ich nehme mal an, es gilt das gleiche wie bei Mutexen:

The thread that owns a mutex can specify the same mutex in repeated
wait function calls without blocking

Ich will aber eine "richtige" Semaphore (ich habe ein Erzeuger-
Verbraucher-Problem, welches ich damit lösen will). Also eine, die
runterzählt, wenn ich WaitForSingleObject aufrufe, egal von welchen
Thread.

Was tun?

C.

Jochen Kalmbach [MS MVP]

unread,
Feb 16, 2012, 12:32:13 PM2/16/12
to

> Ich nehme mal an, es gilt das gleiche wie bei Mutexen:
> The thread that owns a mutex can specify the same mutex in repeated
> wait function calls without blocking

Wir kommst Du da drauf?
Du solltes mal "INFINITE" anstelle von "0" verwenden oder den
Rückgabewert auswerten ;)

Das ganze lässt sich auch einfach Debuggen mit WinDbg, der zeigt Dir
dann an, dass der Counter immer brav runtergezählt wird...

0:000> !handle 38 f
Handle 38
Type Semaphore
Attributes 0
GrantedAccess 0x1f0003:
Delete,ReadControl,WriteDac,WriteOwner,Synch
QueryState,ModifyState
HandleCount 2
PointerCount 3
Name <none>
Object Specific Information
Semaphore Count 0
Semaphore Limit 3


Greetings
Jochen

Stefan Reuther

unread,
Feb 16, 2012, 1:18:43 PM2/16/12
to
Curtis Newton wrote:
> ich wundere mich, warum
>
> HANDLE sem_write = CreateSemaphore(NULL, 3, 3, NULL);
>
> WaitForSingleObject(sem_write, 0L);
> WaitForSingleObject(sem_write, 0L);
> WaitForSingleObject(sem_write, 0L);
> WaitForSingleObject(sem_write, 0L);
> WaitForSingleObject(sem_write, 0L);
> WaitForSingleObject(sem_write, 0L);
^^
> durchläuft, ohne beim vierten Aufruf von WaitForSingleObject zu blocken.

Weil du ihm gesagt hast, dass es nicht blocken soll?


Stefan

Curtis Newton

unread,
Feb 20, 2012, 9:19:30 AM2/20/12
to
On 2012-02-16, Jochen Kalmbach [MS MVP] <nospa...@kalmbach-software.de> wrote:
>
>> Ich nehme mal an, es gilt das gleiche wie bei Mutexen:
>> The thread that owns a mutex can specify the same mutex in repeated
>> wait function calls without blocking
>
> Wir kommst Du da drauf?

Stimmt, war Unsinn. Problem lag woanders im Code. Bei Multithreading
muss man immer so um die Ecke denken ;-)

C.
0 new messages