- 论坛徽章:
- 0
|
//
// @author <[email]zhuxueling@pica.com[/email]>
// @data 2009-02-10
//________Test Class_____________
//这个是摆设
class Test{
public:
void say( const char* sb){
while(true){
std::cout << sb << " is foolish.\n";
sleep(1);
}
}
void say( const char* sb, const char* words){
while(true){
std::cout << sb << " says: " << words << std::endl;
sleep(1);
}
}
};
//_________Thread__________________
int main(){
Test atest;
//创建一个线程变得简单了,类名,成员函数名,参数列表就可以了。
//当然,也可以直接用普通函数名,加参数列表。 而且支持重载的。。
Thread s( atest, &Test::say,"traxex");
s.start();
s.join();
}
//_________Cond__________________
//两个人在不停的添加水果,一个人等有水果就吃。。。
std::queue<string> fruits;
Cond cond;
Mutex mutex;
void add( string name){
while( true){
{
ScopeLock lock( mutex);
fruits.push( name);
}
std::cout << "add " << name << std::endl;
cond.signal();
Sleep( 200);
}
}
void eat( ){
while( true){
cond.wait();
ScopeLock lock( mutex);
fruits.pop();
std::cout << "eat "<< fruits.front() << std::endl;
}
}
int main(){
Thread add_apple ( &add, "apple ");
Thread add_orange( &add, "orange");
Thread eat_fruit ( &eat);
add_apple.start(); add_orange.start(); eat_fruit.start();
add_apple.join(); add_orange.join(); eat_fruit.join();
return 0;
}
//__________Thread Pool___________
//启动一个有3个线程的线程池,来执行这几个函数。
int main(){
ThreadPool tp( 3);
Test atest;
tp.push( atest, &Test::say, "traxex", "fuckyou");
tp.push( atest, &Test::say, "traxex", "Oh, Sorry");
tp.push( atest, &Test::say, "zhuzhu", "sounds good");
tp.push( atest, &Test::say, "gtkmm" , "HELP!");
tp.push( atest, &Test::say, "gtkmm" , "Ahh!!");
return 0;
}
//________TCP Server___________
//简单的服务器
int main(){
TcpServer server("127.0.0.1", 8888);
server.init();
TcpWorker worker = server.accept();
while( worker.closed()){
const char* req = worker.recv();
worker.send( "haha, I'm the joker.");
}
}
//________TCP Client___________
//简单的客户端
int main(){
TcpClient client("127.0.0.1", 8888);
client.connect();
std::string msg;
while( cin>>msg && !client.closed() ){
client.send( msg);
std::cout << client.recv();
}
return 0;
}
//________IO Service_________
//异步IO服务器,动态注册事件,以回调函数的方式得到异步通知。
class MyServer{
public:
MyServer( IOService &ioservice, short port)
:server( port,true),
ios( ioservice){
if( server.init()){
server.async_wait( ios, EV_ACCEPT_LOOP, &MyServer::fun_accept, this);
}
}
public:
static void fun_accept( int fd, uint events, void* self);
static void fun_read ( int fd, uint events, void* self);
TcpServer server;
IOService &ios;
Mutex mutex;
std::map<int,TcpWorker> clients;
};
void MyServer::fun_accept( int fd, uint events, void* ptr){
MyServer *self = (MyServer*)ptr;
ScopeLock lock( self->mutex);
TcpWorker new_worker =self->server.accept();
self->clients[new_worker.fd] = new_worker;
std::cout << "accept " << new_worker.fd << "\n";
if( new_worker.fd == -1){
std::cout << strerror( errno) << std::endl;
}
new_worker.async_wait( self->ios, TcpWorker::EV_READ, &MyServer::fun_read, self);
}
void MyServer::fun_read ( int fd, uint events, void* ptr){
MyServer *self = (MyServer*)ptr;
if( events & EPOLLIN){
std::cout << "\Recv: " << self->clients[fd].recv();
self->clients[fd].async_wait( self->ios, TcpWorker::EV_READ, &MyServer::fun_read, self);
}else{
clients[fd].close();
ScopeLock lock( self->mutex);
self->clients.erase(fd);
}
}
int main(){
IOService service;
MyServer server( service, 8888);
service.run();
}
[[i] 本帖最后由 die 于 2009-2-11 00:49 编辑 [/i]] |
|