windows c thread

/* 
	cpp_demo.cpp 
	MSDN帮助手册:http://msdn.microsoft.com/zh-cn/library/ms235302(v=VS.80).aspx
	只是针对CRT(C RunTime)的 _beginthread 和 _beginthreadex 函数创建多线程
	MFC和WIN32 Api创建的线程,请参考其他:
	运行时需要使用管理员身份运行此实例。
	@author:lx
*/

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#include <..\crt\src\mtdll.h>

struct parameter 
{
	int max;
	int min;
};
void work(void*);

int file_size[]={512,1024,3068,102400};

int _tmain(int argc, _TCHAR* argv[])
{
	printf("starting......\n");
	const int array_length=sizeof(file_size)/sizeof(file_size[0]);
	struct parameter* args;

	HANDLE threads[array_length];

	for (int i=0;i<array_length;i++)
	{
		args=(struct parameter*)malloc(sizeof(struct parameter));
		if (i==0)
		{
			args->min=0;
			args->max=file_size[0];
		}else
		{
			args->min=file_size[i-1];
			args->max=file_size[i];
		}
		threads[i]=(HANDLE)_beginthread(work,0,args);
	}
	//设置等待
	WaitForMultipleObjects(2, threads, TRUE, INFINITE);
	printf("ending......\n");
	return 0;
}

void work(void* arg)
{
	struct parameter* para=(struct parameter*)arg;
	printf("this is a thread:[min-max]:[%d-%d],pid is %d,id is %d\n",para->min,para->max,_getpid(),_threadid);
	char* cmd=(char*)malloc(1000);
	sprintf(cmd,"fsutil file  createnew c:\\test.%d.bin %d",para->max,para->min);
	printf("%s\n",cmd);
	system(cmd);

}

你可能感兴趣的:(windows)