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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:05:49+00:00 2026-05-31T13:05:49+00:00

A mutex is required to be unlocked once the thread is canceled, in order

  • 0

A mutex is required to be unlocked once the thread is canceled, in order to avoid deadlock. So I devised the following method:

// file_a.c
pthread_attr_t attr;
...
rc2 = pthread_attr_init(&attr);
ERR_IF( rc2 != 0 );
rc2 = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
ERR_IF( rc2 != 0 );
rc2 = pthread_create(&destroy_thread, &attr, destroy_expired_sessions, NULL);
ERR_IF( rc2 != 0 );
...
pthread_attr_destroy(&attr);

static void *destroy_expired_sessions(void *t)
{
    ...
    (void)t;

    pthread_cleanup_push(cleanup_handler, NULL);
    while (1)
    {
            ... // doing some work here

            sleep(min_timeout);
    }
    pthread_cleanup_pop(0);
}

static void cleanup_handler(void *arg)
{
    (void)arg;

    authSessionListMutexUnlock();
}

// file_b.c
typedef struct
{
    /** Mutex for using this structure. */
    pthread_mutex_t mutex;
    /** The list of Session nodes. */
    cList *list;
} SessionList;

SessionList *globalSessionList = NULL;

...

void authSessionListMutexUnlock()
{
    if (pthread_mutex_trylock(&globalSessionList->mutex) == EBUSY)
        pthread_mutex_unlock(&globalSessionList->mutex);
}

The reason I use pthread_mutex_trylock() here is to avoid a second pthread_mutex_unlock() if the mutex has been unlocked somewhere else.

However, the pthread_mutex_trylock() and pthread_mutex_lock() here caused a segmentation fault.

But, the program here seems innocuous, doesn’t it?

  • 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-31T13:05:50+00:00Added an answer on May 31, 2026 at 1:05 pm

    You forgot to initialize your mutex using pthread_mutex_init().

    From experience, using an un-initialized mutex is a fairly safe bet to crash your program.

    Another option is if you’re attempting to unlock the mutex from another thread than you locked it from. The behavior of that operation is undefined.

    Edit: A quick remark on the trylock/unlock; if the mutex is locked, you’ll get EBUSY and unlock it, if the mutex is free, trylock will succeed in locking it and it won’t be unlocked. In other words, it will toggle the lock/unlock status of the mutex. Is that really as you intended?

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

Sidebar

Related Questions

Is if almost always required to have thread syncing (i.e. use of mutex, semaphores,
I understand recursive mutex allows mutex to be locked more than once without getting
Is Mutex required on 1 byte shared resource? If not then why not? Consider
Code: #include <iostream> #include stdafx.h #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> using namespace std; boost::mutex mut;
I received the following deadlock log via SHOW INNODB STATUS. Can someone care to
boost::condition_variable cond; boost::mutex mutex; //thread #1 for(;;) { D * d = nullptr; while(
The Mutex class is very misunderstood, and Global mutexes even more so. What is
// A Mutex allows threads mutually exclusive access to a resource. //----------------------------------------------------------------------- class Mutex
static Mutex mutex = new Mutex (false, oreilly.com OneAtATimeDemo); static void Main() { //
I am using a global named mutex for file access synchronization between an ASP.NET

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.