summaryrefslogtreecommitdiff
path: root/src/tests/itoqueue1.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/itoqueue1.cpp')
-rw-r--r--src/tests/itoqueue1.cpp115
1 files changed, 0 insertions, 115 deletions
diff --git a/src/tests/itoqueue1.cpp b/src/tests/itoqueue1.cpp
deleted file mode 100644
index 27cb93c..0000000
--- a/src/tests/itoqueue1.cpp
+++ /dev/null
@@ -1,115 +0,0 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include <string>
9#include "bu/ito.h"
10#include "bu/itoqueue.h"
11#include <stdlib.h>
12#include <stdio.h>
13
14class Reader : public Bu::Ito
15{
16public:
17 Reader( Bu::ItoQueue<std::string *> &q, int id ) :
18 q( q ),
19 id( id )
20 {
21 }
22
23 void run()
24 {
25 for( int i = 0; i < 10; i++ )
26 {
27 std::string *pStr = q.dequeue( true );
28 if( pStr == NULL )
29 {
30 printf("Null received...\n");
31 }
32 else
33 {
34 printf("[%d] read: %s\n", id, pStr->c_str() );
35 delete pStr;
36 }
37 usleep( (int)(((double)rand())/((double)RAND_MAX)*2000000.0) );
38 }
39 }
40
41private:
42 Bu::ItoQueue<std::string *> &q;
43 int id;
44};
45
46class Writer : public Bu::Ito
47{
48public:
49 Writer( Bu::ItoQueue<std::string *> &q, int id, const char *strbase ) :
50 q( q ),
51 strbase( strbase ),
52 id( id )
53 {
54 }
55
56 void run()
57 {
58 for( int i = 0; i < 11; i++ )
59 {
60 usleep( (int)(((double)rand())/((double)RAND_MAX)*2000000.0) );
61 q.enqueue( new std::string( strbase ) );
62 printf("[%d] write: %s\n", id, strbase );
63 }
64 }
65
66private:
67 Bu::ItoQueue<std::string *> &q;
68 const char *strbase;
69 int id;
70};
71
72int main()
73{
74 Writer *wr[5];
75 Reader *rd[5];
76 const char bob[][7]={
77 {"Test 1"},
78 {"Test 2"},
79 {"Test 3"},
80 {"Test 4"},
81 {"Test 5"}
82 };
83
84 Bu::ItoQueue<std::string *> q;
85
86 for( int j = 0; j < 5; j++ )
87 {
88 wr[j] = new Writer( q, j, bob[j] );
89 rd[j] = new Reader( q, j );
90 }
91
92 for( int j = 0; j < 5; j++ )
93 {
94 rd[j]->start();
95 }
96
97 for( int j = 0; j < 5; j++ )
98 {
99 wr[j]->start();
100 }
101
102 for( int j = 0; j < 5; j++ )
103 {
104 rd[j]->join();
105 }
106
107 for( int j = 0; j < 5; j++ )
108 {
109 delete wr[j];
110 delete rd[j];
111 }
112
113 return 0;
114}
115