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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:27:14+00:00 2026-06-02T11:27:14+00:00

I’m working on a project in C that involves creating a user-level thread library

  • 0

I’m working on a project in C that involves creating a user-level thread library by overriding pthread.h. I’m currently working on the mutex functions.

In my implementation, I store mutexes as a linked list of structs. What I would like to do in pthread_mutex_init is store the pointer to the element that is the newly created mutex in the pthread_mutex_t mutex variable. Is this possible, though? Here’s some of my code so you get an idea of what I’m trying to do:

typedef struct mutexList
{
    int mid;
    struct mutexList *prevMutex;
    struct mutexList *nextMutex;
    int locked;
    struct queueItem *owner;
    struct blockedThread *blockedHead;
    struct blockedThread *blockedTail;
} mutexElement;

int mutexCounter = 0;

mutexElement *mutexHead = NULL;
mutexElement *mutexTail = NULL;

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
{
    mutexElement *newMutex = (mutexElement *) malloc(sizeof(mutexElement));

    fprintf(stdout, "***LOG: Creating new mutex.\n");

    if(newMutex == NULL)
        return ENOMEM;
    newMutex->mid = mutexCounter;
    mutexCounter++;
    newMutex->locked = 0;
    newMutex->nextMutex = NULL;
    newMutex->blockedHead = NULL;
    newMutex->blockedTail = NULL;

    if(mutexHead == NULL)
        mutexHead = newMutex;

    if(mutexTail != NULL)
        mutexTail->nextMutex = newMutex;

    mutexTail = newMutex;

    mutex = (&newMutex);

    return 0;
}
  • 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-02T11:27:17+00:00Added an answer on June 2, 2026 at 11:27 am

    I think your general idea can work, but there are at least a few of problems with your particular example:

    • your mutexCounter , mutexHead and mutexTail items aren’t handled in a thread-safe manner (an important property for this kind of library)
    • the line mutex = (&newMutex); is pointless – mutex is a parameter that is a copy of the user’s argument. When your function returns, mutex simply ceases to exist; it doesn’t get returned to the caller.
    • even trying to return &newMutex to the user in the first place is a problem: newMutex is a local variable that will expire when the function returns. Storing a pointer to it in something that’s supposed to live past the current function call’s duration won’t work.

    Note that I’m not saying that the above is an exhaustive list of problems – they’re just ones that jumped out off the bat.

    You could make the pthread_mutex_t type a typedef for a struct mutexList* and place the value of newMutex (not the address of newMutex) in *mutex since mutex would be a struct mutexList**. The user’s pthread_mutex_t variable would contain a pointer to the structure that’s on your linked list.

    If the user neglects to call pthread_mutex_destroy() and lets his pthread_mutex_t variable go out of scope, then the struct mutexList element that was allocated would never be freed, but that’s a user error that you can really do nothing about.

    As far as mutex initialization goes, there’s another big problem that you’d still need to think about – static initialization. The user is allowed to do something like:

    // the following is at file scope, so it has static duration
    pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER;
    

    In this case, the user doesn’t need to call pthread_mutex_init() – in fact, it would be an error to do so. Your library will need to detect and perform whatever appropriate dynamic initialization is needed for these statically initialized mutexes on first use (or something equivalent). And you have to do it in a thread safe manner – the first use is not only allowed to be done on arbitrary threads, one of the main use cases for these static mutexes is to permit contention on the mutex right off the bat (for example to perform more complex initialization of other static data by the first thread that happens to get to it).

    To summarize, I think the general approach of keeping a container of private mutex data structures and having the user’s pthread_mutex_t variables be essentially pointers (or some other indirect reference) to the appropriate item in the container is fine. However, there are a lot of complex details – especially making sure your functions are thread safe – that need to be considered and designed for.

    The kinds of problems that existed in the example indicate that the design so far hasn’t been given nearly enough thought. That might be OK – maybe you just started on it; just don’t underestimate the attention to detail you’ll need to give these things.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but

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.