diff options
author | irfan <irfan@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1997-11-10 20:20:32 +0000 |
---|---|---|
committer | irfan <irfan@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1997-11-10 20:20:32 +0000 |
commit | 6db04aa3ddbed41f5b97d34934905f61b294f2ec (patch) | |
tree | c71051f4d739853c8d0299624f2cc227c5a0e70e /tests/Thread_Mutex_Test.cpp | |
parent | f107ec0ab2fb551aaf3bd0ac64fd2c13cc9b91f9 (diff) | |
download | ATCD-6db04aa3ddbed41f5b97d34934905f61b294f2ec.tar.gz |
*** empty log message ***
Diffstat (limited to 'tests/Thread_Mutex_Test.cpp')
-rw-r--r-- | tests/Thread_Mutex_Test.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/Thread_Mutex_Test.cpp b/tests/Thread_Mutex_Test.cpp new file mode 100644 index 00000000000..f55b4d8495d --- /dev/null +++ b/tests/Thread_Mutex_Test.cpp @@ -0,0 +1,78 @@ +// $Id$ + +// ============================================================================ +// +// = LIBRARY +// tests +// +// = FILENAME +// Thread_Mutex_Test.cpp +// +// = DESCRIPTION +// This is a simple test to illustrate the functionality of +// ACE_Thread_Mutex. The test acquires and releases mutexes. No +// command line arguments are needed to run the test. +// +// = AUTHOR +// Prashant Jain and Doug Schmidt +// +// ============================================================================ + +#include "ace/Thread_Manager.h" +#include "test_config.h" + +static void * +test (void *args) +{ + ACE_Thread_Mutex *mutex = (ACE_Thread_Mutex *) args; + ACE_OS::srand (ACE_OS::time (0)); + + for (int i = 0; i < ACE_MAX_ITERATIONS / 2; i++) + { + ACE_DEBUG ((LM_DEBUG, "(%P|%t) = trying to acquire on iteration %d\n", i)); + ACE_ASSERT (mutex->acquire () == 0); + ACE_DEBUG ((LM_DEBUG, "(%P|%t) = acquired on iteration %d\n", i)); + + // Sleep for a random amount of time between 0 and 2 seconds. + // Note that it's ok to use rand() here because we are running + // within the critical section defined by the Thread_Mutex. + ACE_OS::sleep (ACE_OS::rand () % 2); + + ACE_ASSERT (mutex->release () == 0); + ACE_DEBUG ((LM_DEBUG, "(%P|%t) = released on iteration %d\n", i)); + } + + return 0; +} + +static void +spawn (void) +{ +#if defined (ACE_HAS_THREADS) + ACE_Thread_Mutex mutex; + + if (ACE_Thread_Manager::instance ()->spawn_n (ACE_MAX_THREADS, + ACE_THR_FUNC (test), + (void *) &mutex, + THR_NEW_LWP | THR_DETACHED) == -1) + ACE_ERROR ((LM_ERROR, "%p\n%a", "thread create failed")); + + // Wait for the threads to exit. + ACE_Thread_Manager::instance ()->wait (); + +#else + ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n%")); +#endif /* ACE_HAS_THREADS */ +} + +int +main (int, char *[]) +{ + ACE_START_TEST ("Thread_Mutex_Test"); + + spawn (); + + ACE_END_TEST; + return 0; +} + |