⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 osthread.cpp

📁 本源码为移动公司话费查询中间件TUXEDO使用的实例
💻 CPP
字号:
/*********************************************
	File:		OSThread.cpp

	date:       2001.7.25
	Contains:	Thread abstraction implementation

*********************************************/

#ifdef _WIN32
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "OSThread.h"
//#define  "./print_log/"

//
// OSThread.cpp
//
void*	OSThread::sMainThreadData = NULL;

UInt32	OSThread::sThreadStorageIndex = 0;
FILE *    OSThread::fb = 0;
#ifndef _WIN32
pthread_mutex_t OSThread::counter_mutex ;//PTHREAD_MUTEX_INITALIZER;
#else
HANDLE          OSThread::counter_mutex ;//PTHREAD_MUTEX_INITALIZER;
#endif

int OSThread::TOpen(char *pfn)
{
	if(fb != 0)
    	return 0;//(int)fb;
//	_mkdir("./print_log");
#ifndef _WIN32
	pthread_mutex_init (&counter_mutex,NULL);
#else
	counter_mutex = ::CreateMutex(NULL,FALSE,NULL);
#endif //_WIN32
	fb = fopen(pfn,"a+");
//	fseek(fb,0,2l);
	printf("Log file open success: Handle[%d]\n",(int)fb);

	return 0;

}

int OSThread::TWrite(char *pWrite,int nLen)
{
#ifndef _WIN32
	pthread_mutex_lock(&counter_mutex);
#else
	WaitForSingleObject(counter_mutex,INFINITE);

#endif //_WIN32
//	fprintf(fb,"%s",pWrite);
	fwrite(pWrite,nLen,1,fb);
#ifndef _WIN32
	pthread_mutex_unlock(&counter_mutex);
#else
	ReleaseMutex(counter_mutex);
#endif //_WIN32

	return 0;
}

int OSThread::TClose()
{
#ifndef _WIN32
	pthread_mutex_lock(&counter_mutex);
#else
	WaitForSingleObject(counter_mutex,INFINITE);
#endif //_WIN32
	fclose(fb);
#ifndef _WIN32
	pthread_mutex_unlock(&counter_mutex);
#else
	ReleaseMutex(counter_mutex);
#endif //_WIN32

	return 0;
}

void OSThread::Initialize()
{
#ifdef _WIN32
	sThreadStorageIndex = ::TlsAlloc();
	assert(sThreadStorageIndex >= 0);
#endif //_WIN32
}

OSThread::OSThread()
: 	fStopRequested(false),
	fRunning(false),
	fCancelThrown(false),
	fDetached(false),
	fJoined(false),
	fThreadData(NULL)
{
	fThreadID = 0;
}

OSThread::~OSThread()
{
	this->StopAndWaitForThread();
}

void OSThread::Start(Bool16 bJoin)
{
	unsigned long theId = 0; // We don't care about the identifier
//	fStopRequested = false;
	fRunning = false;
//	fCancelThrown = false;
	fDetached = false;
	fJoined = false;

#ifdef _WIN32
	if(fThreadID >0)
	{
		CloseHandle(fThreadID);
		fThreadID = 0;
	}
	fThreadID = (HANDLE)CreateThread(	NULL, 	// Inherit security
										0,		// Inherit stack size
										_Entry,	// Entry function
										(void*)this,	// Entry arg
										0,		// Begin executing immediately
										&theId );
#else
	if(bJoin)
	{
		pthread_attr_init(&attr);
		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
		pthread_create(&fThreadID,&attr,_Entry,(void*) this);
	}
	else
		pthread_create(&fThreadID,0,_Entry,(void*) this);
#endif //_WIN32
	assert(fThreadID != 0);

	printf("线程ID = %d开始启动\n",fThreadID);
}

void OSThread::StopAndWaitForThread()
{
	if (!fRunning)
		return;
		
	fStopRequested = true;
//	pthread_exit(NULL);
	if (!fJoined && !fDetached)
		Join();
}

void OSThread::Join()
{
	// What we're trying to do is allow the thread we want to delete to complete
	// running. So we wait for it to stop.
	assert(!fJoined && !fDetached);
	fJoined = true;
	UInt32 theErr = 0;

#ifdef _WIN32
	theErr = ::WaitForSingleObject(fThreadID, INFINITE);
#else
		theErr = pthread_join(fThreadID,NULL);
#endif //_WIN32
		fJoined = false;

	assert(theErr == 0);
}


void OSThread::Detach()
{
	assert(!fDetached && !fJoined);
	fDetached = true;
}

void OSThread::CheckForStopRequest()
{
	if (fStopRequested && !fCancelThrown)
		ThrowStopRequest();
}


void OSThread::ThrowStopRequest()
{
	if (fStopRequested && !fCancelThrown) {
		fCancelThrown = true;
		throw (0);
	}
}

void OSThread::CallEntry(OSThread* thread) 	// static method
{
	thread->fRunning = true;

	try
	{
		thread->Entry();
	} 
	catch(...)
	{
		printf("异常出错,线程退出!ID = %d\n",thread->fThreadID);
	}

	thread->fRunning = false;
//by lyh edit 2001.10.19
//	if (thread->fDetached) {
//		assert(!thread->fJoined);
//	}
//end lyh
}

#ifdef _WIN32
	unsigned long WINAPI 
#else
	void*
#endif	
OSThread::_Entry(void* inThread)
{
	OSThread* theThread = (OSThread*)inThread;

	OSThread::CallEntry(theThread);
	return NULL;
}
/*
OSThread*	OSThread::GetCurrent()
{
	return (OSThread *)::TlsGetValue(sThreadStorageIndex);
}
*/
void OSThread::Entry()
{
	SInt64 timeout;
//	while(1)
//	{
//		try{
			timeout = this->Run();
//			if(timeout !=0)
//				break;        //退出线程
//		}
//		catch(...){
//			return ;
//		}
//	}
	printf("线程正常退出 ID = %d\n",fThreadID);
}

int	OSThread::GetErrno()
{
	return 0;
/*
	int winErr = ::GetLastError();
	return winErr;

	
	// Convert to a POSIX errorcode. The *major* assumption is that
	// the meaning of these codes is 1-1 and each Winsock, etc, etc
	// function is equivalent in errors to the POSIX standard. This is 
	// a big assumption, but the server only checks for a small subset of
	// the real errors, on only a small number of functions, so this is probably ok.
	switch (winErr)
	{

//		case ERROR_FILE_NOT_FOUND: return ENOENT;

//		case ERROR_PATH_NOT_FOUND: return ENOENT;		




		case WSAEINTR:		return EINTR;
		case WSAENETRESET:	return EPIPE;
		case WSAENOTCONN:	return ENOTCONN;
		case WSAEWOULDBLOCK:return EAGAIN;
		case WSAECONNRESET:	return EPIPE;
//		case WSAEADDRINUSE:	return EADDRINUSE;
//		case WSAEMFILE:		return EMFILE;
		case WSAEINPROGRESS:return EINPROGRESS;
//		case WSAEADDRNOTAVAIL: return EADDRNOTAVAIL;
		case WSAECONNABORTED: return EPIPE;
		case 0:				return 0;
		
		default: 			return ENOTCONN;
	}
*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -