Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / ThreadSafety / TSS.cpp
blob71c1590a33a7479d12ed6bad001e4f65ea19ac44
1 #include "ace/config-lite.h"
2 #if defined (ACE_HAS_THREADS)
4 #include "ace/Synch.h"
5 #include "ace/Task.h"
6 #include "ClientContext.h"
9 void*
10 ClientContext::get_attribute (const char *name)
12 void * value = 0;
13 attributeMap_.find (name, value);
14 return value;
17 void
18 ClientContext::set_attribute (const char *name, void *value)
20 attributeMap_.bind (name, value);
23 // Listing 2 code/ch14
24 class HA_CommandHandler : public ACE_Task<ACE_MT_SYNCH>
26 public:
27 virtual int svc ()
29 ACE_thread_t tid = this->thr_mgr ()->thr_self ();
30 // Set our identifier in TSS.
31 this->tss_ctx_->set_attribute ("thread_id", &tid);
33 while (handle_requests () > 0)
36 return 0;
39 int handle_requests ()
41 ACE_thread_t *tid =
42 (ACE_thread_t*)this->tss_ctx_->get_attribute ("thread_id");
43 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) TSS TID: %d\n"),
44 *tid));
46 // do work.
47 return -1;
50 private:
51 ACE_TSS<ClientContext> tss_ctx_;
53 // Listing 2
55 int ACE_TMAIN (int, ACE_TCHAR *[])
57 HA_CommandHandler handler;
58 handler.activate (THR_NEW_LWP | THR_JOINABLE, 5);
59 handler.wait ();
60 return 0;
63 #else
64 #include "ace/OS_main.h"
65 #include "ace/OS_NS_stdio.h"
67 int ACE_TMAIN (int, ACE_TCHAR *[])
69 ACE_OS::puts (ACE_TEXT ("This example requires threads."));
70 return 0;
73 #endif /* ACE_HAS_THREADS */