All pastes #68246 Raw Edit

Mine

public text v1 · immutable
#68246 ·published 2006-06-21 08:34 UTC
rendered paste body
#include <iostream>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>

void threadSpawn();
boost::try_mutex myMutex;

int main() {
  boost::thread foo(threadSpawn);
  threadSpawn();
}

void threadSpawn() {
  std::cout << "entering threadSpawn" << std::endl;
  try {
    boost::try_mutex::scoped_try_lock lock(myMutex);
    std::cout << "locked" << std::endl;
    while(true);
  } catch(boost::lock_error) {
    std::cout << "failed to lock" << std::endl;
  }
  std::cout << "leaving threadSpawn" << std::endl;
}



OUTPUT is:
entering threadSpawn
locked
entering threadSpawn
locked

my EXPECTED OUTPUT would have been:
entering threadSpawn
locked
entering threadSpawn
failed to lock
leaving threadSpawn


Substituting the try_lock with a normal lock yields what I would expect:
entering threadSpawn
locked
entering threadSpawn
(end of output, as the lock can't be aquired)