Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Exam
- //
- // Created by Elia Migliore on 20/07/2019.
- // Copyright © 2019 Elia Migliore. All rights reserved.
- //
- #include <iostream>
- #include <thread>
- #include <chrono>
- #include <random>
- #include <sstream>
- #include <functional>
- void output(int val){
- std::ostringstream ss;
- ss << std::this_thread::get_id();
- std::string idstr = ss.str();
- std::string str = idstr + std::string("-") + std::to_string(val) + '\n';
- std::cout << str;
- }
- int rand(int min,int max){
- std::random_device dev;
- std::mt19937 rng(dev());
- std::uniform_int_distribution<std::mt19937::result_type> dist(min,max);
- return dist(rng);
- }
- void random_sleep(){
- int sleeped_millisecond = rand(100, 10000);
- std::this_thread::sleep_for(std::chrono::milliseconds(sleeped_millisecond));
- output(sleeped_millisecond);
- }
- template <typename T,typename... Args>
- class MyThread {
- private:
- std::function<void()> my_func;
- T args;
- public:
- explicit MyThread(std::function<void(T)> func,T args){
- std::function<void()> lambda = [func,args](){
- random_sleep();
- func(args);
- };
- my_func = lambda;
- }
- void operator() () {
- my_func();
- }
- };
- template <>
- class MyThread<void> {
- private:
- std::function<void()> my_func;
- public:
- explicit MyThread(std::function<void()> func){
- std::function<void()> lambda = [func](){
- random_sleep();
- func();
- };
- my_func = lambda;
- }
- void operator() () {
- my_func();
- }
- };
- int main(int argc, const char * argv[]) {
- std::cout<<"started program"<<std::endl;
- auto fun = [](){
- std::cout<<"program started, now random sleep"<<std::endl;
- };
- MyThread<void> p{fun};
- p();
- return 0;
- }
- // https://quuxplusone.github.io/blog/2019/03/27/design-space-for-std-function/ read that
- /*
- struct Foo {
- Foo(int num) : num_(num) {}
- void print_add(int i) const { std::cout << num_+i << '\n'; }
- int num_;
- };
- void print_num(int i)
- {
- std::cout << i << '\n';
- }
- int main()
- {
- // store a free function
- std::function<void(int)> f_display = print_num;
- f_display(-9);
- // store a lambda
- std::function<void()> f_display_42 = []() { print_num(42); };
- f_display_42();
- // store the result of a call to std::bind
- std::function<void()> f_display_31337 = std::bind(print_num, 31337);
- f_display_31337();
- // store a call to a member function
- std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
- Foo foo(314159);
- f_add_display(foo, 1);
- }
- class Example{
- private:
- int variable=4;
- std::function<void(int)> myNonMemberFunction;
- public:
- Example(void){
- }
- Example(std::function<void(int)> MyNonMemberFunction){
- myNonMemberFunction=MyNonMemberFunction;
- }
- void callMyNonMemberFunction() {
- myNonMemberFunction(variable);
- }
- };
- void PrintPlop(int v){
- std::cout<<"plop"<< v << std::endl;
- }
- int main() {
- Example example(PrintPlop);
- example.callMyNonMemberFunction();
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement