1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
// $Id: token.cpp 91671 2010-09-08 18:39:23Z johnnyw $
// Test out the ACE Token class.
#include "ace/OS_main.h"
#include "ace/Token.h"
#include "ace/Task.h"
#include "ace/OS_NS_time.h"
#if defined (ACE_HAS_THREADS)
class My_Task : public ACE_Task<ACE_MT_SYNCH>
{
public:
My_Task (int n);
virtual int svc (void);
static void sleep_hook (void *);
private:
ACE_Token token_;
};
My_Task::My_Task (int n)
{
// Make this Task into an Active Object.
this->activate (THR_BOUND | THR_DETACHED, n);
// Wait for all the threads to exit.
this->thr_mgr ()->wait ();
}
void
My_Task::sleep_hook (void *)
{
ACE_DEBUG ((LM_ERROR, "(%u) blocking, My_Task::sleep_hook () called\n",
ACE_Thread::self())) ;
}
// Test out the behavior of the ACE_Token class.
int
My_Task::svc (void)
{
for (size_t i = 0; i < 100; i++)
{
// Wait for up to 1 millisecond past the current time to get the token.
ACE_Time_Value timeout (ACE_OS::time (0), 1000);
if (this->token_.acquire (&My_Task::sleep_hook, 0, &timeout) == 1)
{
this->token_.acquire ();
this->token_.renew ();
this->token_.release ();
this->token_.release ();
}
else
ACE_Thread::yield ();
}
return 0;
}
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
My_Task tasks (argc > 1 ? ACE_OS::atoi (argv[1]) : 4);
return 0;
}
#else
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_ERROR_RETURN ((LM_ERROR, "your platform doesn't support threads\n"), -1);
}
#endif /* */
|