Author Topic: deadlock detection  (Read 63916 times)

kjellkod

  • Newbie
  • *
  • Posts: 2
    • View Profile
deadlock detection
« on: September 07, 2010, 02:38:04 PM »
Hi,

I tried the deadlock detection sample in the documentation
I compiled as instructed
g++ -std=c++0x -pthread -D_JUST_THREAD_DEADLOCK_CHECK -g -rdynamic -o sample_deadlockcheck sample_deadlock.cpp -ljustthread -lrt

When running the program I do get the deadlock but I don't get the deadlock detection prinouts AND the app does not terminate (as it should with std::terminate), to make sure the standard error stream wasn't lost on me I called the sample like this
 ./sample_deadlockcheck  &>  log.out

I have not tested this on Windows yet, this was on Ubuntu

Best Regards
Kjell Hedström

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: deadlock detection
« Reply #1 on: September 07, 2010, 03:14:56 PM »
Oops. The documentation needs updating.  :(

You need to ensure that the just::thread headers are being used, and that the checked version of the library is being linked.

Code: [Select]
g++ -std=c++0x -pthread -I/usr/include/justthread -D_JUST_THREAD_DEADLOCK_CHECK -g -rdynamic -o sample_deadlockcheck sample_deadlock.cpp -ljustthread_check
If you don't link against the checked version of the library then you'll get linker errors if the include path is set up correctly.

Also, if you move the lock in main() to before the creation of the thread then you'll get a deadlock every time:

Code: [Select]
#include <thread>
#include <mutex>
#include <iostream>

std::mutex io_mutex;

void thread_func()
{
    std::lock_guard<std::mutex> lk(io_mutex);
    std::cout<<"Hello from thread_func"<<std::endl;
}

int main()
{
    std::lock_guard<std::mutex> lk(io_mutex);
    std::thread t(thread_func);
    std::cout<<"Hello from main thread"<<std::endl;
    t.join();
    return 0;
}

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: deadlock detection
« Reply #2 on: September 07, 2010, 10:42:05 PM »
I have updated the online documentation at http://www.stdthread.co.uk/doc/deadlockdebug.html

kjellkod

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: deadlock detection
« Reply #3 on: September 08, 2010, 07:39:57 AM »
Thank you Anthony, that did it  :D