Experiment About demostrating how the multiple thread system run

Teacher’s homework

Problem: Being based on the enviroment of windows/visual studio x64, we are required to finish a task about muliple thread programing.

Requirements:
1. Add 1 to the global variable s after each thread is started, and output the result after adding 1 to the screen and file;
2. After adding 1 to the global variable, the thread sleeps 1-1000ms, and the sleep time is a random number 1-1000;
3. Save the value of s after adding 1 to the file c: \ temp \ r.txt, each value of s is one line
4. If there are two identical s values in r.txt, the theoretical and experimental grades of this course will fail ***
5. The format of each line of s value information in r.txt is:
Thread ID, Time, s

The format of time: 08:59:59.123,

exp:
12115, 09:12:55.226 1
21125, 09:12:58.309 2
12115, 09:12:59.337 3

**

The code we run in experiment

**

#include<iostream>
#include<Windows.h>
#include<thread>
#include<fstream>
using namespace std;

//As the requirement we need to set s as global variation
int s = 0;

//The function about a thread
DWORD WINAPI Fun(LPVOID lpParameter)
{
	ofstream fout2;
	DWORD ThreadID = GetCurrentThreadId();
	
	//In this function, we suppose to display the thread of ID, system time like hours, minutes and second
	for (int i = 0; i <= 1000; i++)
	{
		SYSTEMTIME sys1;
		GetLocalTime(&sys1);
		cout  << ThreadID << " "<< sys1.wHour << ":" << sys1.wMinute << ":" << sys1.wSecond << "." << sys1.wMilliseconds << " " << ++s<<" "<<endl;

		//According to the requirement, we are ought to write the imoformation about thread into a text document
		fout2.open("c:\\temp\\r.txt", ios::out | ios::app);
		fout2 << ThreadID << " " << sys1.wHour << ":" << sys1.wMinute << ":" << sys1.wSecond << "." << sys1.wMilliseconds << " " << s << " " << endl;
		fout2.close();
		Sleep(rand()%1000+1);
	}
	return 0L;
}


int main()
{
	//To use the multitiple thread, it might be a good idea to initialize a new thread.
	HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);

	//As a multiple thread system, each thread works by term, so when a thread finish its work, it must be paused.
	CloseHandle(hThread);
	for (int i = 0; i <= 1000; i++)
	{	
		//cout << "Main Thread Display!" << "\n";
		Sleep(rand() % 1000 + 1);
	}
	return 0;
}

The theory in description

  • We are required to comprehense about the definition about thread and procession. Both of nouns are from Operation System. We can learn their definition after you read some references about Operation System.

  • After we get to know about the definition of thread and procession. Let’s begin our experiment to comprehense how the thread run.

  • According to the requirement from our tutor, we firstly set s as global variable to be a counter of threads.

  • Then we should define a funtion about how to create a thread. In this function, we are permitted to design several steps on my own, like collect some imformation about each thread. We can call some standard function in C program to show some basic imformation about thread.

  • The main function is an entrance of a program. If you want to make the multiple threads run, it is invetible to initialize some new threads in main function to work by term.

Result

  • Crewly print in blackboard

在这里插入图片描述

  • Save in txt document
    在这里插入图片描述
发布了7 篇原创文章 · 获赞 5 · 访问量 864

猜你喜欢

转载自blog.csdn.net/weixin_44944722/article/details/104906537