一些技巧
1.直接在控制台输入 名字 或者 名字.exe即可运行程序
2.在运行程序的时候可以使用输入输出重定向 方便调试时不用重复输入数据
比如: add.exe <./data/in.txt >./data/out.txt
3.可以使用stdexcept库自定义try catch错误信息
#include<iostream>// std::cout#include<thread>// std::thread#include<mutex>// std::mutex, std::lock_guard#include<stdexcept>// std::logic_errorstd::mutex mtx;void print_even(int x){if(x %2==0) std::cout << x <<" is even"<< std::endl;elsethrow(std::logic_error("not even"));}void print_thread_id(int id){try{// using a local lock_guard to lock mtx guarantees unlocking on destruction / exception:std::lock_guard<std::mutex> lck(mtx);print_even(id);}catch(std::logic_error&e){std::cout <<"[exception caught]"<< e.what()<< std::endl;}}int main(){std::thread threads[10];// spawn 10 threads:for(int i =0; i <10;++i)threads[i]= std::thread(print_thread_id, i +1);for(auto& th : threads) th.join();std::cin.get();return0;}
浙公网安备 33010602011771号