Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 578859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:18:18+00:00 2026-05-13T14:18:18+00:00

The usual pattern for a ReadWriteMutex is to use a semaphore and have the

  • 0

The usual pattern for a ReadWriteMutex is to use a semaphore and have the writer loop to acquire all the resources:

inline void write_lock() {
  ScopedLock lock(acquire_mutex_);
  for (size_t i=0; i < resource_count_; ++i) {
    if (sem_wait(semaphore_) < 0) {
      fprintf(stderr, "Could not acquire semaphore (%s)\n", strerror(errno));
    }
  }
}

This is fine except that you have to specify the resource count during semaphore initialization and arbitrarily choosing a resource count of 10 or 99999 does not feel right. Is there a better pattern that would allow “infinite” readers (no need for a resource count) ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:18:18+00:00Added an answer on May 13, 2026 at 2:18 pm

    I found a solution: using pthread_rwlock_t (ReaderWriterLock on Windows). These locks do not require a specific ‘max_readers_count’.

    I suspect that the implementation for this lock uses some kind of condition variable to lock readers entry when a writer needs to write and an atomic reader count.

    Comparing this with my homebrewed semaphore based lock shows that writers are favored (they tend to run first).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have the usual web.xml for our web application which includes some jsp and
I have a shop system that integrates PayPal in the usual way, i.e. the
The usual pattern for a singleton class is something like static Foo &getInst() {
When a C++ function accepts an std::vector argument, the usual pattern is to pass
I am using a simple repository pattern and have objects with a LazyList such
In the usual Strategy Pattern, we make each strategy as a class. Can't we
So I use Qt a lot with my development and love it. The usual
I know what is usual mediator design pattern (some description is in wikipedia: http://en.wikipedia.org/wiki/Mediator_pattern
These days, I've seen a URL pattern like this: http://stackoverflow.com/questions/How-to-do Not a usual and
It seems like whenever I use the Command Pattern, it always leads to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.