#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
DWORD WINAPI threadFunc(LPVOID pParam);
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
while (TRUE)
{
i++;
CreateThread(NULL,0,threadFunc,&i,0,NULL);
Sleep(10000);
}
}
DWORD WINAPI threadFunc(LPVOID pParam)
{
int* i = (int *)pParam;
while (TRUE)
{
cout<<*i<<endl;
Sleep(1000);
}
return 0;
}
只输出新创建的线程了以前的线程好像被覆盖了一样,为什么无法输出以前创建的线程内容了呢?
但是我看任务管理器里面线程并没有减少 为何?
#include <iostream>
#include <windows.h>
using namespace std;
DWORD WINAPI threadFunc(LPVOID pParam);
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
while (TRUE)
{
i++;
CreateThread(NULL,0,threadFunc,&i,0,NULL);
Sleep(10000);
}
}
DWORD WINAPI threadFunc(LPVOID pParam)
{
int* i = (int *)pParam;
while (TRUE)
{
cout<<*i<<endl;
Sleep(1000);
}
return 0;
}
只输出新创建的线程了以前的线程好像被覆盖了一样,为什么无法输出以前创建的线程内容了呢?
但是我看任务管理器里面线程并没有减少 为何?
DWORD WINAPI threadFunc(LPVOID pParam)
{
int* i = (int *)pParam;
while (TRUE)
{
cout<< "thread_id:" << GetCurrentThreadId() << " i:" << *i<<endl;
Sleep(1000);
}
return 0;
}
DWORD WINAPI threadFunc(LPVOID pParam)
{
int i = *(int *)pParam;
while (TRUE)
{
cout<< i <<endl;
Sleep(1000);
}
return 0;
}
若主函数是这样写:
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
while (TRUE)
{
i++;
if (1 == i)
{
CreateThread(NULL,0,threadFunc,&i,0,NULL);
}
Sleep(10000);
}
return 0;
}
就能避免多次创建线程,不知道是否符合楼主的问题