其实可以通过interlocked函数来减少锁的使用的。伪代码如下
struct state_context {
volatile LONG retry;
volatile LONG running;
};
struct overlapped_context {
volatile LONG finish;
struct state_context * state;
};
for ( ; ; ) {
struct state_context * stp;
struct overlapped_context * cxp;
GetQueuedCompletionStatus(port, &count, &key, &overlapped);
cxp = (struct overlapped_context *)overlapped;
stp = cxp->state;
InterlockedExchange(&cxp->finish, 1);
InterlockedExchange(&stp->retry, 1);
for ( ; ; ) {
LONG ready = InterlockedExchange(&stp->running, 1);
if (ready != 0)
break;
ready = InterlockedExchange(&stp->retry, 0);
if (ready != 0) {
;do something for process;
}
InterlockedExchange(&stp->running, 0);
ready = InterlockedExchange(&stp->retry, 0);
if (ready == 0)
break;
InterlockedExange(&stp->retry, 1);
}
}
阅读(895) | 评论(0) | 转发(0) |