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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:34:28+00:00 2026-05-23T17:34:28+00:00

I have a basic question on mutext handling. I have a file which appears

  • 0

I have a basic question on mutext handling.
I have a file which appears at 2 other threads(out of 3). I need to mutually exclude access to it by a thread. So I do the following in a function called from the thread1 handler:

int sub_routine_thread1{

pthread_mutex_lock(&mut)
FILE *fp;
fp = fopen("myfile", "r");
if(fp == NULL){
return -1;
}
pthread_mutex_unlock(&mut)
return 0;
}

As you can see I already know that if the file pointer return a NULL, then my mutex gets locked here and the unlocking never happens. So I changed it to the following:

int sub_routine_thread1{

pthread_mutex_lock(&mut)
FILE *fp;
fp = fopen("myfile", "r");
if(fp == NULL){
   pthread_mutex_unlock(&mut)
   return -1;
}
pthread_mutex_unlock(&mut)
return 0;
}

But after this though I have a bad feeling this is not the way mutex locking should be done. I chanced upon something about cleanup handlers and perhaps thought this is how I must code:

int sub_routine_thread1{
     pthread_cleanup_push(pthread_mutex_unlock, (void *) &mut);
     pthread_mutex_lock(&mut);
     FILE *fp;
     fp = fopen("myfile", "r");
     if(fp == NULL){
     return -1;
     }
     pthread_cleanup_pop(1);
     return 0;
}

Is this the right way? Can you please clarify?

  • 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-23T17:34:29+00:00Added an answer on May 23, 2026 at 5:34 pm

    But after this though I have a bad feeling this is not the way mutex loxking should be done.

    Yes it is. Your second example is perfectly fine. pthread_cleanup_push is used to run functions when the thread is cancelled, that’s not what you should use here.

    Allthough, I’d probably prefer to do something like

    int sub_routine_thread1() {
      FILE *fp;
      int ret = -1;
      pthread_mutex_lock(&mut)
      fp = fopen("myfile", "r");
      if(fp != NULL){
         //do_stuff_unlocked(fp);
         ret = 0;
      }
      pthread_mutex_unlock(&mut)
      return ret;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic question for which I need a some clarification in it's
I have a basic question. If I run an executable file (Release, Visual Studio
I have a basic question. Say you have a NSFetchRequest which you want to
I have a very basic question and need help. I am trying to understand
I have a basic question. I am loading an XML file using the URLLoader
I have a basic question for all of the math experts out there. If
I have just started Rails and have a basic question. I need to add
I have a basic question, in the Django template language how can you tell
I have a basic question about assembly. Why do we bother doing arithmetic operations
I have really basic question. How can I get form id by input element

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.