aboutsummaryrefslogtreecommitdiff
path: root/src/tests/itoqueue2.cpp
blob: d1f20a456542d68f5b024970e77ea5a27af84a72 (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
/*
 * Copyright (C) 2007-2010 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include <string>
#include "bu/ito.h"
#include "bu/itoqueue.h"
#include <errno.h>

class Reader : public Bu::Ito
{
public:
	Reader( Bu::ItoQueue<std::string *> &q, int id ) :
		q( q ),
		id( id )
	{
	}

	void *run()
	{
		for( int i = 0; i < 10; i++ )
		{
			std::string *pStr = q.dequeue( 0, 500000 );
			if( pStr == NULL )
			{
				printf("Null received...\n");
				i--;
			}
			else
			{
				printf("[%d] read:  %s\n", id, pStr->c_str() );
				delete pStr;
			}
		}

		return NULL;
	}

private:
	Bu::ItoQueue<std::string *> &q;
	int id;
};

class Writer : public Bu::Ito
{
public:
	Writer( Bu::ItoQueue<std::string *> &q, int id, const char *strbase ) :
		q( q ),
		strbase( strbase ),
		id( id )
	{
	}

	void *run()
	{
		for( int i = 0; i < 11; i++ )
		{
			sleep( 2 );
			printf("[%d] write: %s\n", id, strbase );
			q.enqueue( new std::string( strbase ) );
		}

		return NULL;
	}

private:
	Bu::ItoQueue<std::string *> &q;
	const char *strbase;
	int id;
};

int main()
{
	printf("ETIMEDOUT: %d\n", ETIMEDOUT );
	Bu::ItoQueue<std::string *> q;
	Writer wr( q, 0, "writer" );
	Reader rd( q, 0 );

	rd.start();
	wr.start();

	rd.join();
	wr.join();

	return 0;
}