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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:29:52+00:00 2026-06-06T19:29:52+00:00

Unfortunately the cygwin GCC 4.5.3 pthread library implementation doesn’t support the POSIX standard function

  • 0

Unfortunately the cygwin GCC 4.5.3 pthread library implementation doesn’t support the POSIX standard function

int pthread_mutex_timedlock(pthread_mutex_t* mutex, struct timespec* abstime);

Has anyone a good idea how to implement a good workaround for this method in a mutex wrapper class? May be using pthread_mutex_trylock() with a (milliseconds based) nanosleep() call?
I don’t have a good feeling about the latter idea, but anyway the C++ implementation could look like this:

bool MyPosixMutexWrapper::try_lock(const TimeDuration<>& timeout)
{
    if(valid)
    {
        if(timeout == TimeDuration<>::Zero)
        {
             if(pthread_mutex_trylock(&mutexHandle) == 0)
             {
                 return true;
             }
        }
        else
        {
            struct timespec now;
            clock_gettime(CLOCK_REALTIME,&now);
            TimeDuration<> tnow(now);
            tnow += timeout;
            struct timespec until = tnow.getNativeValue();
#if defined(_POSIX_TIMEOUTS)
            if(pthread_mutex_timedlock(&mutexHandle,&until) == 0)
            {
                return true;
            }
#else
            long milliseconds = timeout.milliseconds();
            while(milliseconds > 0)
            {
                 if(pthread_mutex_trylock(&mutexHandle) == 0)
                 {
                     return true;
                 }

                 struct timespec interval;
                 struct timespec remaining;
                 interval.tv_sec = 0;
                 interval.tv_nsec = 1000000;
                 do
                 {
                     remaining.tv_sec = 0;
                     remaining.tv_nsec = 0;
                     if(nanosleep(&interval,&remaining) < 0)
                     {
                         if(errno == EINTR)
                         {
                             interval.tv_sec = remaining.tv_sec;
                             interval.tv_nsec = remaining.tv_nsec;
                         }
                         else
                         {
                             return false;
                         }
                     }
                     clock_gettime(CLOCK_REALTIME,&now);
                     tnow = TimeDuration<>(now);
                     if(tnow >= TimeDuration(until))
                     {
                          return pthread_mutex_trylock(&mutexHandle) == 0;
                     }
                } while(remaining.tv_sec > 0 || remaining.tv_nsec > 0);
                --milliseconds;
            }
#endif
        }
    }
    return pthread_mutex_trylock(&mutexHandle) == 0;
}

Does anyone have a better idea or improvments for this code?

  • 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-06T19:29:54+00:00Added an answer on June 6, 2026 at 7:29 pm

    My suggestion would be to use a pthread_cond_timedwait to mimic your timed lock. The trick here is that timed_mutex_ is never held for very long, since waiting on timed_cond_ releases the lock. timed_mutex_ is also released immediately after locked_ is set or unset.

    struct MutexGuard {
        pthread_mutex_t &mutex_;
        MutexGuard (pthread_mutex_t &m) : mutex_(m) {
            pthread_mutex_lock(&mutex_);
        }
        ~MutexGuard () {
            pthread_mutex_unlock(&mutex_);
        }
    };
    
    struct TimedMutex {
        pthread_mutex_t timed_mutex_;
        pthread_cond_t timed_cond_;
        bool locked_;
    
        TimedMutex ()
            : timed_mutex_(), timed_cond_(), locked_(false) {
            pthread_mutex_init(&timed_mutex_, 0);
            pthread_cond_init(&timed_cond_, 0);
        }
    
        ~TimedMutex () {
            pthread_cond_destroy(&timed_cond_);
            pthread_mutex_destroy(&timed_mutex_);
        }
    
        int lock (const struct timespec *t) {
            MutexGuard g(timed_mutex_);
            while (locked_) {
                int r = pthread_cond_timedwait(&timed_cond_, &timed_mutex_, t);
                if (r < 0) return r;
            }
            locked_ = true;
            return 0;
        }
    
        void lock () {
            MutexGuard g(timed_mutex_);
            while (locked_) {
                pthread_cond_wait(&timed_cond_, &timed_mutex_);
            }
            locked_ = true;
        }
    
        void unlock () {
            MutexGuard g(timed_mutex_);
            locked_ = false;
            pthread_cond_signal(&timed_cond_);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

unfortunately Dreamweaver doesn't support Asp.net mvc, and I find it hard to believe that
Unfortunately, Devel::Cover does not yet work with threads. It doesn't work with prefork either.
Unfortunately ThinkingSphinx is not an option for DataMapper (though they say support is planned).
Unfortunately I still have to support ie6. I currently don't have a machine with
I'm using cp.exe from Cygwin to copy files in Windows 7. Unfortunately, when I
My problem: cygwin git doesn't seem to correctly prompt for credentials when using https://
Unfortunately, I have to use a C library with internal state in my Android
I'm trying to install MySQLdb for python on Cygwin. Unfortunately, when I run python
Unfortunately their Wiki is down for maintenance and the web is not being helpful.
Unfortunately I haven't coded Java for about five years and I absolutely can not

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.