1
/* work queue thread test program
3
TODO: Needs to be adapted to boost::theads.
7
//#include <free_thread.h>
8
#include <work_queue_thread.h>
9
#include <controlled_wqt.h>
20
class worker : public work_queue_thread<int,free_thread>
25
scope_lock lock(wlock);
28
scope_lock clock(c_lock);
29
cout << "worker " << iam << " started" << std::endl;
33
scope_lock lock(c_lock);
34
cout << "worker " << iam << " is being deleted." << std::endl;
42
virtual void do_work()
48
delaytime.tv_nsec = my_data * 1000;
49
nanosleep(&delaytime,NULL);
51
scope_lock lock(c_lock);
52
cout << "\tworker " << iam
53
<< " reports packet " << my_data << std::endl;
64
int worker::count = 0;
66
class workera : public controlled_work_queue_thread<int,int,free_thread,1>
71
scope_lock lock(wlock);
74
scope_lock clock(c_lock);
75
cout << "workera " << iam << " started" << std::endl;
79
scope_lock lock(c_lock);
80
cout << "workera " << iam << " is being deleted." << std::endl;
88
virtual void do_work()
92
delaytime.tv_nsec = my_data * 1000;
93
nanosleep(&delaytime,NULL);
95
scope_lock lock(c_lock);
96
cout << "\t\tworkera " << iam
97
<< " reports packet " << my_data << std::endl;
101
virtual void do_control()
105
case 1: pthread_exit(0); break;
108
scope_lock lock(c_lock);
109
cout << "\t\t\tworkera " << iam
110
<< " received control message " << my_ctl
117
mutex workera::wlock;
118
int workera::count = 0;
122
// start five workers
123
cout << "*** Starting workers ***" << endl;
124
for (int i=0; i < 5; i++)
129
cout << "*** sending control message 0 ***" << endl;
131
cout << "*** queuing 0-9 (0-90) ***" << endl;
132
for (int i = 0; i < 10; i++)
135
workera::store(i*10);
137
cout << "*** sending control message 2 ***" << endl;
139
cout << "*** sending 10 \"-1\" to workers ***" << endl;
140
for (int i = 0; i < 10; i++)
144
cout << "*** two second sleep here ***" << endl;
146
cout << "*** Shutting down CWQ now. ***" << endl;
148
cout << "*** Starting 10 second sleep ***" << endl;
150
cout << "worker has " << worker::parked() << " parked threads and "
151
<< worker::work_pending() << " elements in the queue." << endl;
152
cout << "workera has " << workera::parked() << " parked threads and "
153
<< workera::work_pending() << " elements in the queue." << endl;