fork download
  1. #include <bits/stdc++.h>
  2. #include <thread>
  3. #include <mutex>
  4.  
  5. using namespace std;
  6.  
  7. mutex user_mutex;
  8. mutex driver_mutex;
  9. mutex ride_mutex;
  10.  
  11. class User {
  12. public:
  13. string username;
  14. string contact;
  15. string email;
  16. pair<int, int> location;
  17.  
  18. User(string username, string contact) {
  19. this->username = username;
  20. this->contact = contact;
  21. location = {0, 0};
  22. }
  23.  
  24. User() = default;
  25. };
  26.  
  27. class Driver {
  28. public:
  29. string username;
  30. int status;
  31. string vehicleNo;
  32. int earnings;
  33. pair<int, int> location;
  34.  
  35. Driver(string username, string vehicleNo, pair<int, int> location) {
  36. this->username = username;
  37. this->vehicleNo = vehicleNo;
  38. this->location = location;
  39. status = 1;
  40. earnings = 0;
  41. }
  42.  
  43. Driver() = default;
  44. };
  45.  
  46. unordered_map<string, User> users;
  47. unordered_map<string, Driver> drivers;
  48. unordered_map<string, string> activeRides;
  49. unordered_map<string, pair<pair<int, int>, pair<int, int>>> ridesRequested;
  50.  
  51. void add_user(string username, string contactNo) {
  52. lock_guard<mutex> lock(user_mutex);
  53. users[username] = User(username, contactNo);
  54. }
  55.  
  56. void update_user(string username, string updatedContact) {
  57. lock_guard<mutex> lock(user_mutex);
  58. if (users.find(username) != users.end()) {
  59. users[username].contact = updatedContact;
  60. } else {
  61. cout << "User not found" << endl;
  62. }
  63. }
  64.  
  65. void update_userLocation(string username, pair<int, int> location) {
  66. lock_guard<mutex> lock(user_mutex);
  67. if (users.find(username) != users.end()) {
  68. users[username].location = location;
  69. } else {
  70. cout << "User not found" << endl;
  71. }
  72. }
  73.  
  74. void add_driver(string name, string vehicleNo, pair<int, int> location) {
  75. lock_guard<mutex> lock(driver_mutex);
  76. drivers[name] = Driver(name, vehicleNo, location);
  77. }
  78.  
  79. void update_driverLocation(string driver_name, pair<int, int> location) {
  80. lock_guard<mutex> lock(driver_mutex);
  81. if (drivers.find(driver_name) != drivers.end()) {
  82. drivers[driver_name].location = location;
  83. } else {
  84. cout << "Driver not found" << endl;
  85. }
  86. }
  87.  
  88. void change_driver_status(string driver_name, int status) {
  89. lock_guard<mutex> lock(driver_mutex);
  90. if (drivers.find(driver_name) != drivers.end()) {
  91. drivers[driver_name].status = status;
  92. } else {
  93. cout << "Driver not found" << endl;
  94. }
  95. }
  96.  
  97. vector<string> find_ride(string username, pair<int, int> source, pair<int, int> destination) {
  98. lock_guard<mutex> lock(ride_mutex);
  99. if (users.find(username) == users.end()) {
  100. cout << "User not found" << endl;
  101. return {};
  102. }
  103.  
  104. ridesRequested[username] = {source, destination};
  105. vector<string> available_drivers;
  106.  
  107. for (auto& [name, driver] : drivers) {
  108. int dist = abs(driver.location.first - source.first) + abs(driver.location.second - source.second);
  109. if (dist <= 5 && driver.status == 1) {
  110. available_drivers.push_back(name);
  111. }
  112. }
  113.  
  114. if (available_drivers.empty()) {
  115. cout << "No ride found [Since all the drivers are more than 5 units away from user]" << endl;
  116. }
  117.  
  118. return available_drivers;
  119. }
  120.  
  121. void concurrent_find_ride(string username, pair<int, int> source, pair<int, int> destination) {
  122. vector<string> drivers_found = find_ride(username, source, destination);
  123. for (const auto& driver : drivers_found) {
  124. cout << "Driver found for " << username << ": " << driver << endl;
  125. }
  126. }
  127.  
  128. int main() {
  129. add_user("Abhay", "1234");
  130. add_user("Vikram", "2234");
  131. add_user("Kirti", "2345");
  132. update_userLocation("Abhay", {0, 0});
  133. update_userLocation("Vikram", {10, 0});
  134. update_userLocation("Kirti", {15, 6});
  135. add_driver("Driver1", "Swift, KA-01-12345", {10, 1});
  136. add_driver("Driver2", "Swift, KA-01-12345", {11, 10});
  137. add_driver("Driver3", "Swift, KA-01-12345", {5, 3});
  138.  
  139. // Create threads for each user's ride request to simulate concurrency
  140. thread thread2(concurrent_find_ride, "Vikram", make_pair(10, 0), make_pair(15, 3));
  141. thread thread1(concurrent_find_ride, "Abhay", make_pair(0, 0), make_pair(20, 1));
  142. thread thread3(concurrent_find_ride, "Kirti", make_pair(15, 6), make_pair(20, 4));
  143.  
  144. // Join threads
  145. thread1.join();
  146. thread2.join();
  147. thread3.join();
  148.  
  149. return 0;
  150. }
  151.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
No ride found [Since all the drivers are more than 5 units away from user]
No ride found [Since all the drivers are more than 5 units away from user]
Driver found for Vikram: Driver1