void* thr2(void* arg)
{
  if(__VERIFIER_nondet_int())
    my_thread1();
  else
    my_thread2();
  return 0;
}
Exemplo n.º 2
0
int main(void)
{
  __CPROVER_ASYNC_01: monitor();
  while(1)
  {
      __CPROVER_ASYNC_02: my_thread1();
      __CPROVER_ASYNC_03: my_thread2();
  }
}
Exemplo n.º 3
0
int main(int , char**)
{

    std::cout << "Starting hard_work, ready?";
    std::cin.ignore(); 
    std::thread my_thread(hard_work);
    my_thread.join();

    std::cout << "Starting background_task, ready?";
    std::cin.ignore();
    background_task bt;
    bt();
    std::thread my_thread2(bt);
    //std::thread my_thread2({background_task()});
    my_thread2.join();

    std::cout << "Starting lambda, ready?";
    std::cin.ignore();
    std::thread my_thread3(
        []() 
        {
            hard_work();
            more_hard_work_after_hard_work();
        });
    my_thread3.join();

    std::cout << "ready to crash?";
    std::cin.ignore();
    {
        std::thread my_thread(
        []() 
        {
            hard_work();
            more_hard_work_after_hard_work();
        });
        my_thread.detach();
    }

    {
        std::cout << "ready to pass arguments?" << std::endl;
        std::cin.ignore();
        int a = 42;
        std::thread my_thread(foo, a, "soy un thread!");
        std::string str = "soy otro thread";
        std::thread my_thread2([](int a1, const std::string& a2) { std::cout << "t2: a1=" << a1 << " a2=" << a2 << std::endl; }, a, str);
        std::thread my_thread2_capture([&]() { std::cout << "t2 capturing: a1=" << a << " a2=" << str << std::endl; });
        background_task_with_args bt1;
        std::thread my_thread3(bt1,a, "casi el final...");
        background_task_with_args_in_ctor bt2(a, "el final del todo");
        std::thread my_thread4(bt2);

        my_thread.join();
        my_thread2.join();
        my_thread2_capture.join();
        my_thread3.join();
        my_thread4.join();

    }
    {

        //std::cout << "ready to pass arguments (oops)?" << std::endl;
        //std::cin.ignore();
        //ooops(1000);
        //std::cin.ignore();
    }
    {
        std::unique_ptr<big_object> bo(new big_object);
        bo->load("machodedatos.raw");
        //std::thread process_thread(process_big_object, std::move(bo));
        //process_thread.join();
    }
    {
        std::thread r_thread = return_thread();
        join_thread(std::move(r_thread));
    }
    {
        std::cout << "ready to pass arguments with ScopedThread?" << std::endl;
        std::cin.ignore();
        int a = 42;
        ScopedThread my_thread(std::thread(foo, a, "soy un thread!"));
        std::string str = "soy otro thread";
        ScopedThread my_thread2(std::thread([](int a1, const std::string& a2) { std::cout << "t2: a1=" << a1 << " a2=" << a2 << std::endl; }, a, str));
        ScopedThread my_thread3(std::thread([&]() { std::cout << "t2 capturing: a1=" << a << " a2=" << str << std::endl; }));
        background_task_with_args bt1;
        ScopedThread my_thread4(std::thread(bt1,a, "casi el final..."));
        background_task_with_args_in_ctor bt2(a, "el final del todo");
        ScopedThread my_thread5((std::thread(bt2)));
    }

    {
        std::cout << "ready to count cpus?" << std::endl;
        std::cin.ignore();
        std::cout << "num of cpus:" << std::thread::hardware_concurrency() << std::endl;
    }

    {
        std::cout << "ready to check ids?" << std::endl;
        std::cin.ignore();
        std::cout << "main thread id=" << std::this_thread::get_id() << std::endl;
        std::thread t(check_id);
        std::thread::id id = t.get_id();
        t.join();
        std::cout << "thread id=" << id << std::endl;
    }

    {
        std::cout << "ready to fill and find in a list?" << std::endl;
        std::cin.ignore();
        auto producer_function = [](unsigned int numelements)
        {
            for(unsigned int i=0;i<numelements;i++)
            {
                add_to_list(i);
                std::this_thread::sleep_for(std::chrono::milliseconds(1));
            }
        };
        ScopedThread thread_producer1(std::thread(producer_function,100));
        ScopedThread thread_producer2(std::thread(producer_function,100));
        auto finder_function = [](unsigned int value_to_find) 
        {
            auto attempts = 0u;
            bool found = false;
            while(!found && attempts < 100)
            {
                found = contains_in_list(value_to_find);
                attempts++;
                std::this_thread::sleep_for(std::chrono::milliseconds(10));
            }
            if(found)
                std::cout << std::this_thread::get_id() << ": Found! after " << attempts << " attempts" << std::endl;
            else
                std::cout << std::this_thread::get_id() << ": not found! :(" << std::endl;
        };
        ScopedThread thread_finder1(std::thread(finder_function,88));
        ScopedThread thread_finder2(std::thread(finder_function,101));
    }
    {

        std::cout << "ready to crush a stack?" << std::endl;
        std::cin.ignore();
        std::stack<int> s;
        s.push(42);
        if(!s.empty())
        {
            int const value = s.top();
            s.pop();
            foo(value, "do_something");
        }
        std::atomic<int> producersWorking(0);
        std::atomic<bool> started(false);
        ThreadSafeStack<int> ts;
        auto producer_function = [&started, &producersWorking](ThreadSafeStack<int>& ts, unsigned int numelements)
        {
            producersWorking++;
            started = true;
            for(unsigned int i=0;i<numelements;i++)
            {
                ts.push(i);
                std::this_thread::sleep_for(std::chrono::milliseconds(1));
            }
            producersWorking--;
        };
        auto consumer_function = [&started, &producersWorking](ThreadSafeStack<int>& ts)
        {
            while (!started) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); }
            while(producersWorking > 0)
            {
                int value;
                if (ts.pop(value))
                {
                    std::cout << std::this_thread::get_id() << ":popped value=" << value << std::endl;
                    std::this_thread::sleep_for(std::chrono::milliseconds(10));
                }
            }
            std::cout << std::this_thread::get_id() << ":stack is empty" << std::endl;
        };
        ScopedThread thread_producer1(std::thread(producer_function,std::ref(ts),100));
        ScopedThread thread_producer2(std::thread(producer_function,std::ref(ts),100));
        ScopedThread thread_producer3(std::thread(producer_function, std::ref(ts), 100));
        ScopedThread thread_producer4(std::thread(producer_function, std::ref(ts), 100));
        ScopedThread thread_consumer1(std::thread(consumer_function,std::ref(ts)));
        ScopedThread thread_consumer2(std::thread(consumer_function,std::ref(ts)));
        ScopedThread thread_consumer3(std::thread(consumer_function, std::ref(ts)));
    }

    return EXIT_SUCCESS;
}