15,471
社区成员
发帖
与我相关
我的任务
分享
//30个线程,300个任务要执行,同时每个线程只能执行一个任务
//全部执行完后等待5秒后全部任务重新开始,直到用户发送命令终止
#include <windows.h>
#include <vector>
#include <queue>
using namespace std;
HANDLE hSemaphore;
const int NUM_TASK=300;//总任务数
const int NUM_THREAD=30;//开启线程数n个
const int NUM_WAIT=5*1000;//任务全部结束后隔n秒后,重新开始
static bool b_stop=true;
CRITICAL_SECTION m_cs;
HANDLE hEvent;
static int si_thr;//任务序号
/************************
*线程
*
*************************/
unsigned long __stdcall ThreadFunc(void* lpParam)
{
queue<int>* q_Thred=(queue<int>*)lpParam;
while(b_stop)
{
EnterCriticalSection(&m_cs);
if(q_Thred->empty())//队列为空,任务全部完成
{
printf("开始等待\n");
printf("正在等待...\n");
DWORD dwWaritRes=WaitForSingleObject(hEvent,NUM_WAIT);
switch(dwWaritRes)
{
case WAIT_OBJECT_0:
printf("遇到事件结束等待\n");
//break;
case WAIT_TIMEOUT:
printf("等待结束!\n");
b_stop=0;
break;
break;
}
}
int i_newNum=q_Thred->front();
q_Thred->pop();//取出队列,已完成该任务
LeaveCriticalSection(&m_cs);
DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject(
hSemaphore, // handle to semaphore
INFINITE); // zero-second time-out interval
switch (dwWaitResult)
{
// The semaphore object was signaled.
case WAIT_OBJECT_0:
printf("%3d:正在执行任务%3d\n",si_thr++,i_newNum);//(*q_Thred).front());
break;
// Semaphore was nonsignaled, so a time-out occurred.
case WAIT_TIMEOUT:
printf("%3d:TIMEOUT!!!!!!!!!!!!!!!!!!!!",si_thr++);
break;
}
if (!ReleaseSemaphore(
hSemaphore, // handle to semaphore
1, // increase count by one
NULL) ) // not interested in previous count
{
printf("ReleaseSemaphore error: %d\n", GetLastError());
}
}
return 0;
}
bool Start(void)
{
hSemaphore = CreateSemaphore(
NULL, // default security attributes
NUM_THREAD, // initial count
NUM_THREAD, // maximum count
NULL); // unnamed semaphore
if (hSemaphore == NULL)
{
printf("CreateSemaphore error: %d\n", GetLastError());
}
//std::vector<int> m_queueThread;//
queue<int> m_queueThread;//任务列表
for(int i=1;i<=NUM_TASK;i++)
{
m_queueThread.push(i);
}
DWORD ThreadID;
for(int i=0;i<NUM_THREAD;i++)
{
HANDLE handle=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)ThreadFunc,
&m_queueThread,
0,
&ThreadID);
SetThreadPriority(handle,THREAD_PRIORITY_IDLE);
}
return true;
}
int main()
{
int nRetCode = 0;
InitializeCriticalSection(&m_cs);
hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
si_thr=0;
if(Start())
printf("结束!\n");
{
// SetEvent(hEvent);
}
//DeleteCriticalSection(&m_cs);
return nRetCode;
}
//30个线程,300个任务要执行,同时每个线程只能执行一个任务
//全部执行完后等待5秒后全部任务重新开始,直到用户发送命令终止
#include <windows.h>
#include <assert.h>
#include <queue>
#include <time.h>
using namespace std;
HANDLE hSemaphore;
const int NUM_TASK=135;//总任务数
const int NUM_THREAD=13;//开启线程数n个
const int NUM_WAIT=3*1000;//任务全部结束后隔n秒后,重新开始
CRITICAL_SECTION m_cs;
HANDLE hEvent;
int si_thr=1;//任务序号
static int i_thread=1;
unsigned long __stdcall ThreadFuncMain(void* lpParam);//主线程
unsigned long __stdcall ThreadFuncChild(void* lpParam);//子线程
/************************
*主线程
*
*************************/
unsigned long __stdcall ThreadFuncMain(void* lpParam)
{
bool b_Working=true;
while(b_Working)
{
printf("\n开始了!\n");
si_thr=1;
queue<int> m_queueThread;//任务列表
// m_queueThread.clear();
assert(m_queueThread.empty());
for(int i=1;i<=NUM_TASK;i++)
{
m_queueThread.push(i);
}
DWORD ThreadID;
PHANDLE m_phThreads;
m_phThreads = new HANDLE[NUM_THREAD];
for(int i=0;i<NUM_THREAD;i++)
{
m_phThreads[i]=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)ThreadFuncChild,
&m_queueThread,
0,
&ThreadID);
SetThreadPriority(m_phThreads[i],THREAD_PRIORITY_IDLE);
//
}
DWORD dwWr=WaitForMultipleObjects(NUM_THREAD,m_phThreads,true,INFINITE);
// if(dwWr==WAIT_OBJECT_0||dwWr==WAIT_TIMEOUT||dwWr==WAIT_FAILED||dwWr==WAIT_ABANDONED_0)//在这里错了,子线程没有全部完成任务??怎么改??
//{
printf("任务完成!\n");
DWORD dwWaritRes=WaitForSingleObject(hEvent,NUM_WAIT);
switch(dwWaritRes)
{
case WAIT_OBJECT_0:
printf("遇到事件结束等待\n");
b_Working=false;
break;
case WAIT_TIMEOUT:
printf("等待结束!\n");
break;
//break;
}
//}
for(int i;i<NUM_THREAD;i++)
CloseHandle(m_phThreads[i]);
delete []m_phThreads;
//delete m_queueThread;
printf("WaitForMultipleObjects任务完成!\n");
}
return 0;
}
/************************
*子线程
*
*************************/
unsigned long __stdcall ThreadFuncChild(void* lpParam)
{
queue<int>* q_Thred=(queue<int>*)lpParam;
//bool b_Work=true;
while(true)
{
EnterCriticalSection(&m_cs);
if(q_Thred->empty())//队列为空,任务全部完成//size()==0)//
{
LeaveCriticalSection(&m_cs);
//b_Work=false;
break;
}
int i_newNum=q_Thred->front();
q_Thred->pop();//取出队列,已完成该任务
printf("%3d:正在执行任务%3d\n",si_thr++,i_newNum);//(*q_Thred).front());
LeaveCriticalSection(&m_cs);
srand((unsigned)time(0));
int x=(rand()*si_thr)%500;
Sleep(x);
printf("\t\t\t睡眠%d秒后结束\n",x);
//
}
printf("结束一个子线程%d\n",i_thread++);
return 1;
}
/**********************************
*开启主线程
*
**********************************/
bool Start(void)
{
HANDLE hHandel=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)ThreadFuncMain,
NULL,
0,
NULL);
WaitForSingleObject(hHandel,INFINITE);
printf("开启主线程!\n");
return true;
}
int main()
{
//if(getchar()==(int)"a")
//{
// printf("yes");
// SetEvent(hEvent);
//}
int nRetCode = 0;
hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
si_thr=0;
InitializeCriticalSection(&m_cs);
if(Start())
printf("结束!\n");
DeleteCriticalSection(&m_cs);
return nRetCode;
}
现在这个才是对的,原来LeaveCriticalSection(&m_cs);写错地方了//30个线程,300个任务要执行,同时每个线程只能执行一个任务
//全部执行完后等待5秒后全部任务重新开始,直到用户发送命令终止
#include <windows.h>
#include <assert.h>
#include <queue>
using namespace std;
HANDLE hSemaphore;
const int NUM_TASK=333;//总任务数
const int NUM_THREAD=44;//开启线程数n个
const int NUM_WAIT=3*1000;//任务全部结束后隔n秒后,重新开始
CRITICAL_SECTION m_cs;
HANDLE hEvent;
int si_thr=1;//任务序号
unsigned long __stdcall ThreadFuncMain(void* lpParam);//主线程
unsigned long __stdcall ThreadFuncChild(void* lpParam);//子线程
/************************
*主线程
*
*************************/
unsigned long __stdcall ThreadFuncMain(void* lpParam)
{
bool b_Working=true;
while(b_Working)
{
printf("\n开始了!\n");
InitializeCriticalSection(&m_cs);
si_thr=1;
queue<int> m_queueThread;//任务列表
// m_queueThread.clear();
assert(m_queueThread.empty());
for(int i=1;i<=NUM_TASK;i++)
{
m_queueThread.push(i);
}
DWORD ThreadID;
PHANDLE m_phThreads;
m_phThreads = new HANDLE[NUM_THREAD];
for(int i=0;i<NUM_THREAD;i++)
{
m_phThreads[i]=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)ThreadFuncChild,
&m_queueThread,
0,
&ThreadID);
SetThreadPriority(m_phThreads[i],THREAD_PRIORITY_IDLE);
}
//DWORD dwWr=
WaitForMultipleObjects(NUM_THREAD,m_phThreads,false,INFINITE);
//if(dwWr==WAIT_OBJECT_0||dwWr==WAIT_TIMEOUT)//在这里错了,子线程没有全部完成任务??怎么改??
//{
printf("任务完成!\n");
DWORD dwWaritRes=WaitForSingleObject(hEvent,NUM_WAIT);
switch(dwWaritRes)
{
case WAIT_OBJECT_0:
printf("遇到事件结束等待\n");
b_Working=false;
break;
case WAIT_TIMEOUT:
printf("等待结束!\n");
DeleteCriticalSection(&m_cs);
break;
//break;
}
//}
}
return 0;
}
/************************
*子线程
*
*************************/
unsigned long __stdcall ThreadFuncChild(void* lpParam)
{
queue<int>* q_Thred=(queue<int>*)lpParam;
while(true)
{
EnterCriticalSection(&m_cs);
if(q_Thred->empty())//队列为空,任务全部完成
{
break;
}
int i_newNum=q_Thred->front();
q_Thred->pop();//取出队列,已完成该任务
printf("%3d:正在执行任务%3d\n",si_thr++,i_newNum);//(*q_Thred).front());
LeaveCriticalSection(&m_cs);
Sleep(500);
// printf("%3d:睡眠结束\n",si_thr);
}
return 0;
}
/**********************************
*开启主线程
*
**********************************/
bool Start(void)
{
HANDLE hHandel=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)ThreadFuncMain,
NULL,
0,
NULL);
WaitForSingleObject(hHandel,INFINITE);
printf("开启主线程!\n");
return true;
}
int main()
{
int nRetCode = 0;
hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
si_thr=0;
if(Start())
printf("结束!\n");
{
// SetEvent(hEvent);
}
return nRetCode;
}
谢谢大家,听了oyljerry的方法,主线程控制30个子线程循环执行300个任务;