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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:41:08+00:00 2026-06-18T11:41:08+00:00

I have a C application that uses pthreads. There is a lock contention between

  • 0

I have a C application that uses pthreads.

There is a lock contention between two threads(say A and B) where A gets the lock first while B is waiting for the lock, Once A is done and releases the lock, B still doesn’t get it and after a while A gets the lock again(A does acquire and release in a loop).
If I attach my process to gdb and pause thread A after it has given up the lock and manually continue on thread B, it then gets it and does what is needed.

This does not look like a dead lock to me.
What could be preventing thread B from getting the lock? Any help is greatly appreciated.

Sample Code:

Thread A:

while (true)  
{  
    lock.acquire(lock)  
    // Do stuff  
    lock.release(lock)  
    // Do more stuff  
}  

Thread B:

lock.acquire(lock)  
// Do some stuff  
lock.release(lock)  
  • 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-18T11:41:09+00:00Added an answer on June 18, 2026 at 11:41 am

    It looks that you algorithm suffers from starvation, you should queue your access to the lock, see

    pthreads: thread starvation caused by quick re-locking

    or

    Fair critical section (Linux)

    As an answer to the comment, what is a mutex (pthread library)

    A mutex is a lock (from Pthread library) that guarantees the following
    three things:

    Atomicity – Locking a mutex is an atomic operation,
    meaning that the threads library assures you that if you lock a mutex,
    no other thread can succeed in locking that mutex at the same time.

    Singularity – If a thread managed to lock a mutex, it is assured that
    no other thread will be able to lock the same mutex until the original
    thread releases the lock.

    Non-Busy Wait – If threadA attempts to lock a mutex that was locked
    by threadB, the threadA will get suspended (and will not consume any
    CPU resources) until the lock is freed by threadB. When threadB
    unlocks the mutex, then the threadA will wake up and continue
    execution, having the mutex locked by it.

    It do not guaranty fairness.

    If you are still interested in sort of reader writer fairness for pthread_rwlock_rdlock:
    which are allowed to favour writers over readers to avoid writer starvation.

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

Sidebar

Related Questions

I have an application that uses the Google Maps API to geocode distances between
I have an application that uses currently two DLL's for external libraries (AjaxControlToolkit is
I have an application that uses a DLL to make API calls. Is there
I have an application that uses a ResourceDictionary to set the styles, which it
I have an application that uses Hibernate. It generated the following SQL statement. I
I have an application that uses multiple WebViews. Nowhere do I set the priority
I have an application that uses RPC for interprocess communications. Turns out that synchronous
I have an application that uses rest to communicate to a server, i would
I have an application that uses CoreData. I previously had a class named Marker
I have an application that uses the Paperclip plugin for image upload. Now that

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.