1) window os
/*
* BadClass.cpp
*
* Sample code for "Multitasking Applications in Win32"
* This is from Chapter 9, Listing 9-2
*
* Shows the wrong way to try and start a thread
* based on a class member function.
*
* THIS FILE IS NOT SUPPOSED TO COMPILE SUCCESSFULLY
* You should get an error on line 51.
*
* Build this file with the command line:
*
* cl /MD BadClass.cpp
* Modify by denny November 9, 2006 wqf363@hotmail.com
*/
#include <windows.h>
#include <stdio.h>
#include <process.h>
typedef unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC)(LPVOID param);
typedef unsigned *PBEGINTHREADEX_THREADID;
class ThreadObject
{
public:
ThreadObject();
void StartThread();
static DWORD WINAPI ThreadFunc(LPVOID param);
void WaitForExit();
protected:
virtual DWORD ThreadMemberFunc(); //for subclass
private:
HANDLE m_hThread;
DWORD m_ThreadId;
};
ThreadObject::ThreadObject()
{
m_hThread = NULL;
m_ThreadId = 0;
}
void ThreadObject::StartThread()
{
m_hThread = (HANDLE)_beginthreadex(NULL,
0,
(PBEGINTHREADEX_THREADFUNC)ThreadFunc,
(LPVOID)this,
CREATE_SUSPENDED,
(PBEGINTHREADEX_THREADID)&m_ThreadId);
if (m_hThread) {
printf("Thread launched/n");
}
}
void ThreadObject::WaitForExit()
{
WaitForSingleObject(m_hThread, INFINITE);
CloseHandle(m_hThread);
}
DWORD WINAPI ThreadObject::ThreadFunc(LPVOID param)
{
ThreadObject* pto = new ThreadObject;
if(pto)
return pto->ThreadMemberFunc();
return 0;
}
DWORD ThreadObject::ThreadMemberFunc()
{
// Do something useful ...
printf("fuck");
return 0;
}
void main()
{
ThreadObject obj;
obj.StartThread();
obj.WaitForExit();
Sleep(1);
}
2) linux os(tinycxx)
/*
* thread.h -- Wrapper of linux thread
* Copyright (C) |2001-2002| by Alben <alben@yeah.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330 , Boston , MA 02111-1307 USA
*/
#include <iostream.h>
#include <pthread.h>
#include <errno.h>
class CThread
{
friend void* ThreadRun(CThread* pstThread);
protected:
pthread_attr_t m_tThreadAttr;
pthread_t m_tThread;
bool m_bDetach;
bool m_bStart;
public:
CThread(bool bDetach = true);
virtual ~CThread();
void Start();
protected:
virtual void Run() = 0;
virtual void OnEnd() {} //perhaps you will delete THIS here.
virtual void OnDelete();
};
typedef void* (*tc_thread_func)(void*);
void* ThreadRun(CThread* pstThread)
{
pstThread->Run();
pstThread->m_bStart = false;
pstThread->OnEnd();
return NULL;
}
CThread::CThread(bool bDetach)
{
m_bDetach = bDetach;
m_bStart = false;
pthread_attr_init(&m_tThreadAttr);
if (m_bDetach)
pthread_attr_setdetachstate(&m_tThreadAttr, PTHREAD_CREATE_DETACHED);
else
pthread_attr_setdetachstate(&m_tThreadAttr, PTHREAD_CREATE_JOINABLE);
}
CThread::~CThread()
{
pthread_attr_destroy(&m_tThreadAttr);
OnDelete();
}
void CThread::Start()
{
if (m_bStart)
return;
int iResult = pthread_create(&m_tThread, &m_tThreadAttr, (tc_thread_func)ThreadRun, this);
if (iResult)
m_bStart = true;
}
void CThread::OnDelete()
{
int iResult;
if (m_bStart && pthread_self() != m_tThread) //end by another thread
{
iResult = pthread_cancel(m_tThread);
if (iResult)
std::cout<<"pthread_cancel error"<<std::endl;
if (!m_bDetach)
{
iResult = pthread_join(m_tThread, NULL);
if (iResult)
std::cout<<"pthread_join error"<<std::endl;
}
}
}
class CEchoThread : public CThread
{
private:
public:
CEchoThread(){}
virtual ~CEchoThread(){}
protected:
void Run()
{
std::cout<<"fuck"<<std::endl;
}
};
int main()
{
CEchoThread* obj =new CEchoThread();
obj->Start();
sleep(1);
}