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