Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //in Linux you should compile using
- //g++ -std=c++11 file_name.cpp -pthread -o destination_file_name && ./destination_file_name
- #include <iostream>
- #include <thread>
- #include <mutex>
- #include <condition_variable>
- #include <chrono>
- std::mutex mtx;
- std::condition_variable cv;
- //thanks to
- //http://falsinsoft.blogspot.pt/2014/05/set-console-cursor-position-in-windows.html
- void setCursorPos(int XPos, int YPos)//linux version
- {
- printf("\033[%d;%dH", YPos+1, XPos+1);
- }
- int main() {
- int i;
- std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
- for(i = 0; i < 10; i++) {
- std::thread([](int position) {
- for(int x = 0; x < 10000; x++) {
- mtx.lock();
- setCursorPos(position, position);
- std::cout << "X[" << position << "] -> " << x << std::endl;
- if(x == 9999) {
- cv.notify_one();
- }
- mtx.unlock();
- }
- }, i).detach();
- }
- std::unique_lock<std::mutex> lck(mtx);
- cv.wait(lck);
- std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
- std::cout << "First thread took "
- << std::chrono::duration_cast<std::chrono::seconds>(end - start).count()
- << "s.\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement