當前位置:編程學習大全網 - 網站源碼 - C語言中的多線程實現

C語言中的多線程實現

多線程隨機數選擇程序

下面這個程序運行後看起來很有意思,像是壹個隨機數選擇程序,但是並不是完全按照問題寫的,供參考,改起來很容易。

//多線程隨機數選擇程序示例

# 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;

}

  • 上一篇:i5 4590的主機,請達人們點評下整體配置如何
  • 下一篇:用Autoit編程軟件,如何讀取屏幕上的信息?Autoit高手進!
  • copyright 2024編程學習大全網