QQ网名大全

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;
}
佚名
2024-06-02 02:38:48
最佳回答
类似问题(10)