您的位置:首页 > 其它

多线程应用程序开发之二 基于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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: