All pastes #68311 Raw Edit

Someone

public text v1 · immutable
#68311 ·published 2006-06-21 11:50 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;
  boost::try_mutex::scoped_try_lock lock(myMutex);
  if(lock.locked()) {
    std::cout << "locked" << std::endl;
    while(true);
  } else {
    std::cout << "failed to lock" << std::endl;
  }
  std::cout << "leaving threadSpawn" << std::endl;
}


OUTPUT (as expected):
entering threadSpawn
locked
entering threadSpawn
failed to lock
leaving threadSpawn