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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:41:40+00:00 2026-06-17T12:41:40+00:00

I was fiddling around with some code in c++ that for some reason didn’t

  • 0

I was fiddling around with some code in c++ that for some reason didn’t want to work and i narrowed it down to this case:

#include <thread>
#include <atomic>
#include <chrono>
#include <mutex>
#include <iostream>

using namespace std;

void test()
{
  timed_mutex m;
  m.lock();
  std::cout << "Can i have the lock? " << m.try_lock() << std::endl;
  std::cout << "in test(), should block for 10 seconds" << std::endl;
  bool got_lock = m.try_lock_for(std::chrono::seconds(10));
  std::cout << "Now i've blocked, got the lock: " << got_lock << std::endl;
  m.unlock();
}

int main()
{
  thread t = thread(&test);
  t.join();

  return EXIT_SUCCESS;
}

The problem is that test() doesn’t block at all, even though the try_lock returns false. Is there something i have overlooked or is this a bug in gcc or where should i go next to find out what’s wrong? Thankful for any advice and help!

I compiled this little program like so: g++ -pthread -std=c++11 threads.cpp -o threads
and if it’s any help this is the version of gcc and my os:

g++ --version
g++ (GCC) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

uname -a
Linux *computername* 3.6.11-1-ARCH #1 SMP PREEMPT Tue Dec 18 08:57:15 CET 2012 x86_64 GNU/Linux
  • 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-17T12:41:41+00:00Added an answer on June 17, 2026 at 12:41 pm

    Your code’s behavior is undefined. std::timed_mutex has non-recursive ownership semantics.
    It’s forbidden to acquire the lock (include try_lock family) second time on the same thread.

    C++11 Standard 30.4.1.3.1 [thread.timedmutex.class]/p3/b2: (thanks to Howard Hinnant)

    3 The behavior of a program is undefined if:

    • a thread that owns a timed_mutex object calls lock(), try_lock(), try_lock_for(), or try_lock_until() on that object, or

    C++11 Standard 30.4.1.2 [thread.mutex.requirements.mutex]/p6-7:


    EDITED:

    how i am going to "work around this" or get it to behave the way i want? Should i use a recursive mutex instead?

    Generally speaking, it’s discouraged to acquire/release lock of mutex object in light of exception safty. If you use unique_lock object instead, owns_lock() member function may help you.
    Meanwhile recursive-mutex is useless for your purpose, because "recursive" means only "I(a thread) can acquire lock twice or more when I already own lock."

    void test()
    {
      std::timed_mutex m;
      std::unique_lock<decltype(m)> lk(m, std::defer_lock);
    
      // acquire lock
      lk.lock();
      // You can query locked status via unique_lock object
      std::cout << "Do I have own lock? " << lk.owns_lock() << std::endl;
      // release lock
      lk.unlock();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been fiddling around with this for quite some time. I can't work out
I'm fiddling around with a possible way to run some chunks of this stuff
Good Sunday morning! After fiddling around with some JavaScript this weekend, I finally wrote
While fiddling around to try to automate some process, I ran into this seemingly
I am writing some code that will prepare my database for my application. In
Remember, this is using python. Well, I was fiddling around with an app I
This one seems painfully obvious to me, but for some reason I can't get
Background Info I'm fiddling around with some PHP and AJAX at the moment, to
I am fiddling around with .htaccess and mod_rewrite. I have a site that has
I've been fiddling around with some sorting algorithms and timing them to see just

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.