本学期首次课老师给的题目,刚开始我很懵,后来查阅网上资源才大概做出来了。
以下是详情,请各位大神多多指教!!!
题目:基于windows/visulstudio x64 实现多线程编程
要求:
1、每个线程启动后对全局变量s加1,并输出加1之后的结果到屏幕与文件;
2、对全局变量加1后线程sleep 1-1000ms,sleep的时间是一个1-1000的随机数;
3、保存加1之后的s的值到文件c:\temp\r.txt中,每个s值一行;
4、如果r.txt中出现两个相同的s值,则本门课的理论与实验课成绩***为不及格***;
5、r.txt中每行s值信息的格式为:
进程编号, 时间 s值
其中时间的格式为: 08:59:59.123,例如
12115, 09:12:55.226 1
21125, 09:12:58.309 2
12115, 09:12:59.337 3
下面先上我的完整代码:
1、头文件,注意包括fstream//文件流库函数
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include<time.h>
#include <vector>
#include <fstream> //文件流库函数
using namespace std;
static int s = 0;
HANDLE cout_mutex;
2、以下输出格式稍微调整,方便查看。
DWORD WINAPI Fun(LPVOID lpParamter)//函数名字可随意
{
// 把lpParamter当成void指针就完事儿了
int *a = (int *)lpParamter;
//等待其他线程释放锁,并获取锁的使用权
//获取锁之后,只要没有解锁,其他线程就会阻塞在WaitForSingLeObject()语句
WaitForSingleObject(cout_mutex, INFINITE);
s++;
int y = rand() % 1001 + 1;//1-1000ms
Sleep(y);
SYSTEMTIME sys;
GetLocalTime(&sys);//获取系统时间 可至毫秒 年.月.日.时:分:秒.毫秒
cout << left;
cout << GetCurrentThreadId() << " ";
printf("%02d:%02d:%02d.%03d \t", sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds);
cout << s << endl;
if (s == 1)
{
ofstream output;//名字可随意
output.open("C:\\Temp\\r.txt");//覆盖
// output the matrix to the file
if (output.is_open())
{
output << "线程id" << " " << "时间" << " " << "s值" << endl;
output << GetCurrentThreadId() << " " << sys.wHour << ":" << sys.wMinute << ":"
<< sys.wSecond << "." << sys.wMilliseconds << " " << s << endl;//文件中输出
}
output.close();//关闭 人走关门
ReleaseMutex(cout_mutex);//解锁
}
else
{
ofstream outputfile;//名字可随意
outputfile.open("C:\\Temp\\r.txt", ios::app);//app不覆盖
// output the matrix to the file
if (outputfile.is_open())
{
outputfile << GetCurrentThreadId() << " " << sys.wHour << ":" << sys.wMinute << ":"
<< sys.wSecond << "." << sys.wMilliseconds << " " << s << endl;//文件中输出
}
outputfile.close();//关闭 人走关门
ReleaseMutex(cout_mutex);//解锁
}
return 0L;
}
3、主函数
int main()
{
int a[10000];
cout_mutex = CreateMutex(NULL, FALSE, NULL);
cout << "线程id" << " " << "时间" << " " << "s值" << endl;
for (int i = 1; i < 10001; i++)
{
a[i] = i;
HANDLE hThread = CreateThread(NULL, 0, Fun, a + i, 0, NULL);//创建线程
}
Sleep(1000000);
system("pause");
return 0;
}
4、运行结果如下图:运行之后记事本也会有数据,本来名字叫temp.txt,后来改成r,txt了。
5、下面我便介绍下各个功能函数完整代码:
我的创建线程代码:
HANDLE hThread = CreateThread(NULL, 0, Fun, a + i, 0, NULL);//创建线程
c++编程运行输出结果同时输入到文件保存
输完一次信息记得换行,便可保证一行只有一组信息。
此处包含多线程锁定知识,保证输出不混乱,不出错吧
//等待其他线程释放锁,并获取锁的使用权
//获取锁之后,只要没有解锁,其他线程就会阻塞在WaitForSingLeObject()语句
WaitForSingleObject(cout_mutex, INFINITE);
ofstream output;//名字可随意
output.open("r.txt");//覆盖上一次文件中保存的数据
// output the matrix to the file
if (output.is_open())
{
output << "线程id" << " " << "时间" << " " << "s值" << endl;
output << GetCurrentThreadId() << " " << sys.wHour << ":" << sys.wMinute << ":" << sys.wSecond << "." << sys.wMilliseconds << " " << s << endl;//文件中输出
}
output.close();//关闭 人走关门
ReleaseMutex(cout_mutex);//解锁,这样不会抢而导致输出混乱
不覆盖版:
ofstream outputfile;//名字可随意
outputfile.open("r.txt", ios::app);//app不覆盖!!!直接在上次文件尾写入新的数据
// output the matrix to the file
if (outputfile.is_open())
{
outputfile << GetCurrentThreadId() << " " << sys.wHour << ":" << sys.wMinute << ":" << sys.wSecond << "." << sys.wMilliseconds << " " << s << endl;//文件中输出
}
outputfile.close();//关闭 人走关门
ReleaseMutex(cout_mutex);//解锁
此处我为了每运行一次便覆盖原先的数据,因此我在一次运行时第一个写入文件操作中选择了覆盖写入操作,后面的便直接在文件未尾加入数据。
如果有更简便的方法,求多多指教!
获取系统时间并输出:(此处只输出时分秒毫秒,若也需年月日和星期,操作中加上便好)
SYSTEMTIME sys;
GetLocalTime(&sys);//获取系统时间 可至毫秒 年.月.日.时:分:秒.毫秒
printf("%02d:%02d:%02d.%03d \t", sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds);
//需要年月日的如下操作就可以了:
printf("%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d/n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,
sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
Sleep函数延迟:(此处是随机延迟1-1000毫秒)
得#include<windows.h>
int y = rand() % 1001 + 1;//生成随机数
Sleep(y);
操作系统课程的作业,望各位大神多多纠正,指教!谢谢啦哈哈
求解:我这个算是多线程同时运行吗?老师上面写的是进程编号,不过解释时好像是说线程id,照我的代码,获取我的进程编号都是一样的,我的应该是一个进程拥有多个线程。第一次写博客over…
有帮助?那就–>biu开启传送 希望分享的东西对大家有所帮助咯!