blob: d12d0ef2ca634ea150abc57d2e85415b4f2e0ed2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice-E is licensed to you under the terms described in the
// ICEE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceE/IceE.h>
#include <stdio.h>
#include <list>
using namespace std;
class WorkQueue : public IceUtil::Thread
{
public:
WorkQueue() { }
~WorkQueue() { }
virtual void
run()
{
while(1)
{
string item = nextItem();
if(item == "destroy")
{
break;
}
printf("work item: %s\n", item.c_str());
IceUtil::ThreadControl::sleep(IceUtil::Time::seconds(1));
}
}
void
add(const string& item)
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);
if(_queue.empty())
{
_monitor.notify();
}
_queue.push_back(item);
}
private:
string
nextItem()
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);
while(_queue.empty())
{
_monitor.wait();
}
string item = _queue.front();
_queue.pop_front();
return item;
}
IceUtil::Monitor<IceUtil::Mutex> _monitor;
list<string> _queue;
};
typedef IceUtil::Handle<WorkQueue> WorkQueuePtr;
int
main()
{
try
{
WorkQueuePtr h = new WorkQueue();
IceUtil::ThreadControl control = h->start();
printf("Pushing work items");
printf("."); fflush(stdout);
h->add("item1");
printf("."); fflush(stdout);
h->add("item2");
printf("."); fflush(stdout);
h->add("item3");
printf("."); fflush(stdout);
h->add("item4");
printf("."); fflush(stdout);
h->add("item5");
printf("."); fflush(stdout);
h->add("destroy");
printf(" ok\n");
printf("Waiting for WorkQueue to terminate\n");
control.join();
}
catch(const IceUtil::Exception& ex)
{
fprintf(stderr, "%s\n", ex.toString().c_str());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|