Thread: boost::mutex error

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    boost::mutex error

    Hi,
    I'm learning boost::thread from a tutorial. This is the code from that tutorial:
    Code:
    #include <boost/thread.hpp>
    #include <boost/date_time.hpp>
    #include <iostream>
    
    boost::mutex mutex;
    
    void wait(int seconds)
    {
        boost::this_thread::sleep(boost::posix_time::seconds(seconds));
    }
    
    void thread()
    {
        try
        {
            for( int i = 0; i < 5; i++)
            {
                wait(1);
                boost::lock_guard<boost::mutex> lock(mutex);
                std::cout<<"Thread "
                    << boost::this_thread::get_id()
                    << ": " << i <<std::endl;
                mutex.unlock();
    
            }
        }
        catch(boost::thread_interrupted&)
        {
            std::cout<<"Thread Interrupted" << std::endl;
        }
    }
    
    int main()
    {
        boost::thread t1(thread);
        boost::thread t2(thread);
    
        t1.join();
        t2.join();
    
        return 0;
    }
    This code ran then crashed at the end:
    Thread 0x12c22a0: 0
    Thread 0x12c2010: 0
    Thread 0x12c22a0: 1
    Thread 0x12c2010: 1
    Thread 0x12c22a0: 2
    Thread 0x12c2010: 2
    Thread 0x12c22a0: 3
    Thread 0x12c2010: 3
    Thread 0x12c22a0: 4
    Thread 0x12c2010: 4
    /usr/include/boost/thread/pthread/mutex.hpp:45: boost::mutex::~mutex(): Assertion `!pthread_mutex_destroy(&m)' failed.
    why comes this crash the program? If i use boost::mutex::lock() and unlock() wouldn't crash the program, it won't crash.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The lock_guard object will call lock in its constructor, and unlock in its destructor. You don't need the mutex.unlock call.

    Synchronization - Boost 1.46.1

    gg

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    I didn't call mutex.lock() and unlocked() with lock_guard(). I get the error when I use lock_guard() by itself.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    In the code in post #1, you need to remove the "mutex.unlock()" call to make the code correct.

    gg

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Aite. I swear that I took unlocked() out, scanned it so many time and didn't see it at all. Thanks for your help.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange linker error with minGW and boost
    By pheres in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2009, 08:45 PM
  2. Using boost::bind - error, help?
    By Elysia in forum C++ Programming
    Replies: 2
    Last Post: 02-12-2009, 01:33 PM
  3. Boost Regex rule run-time error
    By Hunter0000 in forum C++ Programming
    Replies: 0
    Last Post: 01-07-2008, 11:51 AM
  4. boost::mutex vs criticalsection
    By l2u in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2007, 04:30 AM
  5. link error: with Boost date_time library
    By Hotman_x in forum C++ Programming
    Replies: 0
    Last Post: 01-14-2003, 07:54 AM