米斯特逗的学习笔记gilbert.blog.chinaunix.net
GilBert1987
持之以恒
全部博文(218)
四大组件(3)
开发环境(5)
Games(2)
Interview(1)
HTTP(0)
多线程同步(4)
编译内存性能(1)
基础(1)
String(3)
文件操作(3)
STL(10)
索引结构学习(1)
RTMP(0)
FFMPEG(1)
ACE(12)
网络基础(9)
HTTP(1)
HHTPS&&SSL(0)
Apache(2)
多线程(1)
2013年(8)
2012年(2)
2011年(21)
2010年(55)
2009年(116)
2008年(16)
wxliangz
00450681
孙一萌
☆彼岸★
zergduan
hukui161
怀颂HS
时光懂你
carlytay
温小二
wangkai8
msdb1989
心的回忆
分类: C/C++
2010-03-03 16:59:08
#pragma once #pragma comment(lib,"ACEd.lib") #include "ace/Task.h" #include "ace/Synch.h" #include "ace/OS.h" #include <iostream> #include <string> #include <vector> class CMyThread:public ACE_Task<ACE_MT_SYNCH> { public: void Init(); int svc(void);//线程函数 bool ChangeVector(); private: bool ReadVector(); private: int m_Count; std::vector<std::string> m_StringVec; }; void CMyThread::Init() { m_StringVec.clear(); std::string str = "string"; m_StringVec.push_back(str); m_Count = 0; } int CMyThread::svc(void) { while(1) { ReadVector(); ACE_OS::sleep(3); } return 1; } bool CMyThread::ReadVector() { std::vector<std::string>::const_iterator m_StrIt; for (m_StrIt = m_StringVec.begin();m_StrIt !=.end();m_StrIt++) { std::cout<<(*m_StrIt).c_str()<<" "; } std::cout<<std::endl<<"================="<<std::endl; return true; } bool CMyThread::ChangeVector() { m_Count++; char cStr[20]; memset(cStr,'\0',20); ::sprintf(cStr,"string%d",m_Count); m_StringVec.push_back(cStr); return true; } int main(int arg,char * argc[]) { CMyThread thread; thread.Init(); thread.activate(); while(1) { thread.ChangeVector(); ACE_OS::sleep(2); } getchar(); return 0; }
#pragma once #pragma comment(lib,"ACEd.lib") #include "ace/Task.h" #include "ace/Synch.h" #include "ace/OS.h" #include "ace/Mutex.h" #include "ace/Guard_T.h" #include <iostream> #include <string> #include <vector> class CMyThread:public ACE_Task<ACE_MT_SYNCH> { public: void Init(); int svc(void);//线程函数 bool ChangeVector(); private: bool ReadVector(); private: int m_Count; ACE_MT_SYNCH::MUTEX m_Mutex; //互斥锁 std::vector<std::string> m_StringVec; }; void CMyThread::Init() { m_StringVec.clear(); std::string str = "string"; m_StringVec.push_back(str); m_Count = 0; } int CMyThread::svc(void) { while(1) { ReadVector(); ACE_OS::sleep(3); } return 1; } bool CMyThread::ReadVector() { ACE_Guard monitor(m_Mutex); std::vector<std::string>::const_iterator m_StrIt; for (m_StrIt = m_StringVec.begin();m_StrIt !=.end();m_StrIt++) { std::cout<<(*m_StrIt).c_str()<<" "; } std::cout<<std::endl<<"================="<<std::endl; return true; } bool CMyThread::ChangeVector() { m_Count++; char cStr[20]; memset(cStr,'\0',20); ::sprintf(cStr,"string%d",m_Count); ACE_Guard monitor(m_Mutex); m_StringVec.push_back(cStr); return true; } int main(int arg,char * argc[]) { CMyThread thread; thread.Init(); thread.activate(); while(1) { thread.ChangeVector(); ACE_OS::sleep(2); } getchar(); return 0; }
上一篇:XML编辑器
下一篇:有效使用反应堆Reactor的设计准则(转)
登录 注册