15,471
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/signals.hpp>
//////////////////////////////////////////////////////////////////////////
//扩展io_service,去除对单线程io_service:run依赖
class io_service_ex
{
public:
boost::asio::io_service io;
private:
boost::asio::deadline_timer timer_;//only used for keep io thread live
HANDLE event_stop_;
public:
io_service_ex()
: timer_(io,boost::posix_time::seconds(1))
,event_stop_(0)
{
timer_.async_wait(boost::bind(&io_service_ex::handle_timer,this,boost::asio::placeholders::error));
boost::thread thd(boost::bind(&io_service_ex::control_run,this));
}
~io_service_ex()
{
event_stop_ = CreateEvent(0,0,0,0);
io.stop();
WaitForSingleObject(event_stop_,-1);
}
void control_run()
{
boost::thread thd(boost::bind(&boost::asio::io_service::run,&io));
thd.join();
if(event_stop_)
SetEvent(event_stop_);
}
void handle_timer(const boost::system::error_code er)
{
if(er)
return;
timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
timer_.async_wait(boost::bind(&io_service_ex::handle_timer,this,boost::asio::placeholders::error));
}
};
//////////////////////////////////////////////////////////////////////////
//链表单元结构
struct list_item
{
typedef boost::shared_ptr<list_item> ptr;
int datalen;
boost::shared_ptr<char> data;
};
//////////////////////////////////////////////////////////////////////////
//主程序类,定时维护链表,10秒钟内若链表不为空,则删除链表的最后一个单元
class worker
{
protected:
vector<list_item::ptr> list_;
boost::mutex mutex_;
io_service_ex io_;
boost::shared_ptr<boost::asio::deadline_timer> timer_;
public:
worker()
{
int sec = 10 * 1000;//定时时间,毫秒单位
timer_ = boost::shared_ptr<boost::asio::deadline_timer>(new boost::asio::deadline_timer(io_.io,boost::posix_time::milliseconds(sec)));
timer_->async_wait(boost::bind(&worker::handle_timer,this,boost::asio::placeholders::error,sec));
}
void handle_timer(const boost::system::error_code er,int second)
{
if(er)
{
timer_.reset();
return;
}
onTimer(timer_.get());
timer_->expires_at(timer_->expires_at() + boost::posix_time::milliseconds(second));
timer_->async_wait(boost::bind(&worker::handle_timer,this,boost::asio::placeholders::error,second));
}
void onTimer(boost::asio::deadline_timer * timer)
{
boost::mutex::scoped_lock lock(mutex_);//锁
if(!list_.empty())
{
list_.pop_back();
cout << "list tail item cleared" << endl;
}
else
cout << "list empty" << endl;
}
void push(list_item::ptr item)
{
boost::mutex::scoped_lock lock(mutex_);//锁
list_.push_back(item);
}
};
boost::shared_ptr<worker> g_worker;
bool parse(string cmd)
{
if(cmd=="exit")
return false;
else if(cmd=="start")
g_worker.reset(new worker);
else if(cmd=="stop")
g_worker.reset();
else if(cmd=="push")
{
list_item::ptr item(new list_item);
item->datalen = 100;
item->data.reset(new char[item->datalen]);
g_worker->push(item);
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
char buf[100];
while(true)
{
gets_s(buf,sizeof buf);
if(!parse(buf))
break;
}
return 0;
}
using namespace std;
#include <boost/shared_ptr.hpp>
#include <boost/signals.hpp>
struct videodata
{
typedef boost::shared_ptr<videodata> ptr;
int datalen;
boost::shared_ptr<char> data;
};
class video
{
public:
typedef boost::signal<void(videodata::ptr)> _callback;
protected:
_callback sig_;
public:
void newdata()
{
videodata::ptr data(new videodata);
data->datalen = 1024;
data->data.reset(new char[data->datalen]);
memset(data->data.get(),0x11,data->datalen);
sig_(data);
}
void register_callback(const _callback::slot_type & slot)
{
sig_.connect(slot);
}
};
void mycallback(videodata::ptr data)
{
cout << "video callback datalen:" << data->datalen << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
video v;
v.register_callback(mycallback);
v.newdata();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
char * buf = new char[1000];
return 0;
}
#include <boost/shared_ptr.hpp>
int _tmain(int argc, _TCHAR* argv[])
{
boost::shared_ptr<char> buf(new char[1000]);
return 0;
}
#include <iostream>
using namespace std;
struct videodata
{
int datalen;
char * data;
};
class video
{
public:
bool newdata(videodata ** result)
{
videodata * data = new videodata[1];
data->datalen = 1024;
data->data = new char[data->datalen];
memset(data->data,0x11,data->datalen);
*result = data;
return true;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
video v;
videodata * data;
if(v.newdata(&data))
{
if(data->data)
delete data->data;
delete data;
}
return 0;
}
#include <iostream>
using namespace std;
#include <boost/shared_ptr.hpp>
struct videodata
{
typedef boost::shared_ptr<videodata> ptr;
int datalen;
boost::shared_ptr<char> data;
};
class video
{
public:
videodata::ptr newdata()
{
videodata::ptr data(new videodata);
data->datalen = 1024;
data->data.reset(new char[data->datalen]);
memset(data->data.get(),0x11,data->datalen);
return data;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
video v;
videodata::ptr data = v.newdata();
return 0;
}