Advertisement
C0BRA

Untitled

May 18th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. #include "Util.hpp"
  5.  
  6. // I am well aware that his leaks memory!
  7.  
  8.  
  9. // what I had in windows, which compiled in VS
  10. // however, sometimes the stack fucked up, and arguments supplied were seemingly random
  11. // I couldn't track this down, until I tried to compile this in G++ 4.8
  12. template<typename Args...>
  13. void CallIn(double seconds, std::function<void(Args...)> callback, Args... params)
  14. {
  15.     std::thread* pThread = new std::thread([&]()
  16.     {
  17.         Util::Sleep(seconds);
  18.         callback(params...);
  19.     });
  20. }
  21.  
  22. //Fixed function:
  23. template<typename Args...>
  24. void CallIn(double seconds, std::function<void(Args...)> callback, Args... params)
  25. {
  26.     auto func = [](double sec, std::function<void(Args...)> cb, Args... args)
  27.     {
  28.         Util::Sleep(sec);
  29.         cb(args...)
  30.     };
  31.    
  32.     auto bound = std::bind(func, sec, cb, args...);
  33.     std::thread *pThread = new std::thread(bound);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement