Runway Management System

Hey guys i need a little help here.
i have a plane management system to build. i have completed the project and i run it in three separate threads. Here i have some problem with the threads. One thread adds the plane to the landing queue. The second thread arranges the planes in the queue depending upon the fuel left. The plane with less fuel is sorted to the first position in the queue.


i am running two threads and i want to pause sorting thread the moment i am running the adding thread. when the addition is complete i wanna run the sorting thread that sorts the planes in the queue.
I dont know how to use threads like that. locking the mutex does not work.

Here is some overview of my code.

1) Adding Thread (adds the plane to the queue)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  void Runway::addPlaneToQueue(){
	lock_guard<mutex> l(m);
	adding = true;

		time_t s = time(0);
		time_t e = time(0);
		while (!end && totalPlanes < 6){
			
			if (difftime(e, s) > 2){
				totalPlanes += 1;
				s = time(0);
				prior += 1;
				//string in;
				//cout << "Enter Plane Name   ->";
				string name;

				//getline(cin, in);
				name += "Plane " + totalPlanes;
				//cout << "Enter Fuel Remaining   ->";
				unsigned long litres = (rand() % 900) + 100;
				//getline(cin, in);
				//litres = stoul(in);
				//cout << "Fuel Consumption Rate  ->";
				int cons = (rand() % 5) + 5;
				while (cons <= 0){
					cons = (rand() % 5) + 5;
				}
				//getline(cin, in);
				//cons = stoi(in);

				Plane plane(name, litres, prior, cons);

				q.push(plane);
				cout << "Plane added" << endl;
				adding = false;
				
			}
			e = time(0);
		}
		
		
}



2) Sorting thread (sorts the planes depending upon their fuel)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Runway::updatePlanes(){
	
	time_t start = time(0);
	double d = 0;
	cout << "Thread created :)" << endl;
	while (!end){
		
		d = difftime(time(0), start);
		if (d >= 5 && !adding)
		{
			lock_guard<mutex> l(m);
			q.update(d);
			cout << "Time is " << d << endl;
			
			d = 0;
			start = time(0);
			
		}
	}
	
	
}


I hope you understand the problem well
Topic archived. No new replies allowed.