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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:41:48+00:00 2026-06-04T07:41:48+00:00

I am a little bit confused trying to implement a very simple mutex (lock)

  • 0

I am a little bit confused trying to implement a very simple mutex (lock) in C. I understand that a mutex is similar to a binary semaphore, except that the mutex also enforces the constraint that the thread that releases the lock, must be the same thread that most recently acquired it. I am confused on how the ownership is kept track of?

This is what I have so far. Keep in mind that it is not completed yet, and is suppose to be really simple (uniprocessor, no recursion on mutex, disabling interrupts as mutual exclusion method, etc).

struct mutex {
    char *mutexName;
    volatile int inUse;
};

I believe I should add in another member variable, i.e., whoIsOwner, but I am kind of confused as what to store there. I assume it has to be something that can uniquely identify the thread trying to call the lock? Is this correct?

I have a thread structure in place that has a “char *threadName” member variable (along with others), but I’m not sure how I would access this from within the mutex implementation.

Any pointers/hints/ideas would be appreciated.

  • 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-04T07:41:49+00:00Added an answer on June 4, 2026 at 7:41 am

    You could implement the mutex as an atomic integer which is 0 when unlocked, and which takes the value of the locking thread’s ID to indicate it’s locked. Of course access to the variable has to be atomic, and suitably fenced to prevent reordering (acquire-release fence pairs suffice).

    Ultimately you can of course never prevent yourself from shooting yourself in the foot; if you really want you can overwrite the mutex’s memory by force from another thread, or something like that. You’ll only get the correct behaviour if you use the tools correctly. With that in mind, you might be satisfied with a simple bool for the locking variable.

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

Sidebar

Related Questions

I am little bit confused about this statement that I read in a book
Ok, I'm a little bit confused here. I'm trying to select a DAO class
I am little bit confused of that what happened because it's illogical for me.
I have a question that I am a little bit confused about. I am
I'm trying to understand Android's View structure and I'm a little confused on how
Im a little bit confused about passing structs into functions. I understand pointers and
I'm a little bit confused about something. I was under the impression that the
I'm struggling a bit trying to achieve something that appeared simple initially to me.
Little bit confused... I am trying to track mailto links being clicked, but constantly
I'm trying to learn GUI in Java, but I'm a little bit confused and

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.