From: Rob McCool <robm@netscape.com>
Newsgroups: netscape.devs-nsapi
Subject: Re: semaphores and criticals
Date: Fri, 26 Jul 1996 19:28:17 -0700

RP Johns wrote: > > NT3.51 > FasdtTrack 2.0 > NSAPI > > I've got a message queue onto which multiple threads > can put, and multiple threads can get. I need to make > it thread safe. > > It seems like semaphores and CRITICALS > are the 2 choices at hand. Could someone point me or provide > a few examples using sem_init, sem_grab, sem_release or > crit_init, crit_enter, crit_exit. Given the above problem, not > sure which is the best choice. > > I've implemented something using the sem_* stuff but not > sure I'm using it properly. Here's all the doc I've found:

The somewhat misnamed semaphore stuff is intended to provide server-wide locking. Under NT, I would definitely use critical sections.

To use them, simply place calls to crit_enter and crit_exit around the code that manipulates the queue. You should initialize a critical section when you initialize the queue using crit_init(), e.g.

static CRITICAL qcrit; [...]

int myinit(pblock *args, Session *unused, Request *unused2) { [...] qcrit = crit_init(); }

int mycode(pblock *pb, Session *sn, Request *rq) { crit_enter(qcrit); /* manipulate the queue */ crit_exit(qcrit); }

--
Rob McCool, robm@netscape.com http://home.netscape.com/people/robm/
Stunt Programmer, Netscape Communications Corporation
It was working ten minutes ago, I swear...
Reproduced by permission of the author.