下面這個程序運行後看起來很有意思,像是壹個隨機數選擇程序,但是並不是完全按照問題寫的,供參考,改起來很容易。
//多線程隨機數選擇程序示例
# include & ltstdio.h & gt
# include & ltWindows.h & gt
# include & ltctime & gt
# include & ltcstdlib & gt
# include & ltprocess.h & gt
bool g _ run = true//是否運行?
Void userInput(void*) //監視輸入的線程函數。
{
while(真)
{
if(getchar()= ' \ n ')//是否輸入回車?
{
g_run =!g _ run//回車運行,暫停。
}
睡眠(10);//延遲
}
}
int main()
{
srand(time(0));//隨機數種子
_beginthread(userInput,0,NULL);//打開壹個線程
while(真)
{
if (g_run)
{
系統(“cls”);//清除屏幕
int t = rand()% 1000+1;//1-1000隨機數
printf("\n %d ",t);//輸出
}
睡眠(50);//延遲50毫秒
}
返回0;
}