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

  • SEARCH
  • Home
  • 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 9231171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:00:23+00:00 2026-06-18T06:00:23+00:00

I have the following scenario: I have multiple worker threads running that all go

  • 0

I have the following scenario:

I have multiple worker threads running that all go through a certain section of code, and they’re allowed to do so simultaneously. No critical section surrounds this piece of code right now as it’s not required for these threads.

I have a main thread that also -occassionally- wants to enter that section of code, but when it does, none of the other worker threads should use that section of code.

Naive solution: surround the section of code with a critical section. But that would kill a lot of parallelism between the worker threads, which is important in my case.

Is there a better solution?

  • 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-06-18T06:00:24+00:00Added an answer on June 18, 2026 at 6:00 am

    Use RW locks. RW locks allow multiple readers and only a single writer. Your workers would call read-lock at the start of the critical section and the main thread would write-lock.

    By definition, when calling read-lock, the calling process will wait for any writing threads to finish. When calling write-lock, the calling process will wait for any reading or writing threads to finish.

    Example using POSIX threads:

    pthread_rwlock_t lock;
    /* worker threads */
    void *do_work(void *args) {
        for (int i = 0; i < 100; ++i) {
            pthread_rwlock_rdlock(&lock);
            // do some work...
            pthread_rwlock_unlock(&lock);
            sleep(1);
        }
        pthread_exit(0);
    }
    
    /* main thread */
    int main(void) {
        pthread_t workers[4];
        pthread_rwlock_init(&lock);
        int i;
    
        // spawn workers...
        for (i = 0; i < 4; ++i) {
            pthread_create(workers[i]; NULL, do_worker, NULL);
        }
        for (i = 0; i < 100, ++i) {
            pthread_rwlock_wrlock(&lock);
            // do some work...
            pthread_rwlock_unlock(&lock);
            sleep(1);
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I'm looking for advice over the following scenario: I have a component running
I have the following scenario. The client code only has access to FooHandler, not
I have the following scenario. I have a combobox where multiple selection is available.
Afternoon all. I have the following scenario: I have a search page where by
I have the following scenario: 1) A phone book table 2) Multiple ways of
I've got an issue with the non-allowed multiple inheritance in C# Following scenario: class
I'm stuck with the following scenario. It has a code smell, that I'm not
We have the following scenario using the .NET RabbitMQ library: A worker thread picks
I have the following scenario: Data from multiple video files needs to be held
I have the following scenario: A Doctor can have multiple Companions . A Companion

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.