Sleep sort is a sorting method that uses multithreading to add each element of a list to a new list after certain time proportiional to that element has gone by, this is my code:
  template < typename T >
  void sleepSort(vector < T > & list) {
    vector<T> newList;
    vector<thread> threads;
    for(auto element: list){
      threads.emplace_back([&newList, element](){
        int timeToSleep = float(element) / 100;
        this_thread::sleep_for(chrono::seconds(timeToSleep));
        newList.push_back(element);
      });
    }
    
    for (auto& thread : threads) {
      thread.join();
    }
    for(int i = 0; i < list.size(); i++){
      list[i] = newList[i];
    }
  }
The program enters the function but never exits it, I cant figure out why.
Aucun commentaire:
Enregistrer un commentaire