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 8162245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:39:55+00:00 2026-06-06T18:39:55+00:00

I have some thread to write resource and some to read it.But pthread_rwlock cause

  • 0

I have some thread to write resource and some to read it.But pthread_rwlock cause a lot of context switch. So I imagine a way to avoid it. But I’m not sure it is safe or not.

This is the code:

sig_atomic_t slot = 0;

struct resource {
    sig_atomic_t in_use;  /*Counter,if in_use, not zero*/
    .....
} xxx[2];

int read_thread()
{
    i = slot; /*avoid slot changes in process */
    xxx[i].in_use++;
    read(xxx[i]);
    xxx[i].in_use--;
}

int write_thread()
{
    mutex_lock;  /*mutex between write threads */

    if (slot == 0) {
    while(xxx[1].in_use != 0);  /*wait last read thread in slot 1*/
    clear(xxx[1]);
    write(xxx[1]);
    slot = 1;
    } else if (slot == 1) {
    while(xxx[0].in_use != 0);
    clear(xxx[0]);
    write(xxx[0]);
    slot = 0;
    }

    mutex_unlock;
}

Will that works? The cost is 2 times storage and 3 atomic variable.
Thanks a lot!

  • 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-06T18:39:55+00:00Added an answer on June 6, 2026 at 6:39 pm

    Your strategy is to have writers write to a different slot than what the readers are reading from. And you are switching the reading slot number after a write is completed. However, you will have a race.

    slot    reader             writer1            writer2
    ----    ------             -------            -------
    0                          mutex_lock
            i = 0
                               ... slot=1
    1                          mutex_unlock       mutex_lock
                                                  ... clear(xxx[0])
            xxx[0].in_use++
            read(xxx[0])                          write(xxx[0])
    

    In general, though, this strategy could lead to starvation of writers (that is a writer may spin forever).

    However, if you are willing to tolerate that, it would be safer to let xxx[] be an array of 2 pointers to resource. Let the reader always read from xxx[0], and let the writers contend for updates on xxx[1]. When a writer is finished updating xxx[1], it uses CAS on xxx[0] and xxx[1].

    struct resource {
        sig_atomic_t in_use;  /*Counter,if in_use, not zero*/
        sig_atomic_t writer;
        .....
    } *xxx[2];
    
    void read_thread()
    {
        resource *p = xxx[0];
        p->in_use++;
        while (p->writer) {
            p->in_use--;
            p = xxx[0];
            p->in_use++;
        }
        read(*p);
        p->in_use--;
    }
    
    void write_thread()
    {
        resource *p;
        mutex_lock;  /*mutex between write threads */
        xxx[1]->writer = 1;
    
        while(xxx[1]->in_use != 0);  /*wait last read thread in slot 1*/
        clear(xxx[1]);
        write(xxx[1]);
        xxx[1] = CAS(&xxx[0], p = xxx[0], xxx[1]);
        assert(p == xxx[1]);
    
        xxx[0]->writer = 0;
        mutex_unlock;
    }
    

    If you want to avoid writer starvation, but you want the performance of spinlocks, you are looking at implementing your own reader/writer locks using spinlocks instead of mutex locks. A google search for “read write spinlock implementation” pointed to this page which I found to be an interesting read.

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

Sidebar

Related Questions

I posted a thread about this earlier and have made some progress but now
I have to execute some code in the context of the main thread. I
Suppose we have some resource(a file on disk) in which we have to write
I have some Thread count pCount and I have some float[] array. I want
I have some code where I use a thread static object in C#. [ThreadStatic]
I have some BitmapFrames created on a thread other than the main UI thread;
I have some code that makes heavy use of a thread pool, which I
I have a java code that looks like this: //UI thread //Some code Job
I have a background thread and the thread calls some methods that update the
I know that there are some threads have a similar issue with this thread.

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.