写了个简单的多线程,我将它封装到自己的类里面!
/*
*Date:1/Nov/2013
*Author:pjgan
*Complier:VC++2008
*/
#ifndef TICKER_H
#define TICKER_H
/*include the system head files
*/
#include <windows.h>
/*
*sell the tickets class
*/
class Ticket
{
public:
Ticket(void);
explicit Ticket(int inum_tickets);
void Sell(void);
void BeginThread(void);
void EndThread(void);
void SuspendTicketThread(void);
void ResumeTicketThread(void);
void WaitThread(void);
void TerminationTicketThread(void);
~Ticket();
private:
inline bool IsSoldOut() const {
return m_isSellOut;
}
inline HANDLE GetMutexHanle() const {
return m_hMutex;
}
static unsigned int WINAPI Thread1Proc(LPVOID lpParameter) ;
static unsigned int WINAPI Thread2Proc(LPVOID lpParameter) ;
private:
int m_ticketnum;
HANDLE m_hMutex;
HANDLE m_hThread1;
HANDLE m_hThread2;
bool m_isSellOut;
}; //end of Ticket
#endif //TICKER_H
/*
*MyThread.cpp
*/
#include <iostream>
#include <stdexcept>
#include <process.h>
#include "MyThread.h"
Ticket::Ticket() {
/*
*TODO....
*/
}
Ticket::~Ticket()
{
std::cout<<"Congratulation, the Tickets is Sold out"<<std::endl;
}
Ticket::Ticket(int inum_tickets):
m_ticketnum(inum_tickets) ,
m_isSellOut(false)
{
}
void Ticket::BeginThread() {
m_hMutex = CreateMutex( NULL, false, NULL );
if( !m_hMutex ) {
throw std::runtime_error("Build Event failed!");
}
m_hThread1 = (HANDLE)_beginthreadex( NULL, 0, Ticket::Thread1Proc, this, 0, NULL );
m_hThread2 = (HANDLE)_beginthreadex( NULL, 0, Ticket::Thread2Proc, this, 0, NULL );
}
void Ticket::EndThread() {
CloseHandle(m_hThread1);
CloseHandle(m_hThread2);
CloseHandle(m_hMutex );
}
void Ticket::SuspendTicketThread(void) {
SuspendThread(m_hThread1);
SuspendThread(m_hThread2)
}
void Ticket::ResumeTicketThread(void) {
ResumeThread(m_hThread1);
ResumeThread(m_hThread2);
}
void Ticket::TerminationTicketThread(void) {
_endthreadex(0);
}
void Ticket::WaitThread() {
WaitForSingleObject( m_hThread1, INFINITE );
WaitForSingleObject( m_hThread2, INFINITE );
}
void Ticket::Sell() {
if( m_ticketnum > 0 ) {
std::cout<<"Ticket left "<< m_ticketnum-- <<std::endl;
} else {
std::cout<<"All ticket has sold out!"<<std::endl;
m_isSellOut = true;
}
}
unsigned int WINAPI Ticket::Thread1Proc(LPVOID lpParameter) {
Ticket *pTicker = (Ticket *)lpParameter;
HANDLE h_Mutex = pTicker->GetMutexHanle();
WaitForSingleObject( h_Mutex ,INFINITE );
while (!pTicker->IsSoldOut())
{
pTicker->Sell();
}
ReleaseMutex(h_Mutex);
return 0;
}
unsigned int WINAPI Ticket::Thread2Proc(LPVOID lpParameter)
{
Ticket *pTicker = static_cast<Ticket *>(lpParameter);
HANDLE h_Mutex = pTicker->GetMutexHanle();
WaitForSingleObject( h_Mutex ,INFINITE );
while (!pTicker->IsSoldOut())
{
pTicker->Sell();
}
ReleaseMutex(h_Mutex);
return 0;
}
/*
*Date:31/oct/2013
*Author:pjgan
*Complier:VC++2008
*Theme: Study the Thread,the example is to sell the tickets;
*/
#include "MyThread.h"
int main()
{
Ticket *pticket = new Ticket(100);
pticket->BeginThread();
pticket->SuspendTicketThread();
pticket->ResumeTicketThread();
pticket->WaitThread();
pticket->EndThread();
if(pticket != NULL) {
delete pticket;
pticket = NULL;
}
return 0;
}
还需要完善!