char * name; long i = 0; FILE * file; name =(char *)pvParam; printf(" Starting''%s''\ nn,quot; name); file = fopen(name," ; w"); for(i = 0; i< 10000000; i ++) { fprintf(file, "%ld\\\" ;,i); } fclose(文件); return(dwResult); } VOID主(VOID){ int cnt; int redak [MAX_M]; char a1 [] =" a1"; char b1 [] =" b1"; char c1 [] =" ; c1" ;; char d1 [] =" d1" ;; HANDLE hThreads [MAX_M]; //处理线程 //查看CreateThread的原型 函数) DWORD dwThreadID [MAX_M]; //需要ID-s //创建线程1(0)查看参数 hThreads [0] = CreateThread(NULL,0,ThFunc,(PVOID)a1,0,& dwThreadID); hThreads [1] = CreateThread(NULL,0,ThFunc,(PVOID)b1,0,& dwThreadID); hThreads [2] = CreateThread(NULL,0,ThFunc,( PVOID)c1,0,& dwThreadID); hThreads [3] = CreateThread(NULL,0,ThFunc,(PVOID)d1,0,& dwThreadID); //等到他们完成工作,等待INFINITE WaitForMultipleO bjects(MAX_M,hThreads,TRUE,INFINIT E); //关闭线程句柄 for(cnt = 0; cnt< MAX_M; cnt ++){ printf(" Closing thread%d .... \ n",cnt); CloseHandle(hThreads [cnt]); } } 我希望我能帮到你,如果你对这个问题有更多的疑问 代码请发给我邮件。 最好的问候 Sinisa Try this.#include<stdio.h>#include<windows.h> #define MAX_M 4// define max file to open// thread functionDWORD WINAPI ThFunc(PVOID pvParam){DWORD dwResult = 0; char * name ;long i=0;FILE * file; name =( char*)pvParam; printf( "Starting ''%s''\n", name );file = fopen( name, "w" ); for ( i=0;i< 10000000;i++){fprintf( file, "%ld\n", i );}fclose(file); return(dwResult);} VOID main(VOID){ int cnt;int redak[MAX_M];char a1[]="a1";char b1[]="b1";char c1[]="c1";char d1[]="d1"; HANDLE hThreads[MAX_M];// handles to threads//see prototype of CreateThreadfunction)DWORD dwThreadID[MAX_M];// need for ID-s // create thread 1(0) see argumentshThreads[0] = CreateThread(NULL,0,ThFunc,(PVOID)a1,0,&dwThreadID );hThreads[1] = CreateThread(NULL,0,ThFunc,(PVOID)b1,0,&dwThreadID );hThreads[2] = CreateThread(NULL,0,ThFunc,(PVOID)c1,0,&dwThreadID );hThreads[3] = CreateThread(NULL,0,ThFunc,(PVOID)d1,0,&dwThreadID ); // wait until they finish their job , wait INFINITEWaitForMultipleObjects(MAX_M,hThreads,TRUE,INFINIT E); //closing handles to threadfor(cnt=0; cnt < MAX_M; cnt++){printf("Closing thread %d ....\n",cnt);CloseHandle(hThreads[cnt]);}}I hope I have helped you and if you have futher questions about thiscode send me them by mail. Best regardsSinisa 这篇关于基本多线程有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-27 03:21