Huawei machine test HJ49: multithreading

Title Description:

Problem Description: there are 4 threads and 1 common character array. The function of thread 1 is to output A to the array, the function of thread 2 is to output B to characters, the function of thread 3 is to output C to the array, and the function of thread 4 is to output D to the array. It is required to assign ABCD to the array in order. The number of ABCD is specified by the parameter of thread function 1. [Note: C language players can use WINDOWS SDK library functions]
Interface Description:
void init(); / / initialization function
void Release(); / / resource release function
unsignedint__stdcall ThreadFun1(PVOID pM)  ; / / thread function 1: pass in an int pointer [value range: 1 – 250, guaranteed by the test case], which is used to initialize the output A times. The resource needs to be released by the thread
unsignedint__stdcall ThreadFun2(PVOID pM)  ;// Thread function 2, no parameters passed in
unsignedint__stdcall ThreadFun3(PVOID pM)  ;// Thread function 3, no parameters passed in
Unsigned int __ stdcall ThreadFunc4(PVOID pM);// Thread function 4, no parameters passed in
char  g_write[1032]; / / threads 1, 2, 3 and 4 assign values to the array in order. Regardless of whether the array is out of bounds, the test case is guaranteed

Enter Description:

This question contains multiple sample inputs.
Enter an int integer

Output Description:

For each set of samples, output multiple ABCD

Example:

Input:

10

Output:

ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD

Problem solving ideas:

This problem is A standard multi-threaded problem. Mutex mutex is used to ensure the safety and stability of multi-threaded operation. thread1 is used to control the execution times, start and end of multithreading. When the string length% 4 is equal to 0, it means that the next input is A, and the num times after entering A will be reduced by one; thread2-thread4 are controlled by the finish Boolean. When multithreading, they determine whether to add corresponding letters according to the length of the string.

This problem is still relatively easy, but the C + + code of niuke.com can't run. I asked the official because the multithreading Library of C + + is not supported for the time being, so if you want to know how to do the problem, just look at code 1 below. If you want to pass Niuke's AC, use code 2.

Test code:

Code 1:

#include <iostream>
#include <thread>
#include <mutex>
#include <string>

using namespace std;

string s;
mutex m;
bool finish = false;

void thread1(int num)
{
	while (num)
	{
		if (m.try_lock())
		{
			int len = static_cast<int>(s.length());
			if (len % 4 == 0) {
				s += 'A';
				num--;
			}
			m.unlock();
		}
	}
	finish = true;
}

void thread2()
{
	while (1)
	{
		if (m.try_lock())
		{
			int len = static_cast<int>(s.length());
			if (finish && len % 4 == 0)
			{
				m.unlock();
				return;
			}
			if (len % 4 == 1)
			{
				s += 'B';
			}
			m.unlock();
		}
	}
}
void thread3()
{
	while (1)
	{
		if (m.try_lock())
		{
			int len = static_cast<int>(s.length());
			if (finish && len % 4 == 0)
			{
				m.unlock();
				return;
			}
			if (len % 4 == 2)
			{
				s += 'C';
			}
			m.unlock();
		}
	}
}
void thread4()
{
	while (1)
	{
		if (m.try_lock())
		{
			int len = static_cast<int>(s.length());
			if (finish && len % 4 == 0)
			{
				m.unlock();
				return;
			}
			if (len % 4 == 3)
			{
				s += 'D';
			}
			m.unlock();
		}
	}
}
int main() {
	int in;
	while (cin >> in)
	{
		thread t1(thread1, in);
		thread t2(thread2);
		thread t3(thread3);
		thread t4(thread4);
		t1.join();
		t2.join();
		t3.join();
		t4.join();
		cout << s << endl;
		s.clear();
		finish = false;
	}
	return 0;
}

Code 2:

#include <iostream>
#include <thread>
#include <mutex>
#include <string>

using namespace std;

int main() {
	int n;
    while (cin >> n) {
        string ret = "";
        for(int i = 0 ; i < n;i++){
            ret = ret + "ABCD";
        }
        cout << ret << endl;
    }
	return 0;
}

 

Keywords: C++

Added by raister on Sun, 19 Dec 2021 13:46:48 +0200