Sunday, June 14, 2009, 09:29 AM - Just a thought
One more effect of recession. Children at school now read "Alibaba and the thirty thieves".Happy reading.//




( 3.1 / 164 )





( 3.1 / 164 )
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/locks.hpp>
#include <sys/types.h>
#include <linux/unistd.h>
_syscall0(pid_t,gettid)
boost::timed_mutex m_mutex;
void Thread1_CreateObjectAndWait() {
//Get the lock
boost::timed_mutex::scoped_lock guard(m_mutex);
std::cout << gettid() << ": Thread got the lock" << std::endl;
boost::system_time timeout = boost::get_system_time() +
boost::posix_time::milliseconds(10000);
std::cout << gettid()
<< ": Waiting for thread 2 to unlock or else timeout"
<< std::endl;
bool ret = m_mutex.timed_lock(timeout);
if (ret == true) {
std::cout << gettid() << ": Got the lock before timeout"
<< std::endl;
} else {
std::cout << gettid() << ": Got the lock after timeout"
<< std::endl;
}
}
void Thread2_Unlock() {
std::cout << gettid() << " : Unlocking the mutex " << std::endl;
m_mutex.unlock() ;
}
int main() {
//Create thread 1, which will lock itself and wait for thread 2
//to unlock or timeout.
boost::thread thread1(&Thread1_CreateObjectAndWait);
//sleep some time to give time for thread 1 to start.
sleep(2);
//create thread 2 which will unlock before thread 1 gets a timeout.
boost::thread thread2(&Thread2_Unlock);
//Create thread 3, which will lock itself and wait for thread 2
//to unlock or timeout.
boost::thread thread3(&Thread1_CreateObjectAndWait);
thread1.join();
thread2.join();
thread3.join();
return 0;
}
g++ example.cpp -I<path to boost include dir> <path to boost lib dir>/libboost_thread-gcc34-mt.a -lpthread
28061: Thread got the lock
28061: Waiting for thread 2 to unlock or else timeout
28062 : Unlocking the mutex
28061: Got the lock before timeout
28063: Thread got the lock
28063: Waiting for thread 2 to unlock or else timeout
28063: Got the lock after timeout
while [[ $a = 0* ]]
do
a=${a#0}
done
y=081
let x=10#$y*7; #Treat 08 as a base 10 digit.