Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <mutex>
- #include <thread>
- using std::cout;
- using std::endl;
- using std::thread;
- void threadsleep(int millisecs)
- {
- std::timed_mutex m;
- m.lock();
- m.try_lock_for(std::chrono::milliseconds(millisecs));
- }
- class test
- {
- public:
- test() { }
- void start() {
- t1 = thread(&test::run1, this);
- t2 = thread(&test::run2, this);
- }
- void stop() {
- t1.join();
- t2.join();
- }
- private:
- void run1() {
- for (int i=0; i<100; i++) {
- cout << "Run 1 ";
- threadsleep(100);
- }
- cout << endl;
- }
- void run2() {
- for (int i=0; i<100; i++) {
- cout << "Run 2 ";
- threadsleep(100);
- }
- cout << endl;
- }
- thread t1, t2;
- };
- int main()
- {
- test tt;
- cout << "Start..." << endl;
- tt.start();
- threadsleep(1000);
- tt.stop();
- cout << "Done..." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement