-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriverManager.cpp
More file actions
66 lines (60 loc) · 1.77 KB
/
DriverManager.cpp
File metadata and controls
66 lines (60 loc) · 1.77 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "stdafx.h"
#include "DriverManager.h"
#include "CarFactory.h"
#include "Driver.h"
#include "Car.h"
#include "autoschool.h"
int DriverManager::NumberOfManagers = 0;
DriverManager::DriverManager(const std::string& nameManager, std::shared_ptr<CarFactory>& factory)
: factory_(factory)
, nameManager_(nameManager)
{
}
void DriverManager::GetOneDriver(std::unique_ptr<Driver> CurrentDriver)
{
DriversOwnedByManagers_.push_back(std::move(CurrentDriver));
}
void DriverManager::ThreaFunctionManager(int DriverNumber)
{
int i = 4;
while (i)
{
DriversOwnedByManagers_[DriverNumber]->cleverGo();
std::this_thread::sleep_for(std::chrono::milliseconds(4000));
i--;
}
}
void DriverManager::startThread()
{
std::vector<std::thread> threads;
for (unsigned currentDriverNumber = 0; currentDriverNumber < DriversOwnedByManagers_.size(); ++currentDriverNumber)
{
threads.push_back(std::thread(&DriverManager::ThreaFunctionManager, this, currentDriverNumber));
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
for (auto& t : threads)
{
t.join();
}
}
void DriverManager::setFieldManagerToOnedDrivers(int k)
{
DriversOwnedByManagers_[k]->rememberMyManager(this);
}
std::mutex mtx;
void DriverManager::GetPtrDriverWithCar(Driver* NewDriverWhitCar)
{
std::lock_guard<std::mutex> locked(mtx);
PtrDriversWithCar_.push_back(NewDriverWhitCar);
}
Driver* DriverManager::GivePtrDriverWithCar()
{
std::lock_guard<std::mutex> locked(mtx);
if (PtrDriversWithCar_.empty())
{
throw std::runtime_error ("...tried to buy a used car but they aren’t\n");
}
Driver* p_DriverWithCar = PtrDriversWithCar_.back();
PtrDriversWithCar_.pop_back();
return p_DriverWithCar;
}