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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:06:02+00:00 2026-06-16T16:06:02+00:00

I am reading Thread Synchronization from the book Advance Programming in unix environment. In

  • 0

I am reading Thread Synchronization from the book Advance Programming in unix environment.

In this section, there is a example to use mutex with dynamically allocated object. I have some doubts in the same.

Here I am sharing a timeline of events (top to down) happening to explain my doubts:

  1. Thread1 created.
  2. Thread1 create a mutex var initialize it and put it on a global list so that other can use it.
  3. Now Thread1 acquired a lock to use a shared data Structure say ds. Thread1 need to do very large amount of work with ds, i.e. Thread1 is going to acquire this lock for a long time.
  4. Now while Thread1 still acquired the lock, Thread2 is created.
  5. Now Thread2 also want to use ds.
  6. So Thread2 has to first increment the counter showing the reference to ds is increased. To do so (according to the book) it first need to acquire a lock using the same mutex_t variable before incrementing the count.
  7. But as Thread1 already acquired a lock on this mutex_t variable, so when Thread2 call lock() before incrementig the count, it will have to wait till Thread1 unlock the lock.

Doubts:

  1. About which global list he is talking about (means just to make any list and pass reference of it to all the threads or any specific list)?
  2. When Thread1 created the lock variable it set the count to 1. Then Thread2 is waiting to increment this count to 2. But suppose a situation in which after doing the current work Thread1 didn’t need to use the ds. So before unlocking it also decrease the count or first unlock it then call foo_rele() to again lock and decrease the count. Now is it possible that before Thread2 increment the count, Thread1 decrements it. If yes (according to me) then my data structure will be destroyed? So I think there is a slight error in this example of the book. It would be better if we use different mutex_var to increment the count?
  • 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-16T16:06:03+00:00Added an answer on June 16, 2026 at 4:06 pm

    A. I think that under the term “global list” author understands all variables that are shared between threads.

    Example:

    struct foo* shared_foo; /* This pointer is shared between all threads */
    
    struct foo* foo_alloc(void)
    {
       /* This pointer is local to the thread which allocates the memory */
       struct foo *fp;
    
        if ((fp = malloc(sizeof(struct foo))) != NULL) {
            /* whatever */
        }
        /* local pointer value returned */
        return(fp);
    }
    
    /* probably somewhere in the code the shared pointer (on the 'global list') is initialized this way */
    shared_foo = foo_alloc();
    

    B. Hmm… I don’t really undestand what you say. Could you please write your scenario as a list? In my opinion f_count is set during initialization as a flag ‘This mutex is in use’. So when the mutex is free the f_count value is set to 1. When the Thread1 acquires the lock it’s value is set to 2. When it releases the lock the value is set back to 1. Valid f_count values are: 1 (initalized and free) and 2 (initialized and busy). In order to release the mutex you simply have to call two times foo_rele when it’s taken (f_count = 2) or once when it’s free (f_count = 1). Then the f_count value reaches 0 and the mutex is removed.

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

Sidebar

Related Questions

Are there any concurrency problems with one thread reading from one index of an
I was just reading this thread and it occurred to me that there is
Could I close tcp::socket in different thread from the sync-reading thread? It looks like:
I was reading through this thread: Hidden Features of JavaScript? and found this post:
Are 'boolean' variables thread-safe for reading and writing from any thread? I've seen some
I have been reading about the kernel using timers for thread synchronization. I haven't
I am reading data from a HID device using hiddev , there is a
I am reading the book Java Concurrecny in practice. On page 85 section 5.2.1
I'm reading APUE and I am confused with thread synchronization of chapter 11. Below
This is an interview question. How do you implement a read/write mutex? There will

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.