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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:00:13+00:00 2026-05-27T23:00:13+00:00

#include <stdio.h> #include <errno.h> #include <pthread.h> pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; void *func(void *arg) {

  • 0
#include <stdio.h>
#include <errno.h>
#include <pthread.h>

pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;

void *func(void *arg)
{
        while (1) {
                printf("begin\n");
                pthread_rwlock_wrlock(&rwlock);
                printf("fall through wrlock\n");
                pthread_rwlock_wrlock(&rwlock);
                printf("fall through wrlock\n");
                pthread_rwlock_unlock(&rwlock);
                printf("fall through unlock\n");
                pthread_rwlock_unlock(&rwlock);
                printf("end\n");
        }
}

int main()
{
        pthread_t thd;
        pthread_create(&thd, NULL, func, NULL);

        sleep(100);
}
~                

I write code above on UBUNTU 10.04 and run,
It just output

root@ubuntu:~# ./a.out

begin
fall through wrlock
fall through wrlock
fall through unlock
end
begin

..and finally block here

Why it does not block when the first time
the second
pthread_rwlock_wrlock(&rwlock)
called

  • 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-27T23:00:14+00:00Added an answer on May 27, 2026 at 11:00 pm

    Results are undefined if the calling thread holds the read-write lock (whether a read or write lock) at the time the call is made.

    This is from the Open Group page on pthread_rwlock_wrlock.

    What you’re doing is undefined, at least in that version of the spec. You’re lucky that vicious monkeys didn’t fly in through the window and beat you to death 🙂


    In any case, you really should be checking the return code from all your pthread functions. They can fail, and you really don’t want to continue on the assumption you have a lock when in fact you don’t, since that sort of negates the usefulness of locks.

    That same page linked to above states:

    The pthread_rwlock_wrlock() and pthread_rwlock_trywrlock() functions may fail if:
         [EINVAL] – the value specified by rwlock does not refer to an initialised read-write lock object.
         [EDEADLK] – the current thread already owns the read-write lock for writing or reading.

    I’d be checking to see if you’re actually getting back the EDEADLK error code. This error code is a possibility even in the latest editions of POSIX threads where, as Nemo points out in a comment, the “undefined” wording has been removed.


    One thing is still a worry however. The latest spec states that the “calling thread may deadlock if at the time the call is made it holds the read-write lock” (my bold).

    I see this as an actual deadlock, ie, the thread totally locks up. The EDEADLK error code section also states that the “pthread_rwlock_wrlock() function may fail if …” (again, my bold).

    Both those statements have the weasel word “may” in them, so it’s still not clearly defined which will happen in any given situation. It may (no pun intended) be that this was left unspecified because certain implementations behaved differently (it’s not always easy to detect a deadlock situation in advance) although that’s pure speculation on my part.

    Bottom line, unless you know you have a recursive lock of some sort (where you can relock with impunity), don’t do it.

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

Sidebar

Related Questions

#include<stdio.h> #include<signal.h> #include<stdlib.h> void handler(int signo) { printf(First statement); system(date); exit(EXIT_SUCCESS); } int main()
Here is my code: #include <stdio.h> #include <string.h> #include <errno.h> int cmp(const void *a,
I have the following code: #include <stdlib.h> #include <stdio.h> #include <errno.h> void main(void) {
The following code outputs Illegal seek: #include <stdio.h> #include <errno.h> #include <string.h> int main()
#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
#include<stdio.h> #include<time.h> int main() { clock_t start; double d; long int n,i,j; scanf(%ld,&n); n=100000;
#include <stdio.h> int main() { float a = 1234.5f; printf(%d\n, a); return 0; }
#include<stdio.h> int main() { printf(He %c llo,65); } Output: He A llo #include<stdio.h> int
Consider following example: #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <hiredis/hiredis.h> int main(int argc,
Running the following code on Windows 7 x64 #include <stdio.h> #include <errno.h> int main()

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.