Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  Exam
  4. //
  5. //  Created by Elia Migliore on 20/07/2019.
  6. //  Copyright © 2019 Elia Migliore. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <thread>
  11. #include <chrono>
  12. #include <random>
  13. #include <sstream>
  14. #include <functional>
  15.  
  16.  
  17. void output(int val){
  18.     std::ostringstream ss;
  19.     ss << std::this_thread::get_id();
  20.     std::string idstr = ss.str();
  21.     std::string str = idstr + std::string("-") + std::to_string(val)  + '\n';
  22.    
  23.     std::cout << str;
  24. }
  25.  
  26. int rand(int min,int max){
  27.     std::random_device dev;
  28.     std::mt19937 rng(dev());
  29.     std::uniform_int_distribution<std::mt19937::result_type> dist(min,max);
  30.    
  31.     return dist(rng);
  32. }
  33.  
  34. void random_sleep(){
  35.     int sleeped_millisecond = rand(100, 10000);
  36.     std::this_thread::sleep_for(std::chrono::milliseconds(sleeped_millisecond));
  37.     output(sleeped_millisecond);
  38. }
  39.  
  40. template <typename T,typename... Args>
  41.  
  42. class MyThread {
  43. private:
  44.     std::function<void()> my_func;
  45.     T args;
  46.    
  47. public:
  48.     explicit MyThread(std::function<void(T)> func,T args){
  49.         std::function<void()> lambda = [func,args](){
  50.             random_sleep();
  51.             func(args);
  52.         };
  53.        
  54.         my_func = lambda;
  55.        
  56.     }
  57.    
  58.     void operator() () {
  59.         my_func();
  60.     }
  61. };
  62.  
  63.  
  64. template <>
  65.  
  66. class MyThread<void> {
  67. private:
  68.     std::function<void()> my_func;
  69.    
  70. public:
  71.     explicit MyThread(std::function<void()> func){
  72.         std::function<void()> lambda = [func](){
  73.             random_sleep();
  74.             func();
  75.         };
  76.        
  77.         my_func = lambda;
  78.        
  79.     }
  80.    
  81.     void operator() () {
  82.         my_func();
  83.     }
  84. };
  85.  
  86.  
  87.  
  88. int main(int argc, const char * argv[]) {
  89.     std::cout<<"started program"<<std::endl;
  90.    
  91.     auto fun = [](){
  92.         std::cout<<"program started, now random sleep"<<std::endl;
  93.     };
  94.    
  95.     MyThread<void> p{fun};
  96.     p();
  97.    
  98.     return 0;
  99. }
  100.  
  101.  
  102. // https://quuxplusone.github.io/blog/2019/03/27/design-space-for-std-function/ read that
  103. /*
  104. struct Foo {
  105.     Foo(int num) : num_(num) {}
  106.     void print_add(int i) const { std::cout << num_+i << '\n'; }
  107.     int num_;
  108. };
  109.  
  110. void print_num(int i)
  111. {
  112.     std::cout << i << '\n';
  113. }
  114.  
  115. int main()
  116. {
  117.     // store a free function
  118.     std::function<void(int)> f_display = print_num;
  119.     f_display(-9);
  120.    
  121.     // store a lambda
  122.     std::function<void()> f_display_42 = []() { print_num(42); };
  123.     f_display_42();
  124.    
  125.     // store the result of a call to std::bind
  126.     std::function<void()> f_display_31337 = std::bind(print_num, 31337);
  127.     f_display_31337();
  128.    
  129.     // store a call to a member function
  130.     std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
  131.     Foo foo(314159);
  132.     f_add_display(foo, 1);
  133. }
  134.  
  135. class Example{
  136. private:
  137.     int variable=4;
  138.     std::function<void(int)> myNonMemberFunction;
  139. public:
  140.     Example(void){
  141.     }
  142.     Example(std::function<void(int)> MyNonMemberFunction){
  143.         myNonMemberFunction=MyNonMemberFunction;
  144.     }
  145.     void callMyNonMemberFunction() {
  146.         myNonMemberFunction(variable);
  147.     }
  148. };
  149.  
  150. void PrintPlop(int v){
  151.     std::cout<<"plop"<< v << std::endl;
  152. }
  153.  
  154. int main() {
  155.     Example example(PrintPlop);
  156.     example.callMyNonMemberFunction();
  157. }
  158.  
  159. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement