//最简单的创建多线程实例
#include <stdio.h> #include <windows.h> //子线程函数 1 DWORD WINAPI ThreadFun1(LPVOID pM) { while (1) { printf("子线程的线程1 "); return 0; } } //子线程函数 2 int i=0;DWORD WINAPI ThreadFun2(LPVOID pM) { FILE *fp; char buffer[] = { 'x' , 'y' , 'z' }; i=i+3; printf("i is %d ",i);//printf("子线程输出数字:%d\n",si--); while (1) { printf("子线程的线程2"); fp = fopen ( "myfile.txt" , "wb" ); fseek(fp,100,0); fwrite (buffer , sizeof(buffer), 1 , fp ); fclose (fp); /* fp=fopen("jpeg.txt","wb"); fprintf(fp,"1"); fclose(fp); // fp = NULL; //需要指向空,否则会指向原打开文件地址 */ return 0; } } struct PAR //定义一个结构体,把需要的两个参数传给add函数!{ int a; int b;}; //主函数,所谓主函数其实就是主线程执行的函数。 int main() { HANDLE handle1; HANDLE handle2; printf(" 最简单的创建多线程实例\n"); printf(" -- by MoreWindows( ) --\n\n"); while (1) { handle1= CreateThread(NULL, 0, ThreadFun1, NULL, 0, NULL); WaitForSingleObject(handle1, INFINITE);handle2= CreateThread(NULL, 0, ThreadFun2, NULL, 0, NULL);
WaitForSingleObject(handle2, INFINITE); // Sleep(100); printf(" 最简单的创建多线程实例\n"); } return 0; }