當前位置:編程學習大全網 - 編程軟體 - C語言多線程實現

C語言多線程實現

多線程隨機選號程序

以下程序運行後看起來比較有意思,像壹個隨機選號程序,但不是完全按照問題所說的寫的 可供參考,要改很容易

//多線程隨機選號程序示例

#include <stdio.h>

#include <Windows.h>

#include <ctime>

#include <cstdlib>

#include <process.h>

bool g_run = true; //是否運行

void userInput(void*) //監視輸入的線程函數

{

while (true)

{

if (getchar()=='\n') //是否輸入回車

{

g_run = !g_run; //回車運行 回車暫停

}

Sleep(10); //延遲

}

}

int main()

{

srand(time(0)); //隨機數種子

_beginthread(userInput,0,NULL); //開線程

while (true)

{

if (g_run)

{

system("cls"); //清屏

int t = rand() % 1000+ 1;//1-1000的隨機數

printf("\n %d",t); //輸出

}

Sleep(50); //延遲50毫秒

}

return 0;

}

  • 上一篇:開關編程考研
  • 下一篇:數學CR(AUB)啥意思?
  • copyright 2024編程學習大全網