diff options
author | schmidt <douglascraigschmidt@users.noreply.github.com> | 1997-05-14 22:48:54 +0000 |
---|---|---|
committer | schmidt <douglascraigschmidt@users.noreply.github.com> | 1997-05-14 22:48:54 +0000 |
commit | 7f112ff6968e4fee2a38bcca1a6c1f6630d75e37 (patch) | |
tree | 78778590b5c6c5e56fd628273ce5a8d6130d6ac1 /examples | |
parent | ec23ba492313939a7bc4c3482c7bdccb7cecb9ea (diff) | |
download | ATCD-7f112ff6968e4fee2a38bcca1a6c1f6630d75e37.tar.gz |
*** empty log message ***
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Threads/thread_specific.cpp | 41 | ||||
-rw-r--r-- | examples/Threads/tss2.cpp | 44 |
2 files changed, 53 insertions, 32 deletions
diff --git a/examples/Threads/thread_specific.cpp b/examples/Threads/thread_specific.cpp index 290cc4a4fa4..1432eb77474 100644 --- a/examples/Threads/thread_specific.cpp +++ b/examples/Threads/thread_specific.cpp @@ -1,16 +1,16 @@ -#include "ace/Service_Config.h" // $Id$ +#include "ace/Service_Config.h" #include "ace/Synch.h" #if defined (ACE_HAS_THREADS) // Define a class that will be stored in thread-specific data. Note // that as far as this class is concerned it's just a regular C++ -// class. The ACE_TSS wrapper transparently ensures that -// objects of this class will be placed in thread-specific storage. -// All calls on ACE_TSS::operator->() are delegated to the -// appropriate method in the Errno class. +// class. The ACE_TSS wrapper transparently ensures that objects of +// this class will be placed in thread-specific storage. All calls on +// ACE_TSS::operator->() are delegated to the appropriate method in +// the Errno class. class Errno { @@ -55,17 +55,14 @@ ACE_MT (ACE_Thread_Mutex Errno::lock_); int Errno::flags_; // This is our thread-specific error handler... -static ACE_TSS<Errno> TSS_Error; +static ACE_TSS<Errno> tss_error; -#if defined (ACE_HAS_THREADS) // Serializes output via cout. -static ACE_Thread_Mutex lock; +static ACE_SYNCH_MUTEX lock; +#if defined (ACE_HAS_THREADS) typedef ACE_TSS_Guard<ACE_Thread_Mutex> GUARD; #else -// Serializes output via cout. -static ACE_Null_Mutex lock; - typedef ACE_Guard<ACE_Null_Mutex> GUARD; #endif /* ACE_HAS_THREADS */ @@ -94,17 +91,17 @@ worker (void *c) if (ACE_OS::thr_keycreate (&key, cleanup) == -1) ACE_ERROR ((LM_ERROR, "(%t) %p\n", "ACE_OS::thr_keycreate")); - ip = new int; + ACE_NEW_RETURN (ip, int, 0); if (ACE_OS::thr_setspecific (key, (void *) ip) == -1) ACE_ERROR ((LM_ERROR, "(%t) %p\n", "ACE_OS::thr_setspecific")); - for (int i = 0; i < count; i++) + for (size_t i = 0; i < count; i++) { if (ACE_OS::thr_keycreate (&key, cleanup) == -1) ACE_ERROR ((LM_ERROR, "(%t) %p\n", "ACE_OS::thr_keycreate")); - ip = new int; + ACE_NEW_RETURN (ip, int, 0); ACE_DEBUG ((LM_DEBUG, "(%t) in worker 1, key = %d, ip = %x\n", key, ip)); @@ -126,21 +123,21 @@ worker (void *c) ACE_OS::read (ACE_INVALID_HANDLE, 0, 0); // The following two lines set the thread-specific state. - TSS_Error->error (errno); - TSS_Error->line (__LINE__); + tss_error->error (errno); + tss_error->line (__LINE__); // This sets the static state (note how C++ makes it easy to do // both). - TSS_Error->flags (count); + tss_error->flags (count); { // Use the guard to serialize access to cout... ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, lock, 0); cout << "(" << ACE_Thread::self () - << ") errno = " << TSS_Error->error () - << ", lineno = " << TSS_Error->line () - << ", flags = " << TSS_Error->flags () + << ") errno = " << tss_error->error () + << ", lineno = " << tss_error->line () + << ", flags = " << tss_error->flags () << endl; } key = 0; @@ -148,7 +145,7 @@ worker (void *c) if (ACE_OS::thr_keycreate (&key, cleanup) == -1) ACE_ERROR ((LM_ERROR, "(%t) %p\n", "ACE_OS::thr_keycreate")); - ip = new int; + ACE_NEW_RETURN (ip, int, 0); ACE_DEBUG ((LM_DEBUG, "(%t) in worker 2, key = %d, ip = %x\n", key, ip)); @@ -196,7 +193,7 @@ main (int argc, char *argv[]) ACE_THR_FUNC (&worker), (void *) count, THR_BOUND | THR_DETACHED) == -1) - ACE_OS::perror ("ACE_Thread_Manager::spawn_n"); + ACE_ERROR ((LM_ERROR, "%p\n", "ACE_Thread_Manager::spawn_n"), -1); ACE_Service_Config::thr_mgr ()->wait (); #else diff --git a/examples/Threads/tss2.cpp b/examples/Threads/tss2.cpp index 41cd4b00fd4..1b02dfe5be6 100644 --- a/examples/Threads/tss2.cpp +++ b/examples/Threads/tss2.cpp @@ -9,8 +9,8 @@ // TSS_Test.cpp // // = DESCRIPTION -// This program tests various features of ACE_Thread and thread -// specific storage of data. +// This program tests various features of ACE_Thread and the +// thread-specific storage variant of <ACE_SingletonEx>. // // = AUTHOR // Prashant Jain and Doug Schmidt @@ -19,12 +19,29 @@ #include "ace/Task.h" #include "ace/Token.h" +#include "ace/Singleton.h" #if defined (ACE_HAS_THREADS) const int MAX_TASKS = 4; const int MAX_ITERATIONS = 10; +class TSS_Data + // = TITLE + // Data that is stored in thread-specific storage. +{ +public: + void *data (void) { return this->data_; } + void data (void *v) { this->data_ = v; } + +private: + // = data_ will be thread-specific data so it doesn't need a lock. + void *data_; +}; + +typedef ACE_SingletonEx<TSS_Data, ACE_SYNCH_MUTEX, ACE_SINGLETON_TSS> + TSS_DATA; + class TSS_Obj // = TITLE // This object is stored in thread-specific storage. @@ -100,20 +117,27 @@ Test_Task::svc (void *arg) // automatically. ACE_TSS<TSS_Obj> tss (new TSS_Obj); + TSS_DATA::instance ()->data (arg); + Test_Task::wait_count_++; Test_Task::max_count_++; - ACE_DEBUG ((LM_DEBUG, "(%t) svc: waiting\n")); + ACE_DEBUG ((LM_DEBUG, "(%t) svc: waiting (data = %u)\n", + arg)); - while (1) - { - if (Test_Task::max_count_ >= num_tasks) - break; - else - ACE_Thread::yield (); + // Do a bunch of set operations on the TSS data just to make sure + // that it's truly in TSS (it it weren't, the assertion would fail). + + while (Test_Task::max_count_ < num_tasks) + { + TSS_DATA::instance ()->data (arg); + ACE_Thread::yield (); } - ACE_DEBUG ((LM_DEBUG, "(%t) svc: waiting finished\n")); + ACE_DEBUG ((LM_DEBUG, "(%t) svc: waiting finished (data = %u)\n", + arg)); + + ACE_ASSERT (TSS_DATA::instance ()->data () == arg; delete (Test_Task *) arg; |