多线程应用程序开发之二 基于omniThread 的多线程应用程序示例
2009-06-03 18:33
369 查看
onmiThread提供了跨平台的thread底层封装,用户在使用时可以完全不用考虑系统间的差异。本文是关于关于omniThread使用的一个简单示例程序,程序中我们定义了自己的线程类myThread,详细代码如下:
myThread.h
#if !defined(AFX_MYTHREAD_H__B109AC55_F90B_4BD3_A902_60C090896C15__INCLUDED_)
#define AFX_MYTHREAD_H__B109AC55_F90B_4BD3_A902_60C090896C15__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "omniORB4/CORBA.h"
#include "omniThread.h"
class myThread
{
public:
myThread();
virtual ~myThread();
void start();
unsigned int getId() const;
void terminate();
private:
omni_thread* m_pThread;
};
#endif // !defined(AFX_MYTHREAD_H__B109AC55_F90B_4BD3_A902_60C090896C15__INCLUDED_)
myThread.cpp
// myThread.cpp: implementation of the myThread class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "myThread.h"
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
void* hello( void *)
{
for (int i=0;i<100;i++)
{
std::cout<<i<<":Hello world"<<std::endl;
Sleep(100);
}
return NULL;
}
myThread::myThread()
{
}
myThread::~myThread()
{
}
void myThread::start()
{
m_pThread=new omni_thread(&hello,(void*) this);
m_pThread->start();
m_pThread->join((void**)NULL);
}
unsigned int myThread::getId() const
{
return (m_pThread != NULL) ? m_pThread->id() : static_cast< unsigned int >( -1 );
}
void myThread::terminate()
{
m_pThread->join((void**)NULL);
m_pThread = (omni_thread*)NULL;
}
下面是主控程序代码:
#include "stdafx.h"
#include <iostream>
#include "myThread.h"
using namespace std;
int main(int argc, char* argv[])
{
myThread threadtest;
threadtest.start();
std::cout<<"The thread id="<<threadtest.getId()<<std::endl;
return 0;
}
myThread.h
#if !defined(AFX_MYTHREAD_H__B109AC55_F90B_4BD3_A902_60C090896C15__INCLUDED_)
#define AFX_MYTHREAD_H__B109AC55_F90B_4BD3_A902_60C090896C15__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "omniORB4/CORBA.h"
#include "omniThread.h"
class myThread
{
public:
myThread();
virtual ~myThread();
void start();
unsigned int getId() const;
void terminate();
private:
omni_thread* m_pThread;
};
#endif // !defined(AFX_MYTHREAD_H__B109AC55_F90B_4BD3_A902_60C090896C15__INCLUDED_)
myThread.cpp
// myThread.cpp: implementation of the myThread class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "myThread.h"
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
void* hello( void *)
{
for (int i=0;i<100;i++)
{
std::cout<<i<<":Hello world"<<std::endl;
Sleep(100);
}
return NULL;
}
myThread::myThread()
{
}
myThread::~myThread()
{
}
void myThread::start()
{
m_pThread=new omni_thread(&hello,(void*) this);
m_pThread->start();
m_pThread->join((void**)NULL);
}
unsigned int myThread::getId() const
{
return (m_pThread != NULL) ? m_pThread->id() : static_cast< unsigned int >( -1 );
}
void myThread::terminate()
{
m_pThread->join((void**)NULL);
m_pThread = (omni_thread*)NULL;
}
下面是主控程序代码:
#include "stdafx.h"
#include <iostream>
#include "myThread.h"
using namespace std;
int main(int argc, char* argv[])
{
myThread threadtest;
threadtest.start();
std::cout<<"The thread id="<<threadtest.getId()<<std::endl;
return 0;
}
相关文章推荐
- 多线程应用程序开发之一 基于MFC CWinThread 派生的工作者线程
- myeclipse6.5上基于JAX-WS开发Webservice(中文示例)
- 基于DDD的.NET开发框架ABP实例,多租户 (Sass)应用程序,采用.NET MVC, Angularjs, EntityFramework-介绍
- 基于 ArcGIS Silverlight API开发的WebGIS应用程序的部署
- iOS开发基于xcode7.2的应用程序需国际化本地化处理
- Prism框架(三)——基于模块的应用程序开发
- 使用Apworks开发基于CQRS架构的应用程序(五):命令
- 基于MINA框架快速开发网络应用程序
- [转] 基于MINA框架快速开发网络应用程序
- 利用VTK库开发基于MFC的应用程序
- 基于MINA框架快速开发网络应用程序
- 多线程编程之二——MFC中的多线程开发
- 基于MINA框架快速开发网络应用程序
- 基于vue2.0+vuex+localStorage开发的本地记事本示例
- WCF for .Net Compact Framework 3.5 应用程序开发示例
- 基于Oracle ADF的应用程序开发
- 基于Swift的iOS应用程序开发:“属性观察器”简介
- myeclipse6.5上基于JAX-WS开发Webservice(中文示例)
- [.NET] 在Windows系统中搭建基于.NET的iPhone应用程序虚机开发环境
- 基于Swift的iOS应用程序开发:使用表格显示并控制数据(四):使用UIRefreshController实现下拉刷新