1 //=============================================================================
3 * @file Task_Ex_Test.cpp
5 * This test program illustrates the ACE_Task_Ex class which has the ACE_Message_Queue_Ex
6 * that has the capability to hold user-defined messages instead of ACE_Message_Block
8 * @author Kobi Cohen-Arazi <kobi-co@barak-online.net>
10 //=============================================================================
12 #include "test_config.h"
13 #include "Task_Ex_Test.h"
14 #include "ace/Task_Ex_T.h"
15 #include "ace/Log_Msg.h"
16 #include "ace/Auto_Ptr.h"
18 #if defined (ACE_HAS_THREADS)
21 #if defined (ACE_VXWORKS) || defined (ACE_LYNXOS_MAJOR)
22 // this is a very expensive test on VxWorks so limit it otherwise it will never finish in time:-)
23 const ACE_INT32 PRODUCER_THREADS_NO
=10;
24 const ACE_INT32 CONSUMER_THREADS_NO
=10;
25 const ACE_INT32 NUMBER_OF_MSGS
=200;
27 const ACE_INT32 PRODUCER_THREADS_NO
=20;
28 const ACE_INT32 CONSUMER_THREADS_NO
=20;
29 const ACE_INT32 NUMBER_OF_MSGS
=2000;
32 /// @class Consumer consumes user defined Msgs
33 class Consumer
: public ACE_Task_Ex
<ACE_MT_SYNCH
, User_Defined_Msg
>
36 //FUZZ: disable check_for_lack_ACE_OS
37 /// activate/spawn the threads.
38 ///FUZZ: enable check_for_lack_ACE_OS
39 int open (void*) override
;
41 /// svc thread entry point
46 int Consumer::open (void*)
48 if(this->activate (THR_NEW_LWP
| THR_JOINABLE
,
49 CONSUMER_THREADS_NO
)==-1)
51 ACE_ERROR_RETURN((LM_ERROR
,
52 ACE_TEXT("Consumer::open Error spanwing thread %p\n"),
61 User_Defined_Msg
* pMsg
=0;
62 while(this->getq (pMsg
)!=-1)
64 ACE_TEST_ASSERT (pMsg
!=0);
65 std::unique_ptr
<User_Defined_Msg
> pAuto(pMsg
);
67 ACE_TEXT("Consumer::svc got msg id=%d\n"),
69 if(pMsg
->msg_id ()==NUMBER_OF_MSGS
-1)
74 ACE_TEXT("Consumer::svc ended thread %t\n")));
80 /// producer function produces user defined messages.
81 ACE_THR_FUNC_RETURN
producer (void *arg
)
83 Consumer
* c
= static_cast<Consumer
*> (arg
);
84 ACE_TEST_ASSERT(c
!=0);
88 ACE_TEXT("producer Error casting to consumer\n")));
89 return (ACE_THR_FUNC_RETURN
)-1;
91 for (int i
=0;i
!=NUMBER_OF_MSGS
;++i
)
93 User_Defined_Msg
* pMsg
=0;
94 ACE_NEW_NORETURN(pMsg
, User_Defined_Msg(i
));
98 ACE_TEXT("producer Error allocating data %p\n"),
100 return (ACE_THR_FUNC_RETURN
)-1;
102 if(c
->putq (pMsg
)==-1)
105 ACE_TEXT("producer Error putq data %p\n"),
107 return (ACE_THR_FUNC_RETURN
)-1;
113 #endif /* ACE_HAS_THREADS */
116 run_main (int, ACE_TCHAR
*[])
118 ACE_START_TEST (ACE_TEXT ("Task_Ex_Test"));
120 #if defined (ACE_HAS_THREADS)
124 ACE_ERROR_RETURN((LM_ERROR
,
125 ACE_TEXT ("main Error opening consumer\n")),-1);
128 int result
=ACE_Thread_Manager::instance()->spawn_n (PRODUCER_THREADS_NO
,
129 ACE_THR_FUNC(producer
),
130 static_cast<void*> (&c
));
133 ACE_ERROR_RETURN((LM_ERROR
,
134 ACE_TEXT ("main Error spawning threads %p\n"),
139 int wait_result
=ACE_Thread_Manager::instance()->wait();
143 ACE_TEXT("main Error Thread_Manager->wait %p\n"),
149 ACE_TEXT ("threads not supported on this platform\n")));
150 #endif /* ACE_HAS_THREADS */