?? osthread.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 + -