一些技巧

1.直接在控制台输入 名字 或者 名字.exe即可运行程序

2.在运行程序的时候可以使用输入输出重定向 方便调试时不用重复输入数据
比如: add.exe <./data/in.txt >./data/out.txt
3.可以使用stdexcept库自定义try catch错误信息
  1. #include<iostream>// std::cout
  2. #include<thread>// std::thread
  3. #include<mutex>// std::mutex, std::lock_guard
  4. #include<stdexcept>// std::logic_error
  5. std::mutex mtx;
  6. void print_even(int x){
  7. if(x %2==0) std::cout << x <<" is even"<< std::endl;
  8. elsethrow(std::logic_error("not even"));
  9. }
  10. void print_thread_id(int id){
  11. try{
  12. // using a local lock_guard to lock mtx guarantees unlocking on destruction / exception:
  13. std::lock_guard<std::mutex> lck(mtx);
  14. print_even(id);
  15. }
  16. catch(std::logic_error&e){
  17. std::cout <<"[exception caught]"<< e.what()<< std::endl;
  18. }
  19. }
  20. int main()
  21. {
  22. std::thread threads[10];
  23. // spawn 10 threads:
  24. for(int i =0; i <10;++i)
  25. threads[i]= std::thread(print_thread_id, i +1);
  26. for(auto& th : threads) th.join();
  27. std::cin.get();
  28. return0;
  29. }
 





posted @ 2015-12-21 18:46  狗仔小分队  阅读(93)  评论(0)    收藏  举报