hThread = _beginthread (ThreadProc, uiStackSize, pParam) ;
它更简单,对于大多数应用程序很完美,这个线程函数的语法为:
void __cdecl ThreadProc (void * pParam) ;
在建立多线程的Windows程序时,需要在「Project Settings」对话框中做一些修改。选择「C/C」页面标签,然后在「Category」下拉式清单方块中选择「Code Generation」。在「Use Run-Time Library」下拉式清单方块中,可以看到用于「Release」设定的「Single-Threaded」和用于Debug设定的「Debug Single-Threaded」。将这些分别改为「Multithreaded」和「Debug Multithreaded」。这将把编译器旗标改为/MT,它是编译器在编译多线程的应用程序所需要的。
第一个demo.
/*******************************************************
*
* deom1---四个线程同时写一个文件( 没有参数 )
*
*
***********************************************************/
#i nclude <windows.h>
#i nclude <process.h> /* _beginthread, _endthread */
#i nclude <iostream>
#i nclude <fstream>
using namespace std;
ofstream out("out.txt");
void ThreadFunc1(PVOID param)
{
while(1)
{
Sleep(10);
out<<"This was draw by thread l"<<endl;
}
}
void ThreadFunc2(PVOID param)
{
while(1)
{
Sleep(10);
out<<"This was draw by thread 2"<<endl;
}
}
void ThreadFunc3(PVOID param)
{
while(1)
{
Sleep(10);
out<<"This was draw by thread 3"<<endl;
}
}
void ThreadFun(PVOID param)
{
while(1)
{
Sleep(10);
out<<"This was draw by thread 4"<<endl;
}
}
int main()
{
int i=0;
_beginthread(ThreadFunc1,0,NULL);
_beginthread(ThreadFunc2,0,NULL);
_beginthread(ThreadFunc3,0,NULL);
_beginthread(ThreadFun,0,NULL);
Sleep(3000);
out<<"end";
return 0;
}
//demo1 end-----------------------------------------------
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-49702-5.html
这不正好给了中国军事建设南海岛礁的口实么
能打吗