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

Suggestions for multiple-reader/single-writer lock?

1,161 views
Skip to first unread message

Marty Hoban

unread,
Apr 13, 1995, 3:00:00 AM4/13/95
to
Hello,

I am trying to build a lock structure using Win32 locking primitives (i.e.
Mutexes, Semaphores etc.) that will control multiple-reads/single-writes.
That is, I need a sort of "reference count" lock that will block a thread
if one or more other threads are using the structure (i.e. reading), and
will release it when no other threads are using it (allowing the thread to
progress with an exclusive write).

Some other operating systems enable this type of lock either natively or
through primitives not provided in Win32, such as condition variables. I
have spent a lot of time thinking of ways to do this with Win32, and so
far can only come up with complicated methods using queues of mutexes etc.
Does anyone know of a reasonably elegant way to build a reader-writer lock
with Win32 primitives?

Thanks for any help.

Marty Hoban

Daniel Earwicker

unread,
Apr 13, 1995, 3:00:00 AM4/13/95
to
I am trying to do the following extremely simple thing with BC++ 4.5:

call LoadLibrary and GetProcAddress to get a pointer to one, just one tiny function.

I then try to call it, and my program jumps to a junk address. I have tried casting
the return function pointer to an unsigned long and then used the Borland debugger
to look at what it points at: hey presto - it points at the start of the right function!

But can I call it? Fuck no.

The weird thing is, this is something that never gives me any trouble under Visual C++,
but I* assume* it is possible under BC. Ordinary load-time linking works, using an
import library, so as a next step I tried compiling the extremely simple example
from the Win32 SDK which just exports a single function and then uses GetProcAddress
on it. BUT... BC cannot link it, complaining that the EXPORT statement is
trying to export a non-public symbol.

So, Borland fans: what very stupid thing I am doing to upset this compiler?


Sridhar ALSE

unread,
Apr 17, 1995, 3:00:00 AM4/17/95
to

There is an article in MSDN CD, under the category "Technical Articles". It is
titled
"Compound Win32 Synchronization Objects" and gives a model for traditional
Reader/Writer lock you have mentioned.
The strategy seems to be pretty simple, understandable and easy to implement.
I had a need to implement sets of such locks, with capability to wait "for all
or none".
Therefore, I had to use an approach different from it, but still based on the
same
principles.
Gook luck.
--ALSE


Sridhar ALSE

My opinions, not my employer's!

Edmond Underwood

unread,
Apr 18, 1995, 3:00:00 AM4/18/95
to
If the compiler is complaining that your definition file is trying to export a
non-public pointer or something like that than that is infact what it is. Check
your WNDCLASS wndclass structure. Do you have a lpfnWndProc associated with the
procedure? If not, it is probably non-public.

---------------------------------------------------------------------------------

Edmond Underwood
Systems Management Group
Computing & Network Services (University of Colorado)
E-mail: unde...@Colorado.Edu

Tom Davies

unread,
Apr 19, 1995, 3:00:00 AM4/19/95
to
to...@blue.seas.upenn.edu (Marty Hoban) wrote:

>Hello,

>I am trying to build a lock structure using Win32 locking primitives (i.e.
>Mutexes, Semaphores etc.) that will control multiple-reads/single-writes.

See Chapter 5 of Richter, esp. the bucket example.

Regards,
Tom Davies


hamilton on BIX

unread,
Apr 21, 1995, 3:00:00 AM4/21/95
to
to...@blue.seas.upenn.edu (Marty Hoban) wrote:

>I am trying to build a lock structure using Win32 locking primitives (i.e.
>Mutexes, Semaphores etc.) that will control multiple-reads/single-writes.

Here's the basic structure and set of routines I used in to control
some multiple-access objects in my highly multithreaded Hamilton
C shell:

typedef struct sh {
CRITICAL_SECTION lock;
HANDLE writelock; /* Manual-reset event */
int readers;
} sharedread_sem;

void initialize_sharedread( sharedread_sem *s )
{
InitalizeCriticalSection(&s->lock);
s->readers = 0;
s->writelock = CreateEvent(NULL, TRUE, FALSE, NULL);
}

void write_lock( sharedread_sem *s )
{
top:
EnterCriticalSection(&s->lock);
if (s->readers)
{
LeaveCriticalSection(&s->lock);
WaitForSingleObject(s->writelock, INFINITE);
goto top;
}
}

void release_write_lock( sharedread_sem *s )
{
LeaveCriticalSection(&s->lock);
}

void read_lock( sharedread_sem *s )
{
EnterCriticalSection(&s->lock);
s->readers++;
ResetEvent(s->writelock);
LeaveCriticalSection(&s->lock);
}

void release_read_lock( sharedread_sem *s )
{
EnterCriticalSection(&s->lock);
if (--s->readers == 0)
SetEvent(s->writelock);
LeaveCriticalSection(&s->lock);
}

Regards,
Doug Hamilton KD1UJ hami...@bix.com Ph 508-440-8307 FAX 508-440-8308
Hamilton Laboratories, 21 Shadow Oak Drive, Sudbury, MA 01776-3165, USA

0 new messages