65,210
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#define _CRT_RAND_S
//
#include <windows.h>
#include <iostream>
using namespace std;
//
long lWantP = 34300; //待处理数据条数
int nMaxThread = 10; //分多少个线程来处理
HANDLE* hThread = NULL; //子线程句柄
HANDLE hMain = NULL; //主线程句柄
HANDLE* hEvent = NULL; //所有事件句柄
//
int GetPVal(void); //返回待处理的数据,返回0 表示没有了
struct sctTh{
HANDLE bDelete; //保存子线程 自已创建线程时要CloseHandle的句柄
HANDLE* hThread; //子线程本身执行的句柄
int lVal; //要处理的数据值,这里只做最简单的 整型处理
int eId; //对应的事件ID值 与 hEvent 指针对应
};
sctTh* pTh = NULL;
//
CRITICAL_SECTION g_cs = {0};
CRITICAL_SECTION o_cs = {0};
void CreateChildThread(sctTh* st);
//
DWORD WINAPI ClidThread(LPVOID l)
{
sctTh* th = reinterpret_cast< sctTh* >( l );
if( !th ){ return 1; }
::WaitForSingleObject( hEvent[th->eId], INFINITE );
//if( !SetEvent( hEvent[th->eId] ) )
{
// cout<<" here has a error hEvent["<<th->eId<<"] Coundn't SetEvent signed"<<endl;return 2;
}
if( th->bDelete )
{
CloseHandle( th->bDelete );
th->bDelete = NULL;
}
th->lVal++; //这里为了测试,只是简单+1后抛弃这个数据
int val = GetPVal();
SetEvent( hEvent[th->eId] );
if( val )
{
::EnterCriticalSection( &o_cs );
cout<<" GET val: " << val <<endl;
::LeaveCriticalSection( &o_cs );
th->bDelete = *th->hThread ;
th->lVal = val;
(HANDLE)*th->hThread = ::CreateThread(NULL,0,LPTHREAD_START_ROUTINE(ClidThread),LPVOID( th ),0,NULL);
//CreateChildThread(th);
}
return 0;
}
//
void CreateChildThread(sctTh* st)
{
HANDLE close = (*st->hThread) ;
(*st->hThread) = CreateThread(NULL,0,LPTHREAD_START_ROUTINE(&ClidThread),LPVOID( st ),0,NULL);
if( ! *st->hThread ){
cout<<"Create Error!"<<endl;
}
CloseHandle( close );
}
DWORD WINAPI MainThread(LPVOID l)
{
hThread = new HANDLE[nMaxThread];
pTh = new sctTh [nMaxThread];
hEvent = new HANDLE[nMaxThread];
memset( pTh,0, sizeof(sctTh)*nMaxThread );
memset( hThread,0,sizeof(HANDLE)*nMaxThread);
//创建几个子线程来运行,直到处理完所有数据为止,线程结束后自动判断是否再次创建线程来保存有 nMaxThread 个线程在运行
for(int i=0; i<nMaxThread; i++)
{
hEvent[i] = CreateEvent(
NULL, //Default EventAttributes
FALSE, //
TRUE, //no signed at init
NULL // no name
);
if( !hEvent[i] )
{
cout<<" Couldn't Create Event now!!!"<<endl;break;
}
int val = GetPVal();
::EnterCriticalSection( &o_cs );
cout<<"Get Val:"<<val<<endl;
::LeaveCriticalSection( &o_cs );
if( !val ){ break; }
pTh[i].eId = i;
pTh[i].bDelete = NULL;
pTh[i].hThread = &hThread[i];
pTh[i].lVal = val;
hThread[i] = ::CreateThread(NULL,0,LPTHREAD_START_ROUTINE(&ClidThread),LPVOID( &pTh[i] ),0,NULL);
//Sleep(100);
if( ! hThread[i] )
{
cout<<"Create T F"<<endl;
}
}
//等所有线程结束
///::WaitForMultipleObjects( nMaxThread,hThread,TRUE,INFINITE ); //THERE have a problem
//就这样笨办法等它完哟,很可能有隐患哟
while( lWantP )
{
Sleep(1000);
}
cout<<"ALL FINISHED <<endl;
for( int i=0; i<nMaxThread; i++ )
{
CloseHandle( hThread[i] ); hThread[i] = NULL;
}
cout<<"CLOSE ALL CHILD"<<endl;
delete [] pTh; pTh = NULL;
delete [] hThread;hThread = NULL;
delete [] hEvent; hEvent = NULL;
return 0;
}
int _tmain(int,char**)
{
::InitializeCriticalSection( &g_cs );
::InitializeCriticalSection( &o_cs );
HANDLE m =(
::CreateThread( NULL, 0, LPTHREAD_START_ROUTINE(&MainThread), NULL, 0, NULL )
);
if( m )
{
::WaitForSingleObject( m ,INFINITE );
}else{
cout<<" Main Thread wasn't Created" <<endl;
}
::LeaveCriticalSection( &g_cs );
::LeaveCriticalSection( &o_cs );
return 0;
}
//
int GetPVal(void)
{
int rel = 0;
::EnterCriticalSection( &g_cs );
if( lWantP > 0)
{
rel = lWantP-- ;
}
::LeaveCriticalSection( &g_cs );
return rel;
}
long lWantP = 10; //待处理数据条数 //等所有线程结束
::WaitForMultipleObjects( nMaxThread,hThread,TRUE,INFINITE );
//
#include "stdafx.h"
#define _CRT_RAND_S
//
#include <windows.h>
#include <iostream>
using namespace std;
//
long lWantP = 100000; //待处理数据条数
int nMaxThread = 5; //分多少个线程来处理
HANDLE* hThread = NULL; //子线程句柄
HANDLE hMain = NULL; //主线程句柄
//
int GetPVal(void);
struct sctTh{
HANDLE bDelete; //保存子线程 自已创建线程时要CloseHandle的句柄
HANDLE hThread; //子线程本身执行的句柄
int lVal; //要处理的数据值,这里只做最简单的 整型处理
};
sctTh* pTh = NULL;
//
CRITICAL_SECTION g_cs = {0};
CRITICAL_SECTION o_cs = {0};
DWORD WINAPI ClidThread(LPVOID l)
{
sctTh* th = reinterpret_cast< sctTh* >( l );
if( !th ){ return 1; }
if( th->bDelete )
{
CloseHandle( th->bDelete );
th->bDelete = NULL;
}
::EnterCriticalSection( &o_cs );
cout<<" CT val: " << th->lVal <<endl;
::LeaveCriticalSection( &o_cs );
th->lVal++; //这里为了测试,只是简单+1后抛弃这个数据
int val = GetPVal();
if( val )
{
th->bDelete = th->hThread ;
th->lVal = val;
th->hThread = ::CreateThread(NULL,0,LPTHREAD_START_ROUTINE(&ClidThread),LPVOID( th ),0,NULL);
}
return 0;
}
//
DWORD WINAPI MainThread(LPVOID l)
{
hThread = new HANDLE[nMaxThread];
pTh = new sctTh [nMaxThread];
//
memset( pTh,0, sizeof(sctTh)*nMaxThread );
memset( hThread,0,sizeof(HANDLE)*nMaxThread);
//创建几个子线程来运行,直到处理完所有数据为止,线程结束后自动判断是否再次创建线程来保存有 nMaxThread 个线程在运行
for(int i=0; i<nMaxThread; i++)
{
int val = GetPVal();
if( !val ){ continue; }
pTh[i].bDelete = NULL;
pTh[i].hThread = &hThread[i];
pTh[i].lVal = val;
hThread[i] = ::CreateThread(NULL,0,LPTHREAD_START_ROUTINE(&ClidThread),LPVOID( &pTh[i] ),0,NULL);
}
//等所有线程结束
::WaitForMultipleObjects( nMaxThread,hThread,TRUE,INFINITE );
for( int i=0; i<nMaxThread; i++ )
{
CloseHandle( hThread[i] ); hThread[i] = NULL;
}
delete [] pTh;
delete [] hThread;hThread = NULL;
return 0;
}
//
int GetPVal(void)
{
int rel = 0;
::EnterCriticalSection( &g_cs );
if( lWantP > 0)
{
rel = lWantP-- ;
}
::LeaveCriticalSection( &g_cs );
return rel;
}
//
int _tmain(int argc, _TCHAR* argv[])
{
::InitializeCriticalSection( &g_cs );
::InitializeCriticalSection( &o_cs );
hMain = ::CreateThread(NULL,0,LPTHREAD_START_ROUTINE(&MainThread),LPVOID(0),0,NULL);
//if( !hMain ){ cout<<"Thread Create Failed !!"<<endl; }
::WaitForSingleObject(hMain,INFINITE);
Sleep(1000);
::DeleteCriticalSection( &g_cs );
::DeleteCriticalSection( &o_cs );
return 0;
}