fork download
  1. #include <iostream>
  2. #include <mutex>
  3. #include <functional>
  4. #include <future>
  5. #include <thread>
  6. #include <chrono>
  7. #include <random>
  8.  
  9. /** /
  10. template<class R>
  11. R Semaphore(const std::function<R(void)>& F) {
  12. static std::mutex M;
  13. std::lock_guard<std::mutex> L(M);
  14. return F();
  15. }/**/
  16. template<class R,class... Arg>
  17. R Semaphore(const std::function<R(Arg&...)>& F,const Arg& ...A) {// a good semaphoe.but compile.
  18. static std::mutex M;
  19. std::lock_guard<std::mutex> L(M);
  20. return F(A...);
  21. }/**/
  22. template<class R>
  23. R Semaphore2(const std::function<R(void)>& F,std::mutex M) {
  24. // static std::mutex M;
  25. std::lock_guard<std::mutex> L(M);
  26. return F();
  27. }
  28. /**/
  29. int main() {
  30. std::mutex M;
  31. std::mt19937 Rnd;
  32. std::uniform_int_distribution<> U(-3, 3);
  33. /** /
  34. for (std::size_t i = 0; i < 128; i++) {
  35. auto F = [i]()->bool {std::cout << i << std::endl; return true; };
  36. Semaphore<bool>(F);
  37. }
  38. /**/
  39. for (std::size_t i = 0; i < 12800; i++) {
  40. auto F = [i]()->bool {std::cout << i << std::endl; return true; };
  41. // Semaphore<bool>(F);
  42. // auto FU = std::async(std::launch::async, Semaphore<bool>, F);//compiler cant solve to some of same name functions issue.
  43. auto FU = std::async(std::launch::async, Semaphore<bool>, F,10);//compiler cant solve template argements issue.
  44. // auto FU = std::async(std::launch::async, [F]() {return Semaphore(F); });//why not...
  45. // auto FU = std::async(std::launch::async, [](auto F) {Semaphore<bool>(F); std::this_thread::sleep_for(std::chrono::nanoseconds(3)); return true; }, F);
  46. // auto FU = std::async(std::launch::async, [i]() {std::cout << i << std::endl; });//why not...
  47. // auto FU = std::async(std::launch::async, [](auto i) {std::cout << i << std::endl; },i);//why not...
  48. // auto FU = std::async(std::launch::async, [](auto F,auto R) {Semaphore<bool>(F); std::this_thread::sleep_for(std::chrono::milliseconds(R)); return true; }, F,U(Rnd));
  49. }
  50. std::this_thread::sleep_for(std::chrono::seconds(3));
  51. return 0;
  52. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:43:65: error: no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, main()::<lambda()>&, int)’
   auto FU = std::async(std::launch::async, Semaphore<bool>, F,10);//compiler cant solve template argements issue.
                                                                 ^
In file included from prog.cpp:4:
/usr/include/c++/8/future:1712:5: note: candidate: ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)’
     async(launch __policy, _Fn&& __fn, _Args&&... __args)
     ^~~~~
/usr/include/c++/8/future:1712:5: note:   template argument deduction/substitution failed:
/usr/include/c++/8/future: In substitution of ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...) [with _Fn = bool (&)(const std::function<bool()>&); _Args = {main()::<lambda()>&, int}]’:
prog.cpp:43:65:   required from here
/usr/include/c++/8/future:1712:5: error: no type named ‘type’ in ‘class std::result_of<bool (*(main()::<lambda()>, int))(const std::function<bool()>&)>’
/usr/include/c++/8/future:1745:5: note: candidate: ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(_Fn&&, _Args&& ...)’
     async(_Fn&& __fn, _Args&&... __args)
     ^~~~~
/usr/include/c++/8/future:1745:5: note:   template argument deduction/substitution failed:
/usr/include/c++/8/future: In substitution of ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(_Fn&&, _Args&& ...) [with _Fn = std::launch; _Args = {bool (&)(const std::function<bool()>&), main()::<lambda()>&, int}]’:
prog.cpp:43:65:   required from here
/usr/include/c++/8/future:1745:5: error: no type named ‘type’ in ‘class std::result_of<std::launch(bool (*)(const std::function<bool()>&), main()::<lambda()>, int)>’
stdout
Standard output is empty