Changeset 3948


Ignore:
Timestamp:
Jul 20, 2020, 9:12:46 AM (3 years ago)
Author:
Peter
Message:

replace boost::thread with std::thread in Scheduler. refs #953

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/priority_queue.cc

    r3832 r3948  
    2626#include "Suite.h"
    2727
    28 #include <boost/thread.hpp>
    29 
    3028#include <iostream>
     29#include <memory>
     30#include <thread>
     31#include <vector>
    3132
    3233using namespace theplu::yat;
     
    5051        }
    5152        suite_.out() << str;
    52         boost::this_thread::interruption_point();
    5353      }
    5454    }
     
    8181    suite.out() << "popped one element in main thread: " << str << "\n";
    8282
    83   boost::thread_group workers;
    84   workers.create_thread(Worker(queue, suite));
    85   workers.create_thread(Worker(queue, suite));
     83  std::vector<std::thread> threads;
     84  for (size_t i=0; i<2; ++i)
     85    threads.push_back(std::thread(Worker(queue, suite)));
    8686
    8787  sleep(2);
    8888  queue.push("poison pill");
    8989
    90   workers.join_all();
    91   suite.out() << "\n";
     90  for (size_t i=0; i<threads.size(); ++i)
     91    threads[i].join();
    9292
    9393  typedef std::greater<std::string> MyComp;
  • trunk/test/queue.cc

    r3062 r3948  
    2626#include "yat/utility/Queue.h"
    2727
    28 #include <boost/thread.hpp>
    29 
    3028#include <iostream>
     29#include <thread>
     30#include <vector>
    3131
    3232using namespace theplu::yat;
     
    5050      }
    5151      sleep(1);
    52       // Make sure we can be interrupted
    53       boost::this_thread::interruption_point();
    5452    }
    5553  }
     
    6563  queue.push("Peter");
    6664
    67   boost::thread_group workers;
    68   workers.create_thread(Worker(queue));
    69   workers.create_thread(Worker(queue));
    70   workers.create_thread(Worker(queue));
    71   workers.create_thread(Worker(queue));
     65  std::vector<std::thread> threads;
     66  threads.push_back(std::thread(Worker(queue)));
     67  threads.push_back(std::thread(Worker(queue)));
     68  threads.push_back(std::thread(Worker(queue)));
     69  threads.push_back(std::thread(Worker(queue)));
    7270
    73   workers.join_all();
     71  for (size_t i=0; i<threads.size(); ++i)
     72    threads[i].join();
    7473
    7574  Queue<std::string> queue2(queue);
  • trunk/test/queue2.cc

    r3063 r3948  
    2626#include "Suite.h"
    2727
    28 #include <boost/thread.hpp>
    29 
    3028#include <iostream>
     29#include <thread>
     30#include <vector>
    3131
    3232using namespace theplu::yat;
     
    5050        }
    5151        suite_.out() << str;
    52         boost::this_thread::interruption_point();
    5352      }
    5453    }
     
    7776  queue.push("!!!");
    7877
    79   boost::thread_group workers;
    80   workers.create_thread(Worker(queue, suite));
    81   workers.create_thread(Worker(queue, suite));
     78  std::vector<std::thread> threads;
     79  threads.push_back(std::thread(Worker(queue, suite)));
     80  threads.push_back(std::thread(Worker(queue, suite)));
    8281
    8382  sleep(2);
    8483  queue.push("poison pill");
    8584
    86   workers.join_all();
     85  for (size_t i=0; i<threads.size(); ++i)
     86    threads[i].join();
    8787  suite.out() << "\n";
    8888  return suite.return_value();
  • trunk/yat/utility/Scheduler.cc

    r3921 r3948  
    7878        We cannot relaunch a thread, so instead we create a new thread
    7979        and move its guts to our member variable, job_handler_.
    80         In c++11 we could have done:
    81         job_handler_ = boost::thread(JobHandler(data_));
    82         but in c++98 we instead use swap
    8380       */
    84       JobHandler tmp_handler(data_);
    85       boost::thread tmp(tmp_handler);
    86       swap(tmp, job_handler_);
     81      job_handler_ = std::thread(JobHandler(data_));
    8782    }
    8883    data_.messengers().push(boost::make_shared<JobMessenger>(job));
     
    197192
    198193  Scheduler::Job::status_type
    199   Scheduler::Job::status(const boost::unique_lock<boost::mutex>& lock) const
     194  Scheduler::Job::status(const std::unique_lock<std::mutex>& lock) const
    200195  {
    201196    return status_;
     
    203198
    204199
    205   void Scheduler::Job::status(const boost::unique_lock<boost::mutex>& lock,
     200  void Scheduler::Job::status(const std::unique_lock<std::mutex>& lock,
    206201                              Scheduler::Job::status_type s)
    207202  {
     
    243238      // return job to scheduler
    244239      completed_.push(boost::make_shared<JobMessenger>(job));
    245 
    246       // Make sure we can be interrupted
    247       boost::this_thread::interruption_point();
    248240    }
    249241  }
     
    289281
    290282
    291   boost::mutex& Scheduler::Dependency::mutex(void)
     283  std::mutex& Scheduler::Dependency::mutex(void)
    292284  {
    293285    return mutex_;
     
    425417  void Scheduler::JobHandlerData::Count::decrement(void)
    426418  {
    427     boost::unique_lock<boost::mutex> lock(mutex_);
     419    std::unique_lock<std::mutex> lock(mutex_);
    428420    assert(x_ > 0);
    429421    --x_;
     
    433425  int Scheduler::JobHandlerData::Count::get(void) const
    434426  {
    435     boost::unique_lock<boost::mutex> lock(mutex_);
     427    std::unique_lock<std::mutex> lock(mutex_);
    436428    return x_;
    437429  }
     
    440432  void Scheduler::JobHandlerData::Count::increment(void)
    441433  {
    442     boost::unique_lock<boost::mutex> lock(mutex_);
     434    std::unique_lock<std::mutex> lock(mutex_);
    443435    ++x_;
    444436  }
     
    447439  void Scheduler::JobHandlerData::Count::set(int x)
    448440  {
    449     boost::unique_lock<boost::mutex> lock(mutex_);
     441    std::unique_lock<std::mutex> lock(mutex_);
    450442    x_ = x;
    451443  }
     
    465457    for (size_t i=0; i<n; ++i) {
    466458      Worker worker(data_->queue(), data_->messengers());
    467       workers_.push_back(boost::make_shared<boost::thread>(worker));
     459      workers_.push_back(boost::make_shared<std::thread>(worker));
    468460    }
    469461    n_target_workers_ += n;
     
    641633        break;
    642634      }
    643 
    644       // Make sure we can be interrupted
    645       boost::this_thread::interruption_point();
    646635    }
    647636
  • trunk/yat/utility/Scheduler.h

    r3918 r3948  
    2727
    2828#include <boost/exception_ptr.hpp>
    29 #include <boost/thread.hpp>
    3029#include <boost/shared_ptr.hpp>
    3130
    3231#include <list>
     32#include <mutex>
    3333#include <set>
     34#include <thread>
    3435#include <vector>
    3536
     
    116117      friend class JobHandler;
    117118      void add_prerequisite(const boost::shared_ptr<Job>&,
    118                             const boost::unique_lock<boost::mutex>& lock);
     119                            const std::unique_lock<std::mutex>& lock);
    119120      // \brief remove job from list of prerequisite
    120121      // \return number of prerequisite (after removal)
    121122      size_t remove_prerequisite(const boost::shared_ptr<Job>&,
    122                                  const boost::unique_lock<boost::mutex>& lock);
     123                                 const std::unique_lock<std::mutex>& lock);
    123124      // set of jobs that have to finish before this can run
    124125      std::set<boost::shared_ptr<Job> > prerequisite_;
     
    126127      // functions below passing Dependency::Lock
    127128      std::set<boost::shared_ptr<Job> >&
    128       prerequisite(const boost::unique_lock<boost::mutex>& lock);
     129      prerequisite(const std::unique_lock<std::mutex>& lock);
    129130      const std::set<boost::shared_ptr<Job> >&
    130       prerequisite(const boost::unique_lock<boost::mutex>& lock) const;
     131      prerequisite(const std::unique_lock<std::mutex>& lock) const;
    131132      // - pristine is what it says
    132133      // - prepared - job has been either submitted directly a job that
     
    136137      enum status_type { pristine, prepared, running, completed};
    137138      status_type status_;
    138       status_type status(const boost::unique_lock<boost::mutex>& lock) const;
    139       void status(const boost::unique_lock<boost::mutex>& lock, status_type s);
     139      status_type status(const std::unique_lock<std::mutex>& lock) const;
     140      void status(const std::unique_lock<std::mutex>& lock, status_type s);
    140141      unsigned priority_;
    141142      unsigned id_;
     
    281282    {
    282283    public:
    283       typedef boost::unique_lock<boost::mutex> Lock;
     284      typedef std::unique_lock<std::mutex> Lock;
    284285
    285286      void add(const JobPtr& child, const JobPtr& parent, const Lock& lock);
    286287      void remove(const JobPtr& parent, const Lock& lock);
    287288      std::vector<JobPtr>& children(const JobPtr& key, const Lock& lock);
    288       boost::mutex& mutex(void);
    289     private:
    290       boost::mutex mutex_;
     289      std::mutex& mutex(void);
     290    private:
     291      std::mutex mutex_;
    291292
    292293      // job in key has to finish before jobs in vector
     
    315316        void set(int x);
    316317      private:
    317         mutable boost::mutex mutex_;
     318        mutable std::mutex mutex_;
    318319        int x_;
    319320      };
     
    341342      JobQueue queue_;
    342343
    343       boost::mutex mutex_;
     344      std::mutex mutex_;
    344345
    345346      Count running_jobs_;
     
    384385
    385386      void send2queue(JobPtr& job,
    386                       const boost::unique_lock<boost::mutex>& lock);
     387                      const std::unique_lock<std::mutex>& lock);
    387388
    388389      JobHandlerData* data_;
     
    391392      void join_workers(void);
    392393
    393       typedef std::list<boost::shared_ptr<boost::thread> > WorkerList;
     394      typedef std::list<boost::shared_ptr<std::thread> > WorkerList;
    394395      // We keep workers here (rather than in JobHandler::operator()
    395396      // scope), so we can access it from other functions and don't
     
    403404    void throw_if_error(void) const;
    404405
    405     mutable boost::mutex mutex_;
     406    mutable std::mutex mutex_;
    406407    JobHandlerData data_;
    407     boost::thread job_handler_;
     408    std::thread job_handler_;
    408409  }; // end class Scheduler
    409410
Note: See TracChangeset for help on using the changeset viewer.