Example #1
0
int main()
{
  std::thread producer1(producer, 0, 10);
  std::thread producer2(producer, 10, 20);
  std::thread producer3(producer, 20, 30);
  std::thread consumer1(consumer, 20);
  std::thread consumer2(consumer, 10);

  producer1.join();
  producer2.join();
  producer3.join();
  consumer1.join();
  consumer2.join();
}
Example #2
0
	~destroy_producer_proxy()
	{		
		static auto destroyers = std::make_shared<tbb::concurrent_bounded_queue<std::shared_ptr<executor>>>();
		static tbb::atomic<int> destroyer_count;

		try
		{
			std::shared_ptr<executor> destroyer;
			if(!destroyers->try_pop(destroyer))
			{
				destroyer.reset(new executor(L"destroyer"));
				destroyer->set_priority_class(below_normal_priority_class);
				if(++destroyer_count > 16)
					CASPAR_LOG(warning) << L"Potential destroyer dead-lock detected.";
				CASPAR_LOG(trace) << "Created destroyer: " << destroyer_count;
			}
				
			auto producer = producer_.release();
			auto pool	  = destroyers;
			destroyer->begin_invoke([=]
			{
				std::unique_ptr<std::shared_ptr<frame_producer>> producer2(producer);

				auto str = (*producer2)->print();
				try
				{
					if(!producer->unique())
						CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
					else
						CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
				}
				catch(...){}
								
				producer2.reset();
				pool->push(destroyer);
			}); 
		}
		catch(...)
		{
			CASPAR_LOG_CURRENT_EXCEPTION();
			try
			{
				producer_.reset();
			}
			catch(...){}
		}
	}
Example #3
0
    bool test3Threads()
    {
        std::atomic<bool> go { false };
        std::thread producer1(
            [&](){
                while(!go);
                for(D::Msg i = 3; i < iterations; i += 2)
                {
                    Put() = i;
                }
            });

        std::thread producer2(
            [&](){
                while(!go);
                for(D::Msg i = 2; i < iterations; i += 2)
                {
                    Put() = i;
                }
            });

        go = true;
        D::Msg oldOdd = 1;
        D::Msg oldEven = 0;
        for(size_t i = 2; i < iterations;)
        {
            for(auto msg: Get())
            {
//                dbg<D>();
                
                ++i;
                D::Msg& old = msg & 0x1L ? oldOdd : oldEven;
                if(msg != old + 2)
                {
                    // std::cout << "old: " << old << ", new: " << msg
                    //           << std::endl;
                    return false;
                }
                old = msg;
            }
        }
        std::cout << "Cons done" << std::endl;
        producer1.join();
        producer2.join();
        return true;
    }
int main() {

  std::shared_ptr<BlockingQueue<int> > int_queue(new BlockingQueue<int>(10));

  std::thread producer1(Producer(), std::ref(*int_queue));
  std::thread producer2(ProducerWaiter(), std::ref(*int_queue));
  std::thread consumer1(ConsumerWaiter(), std::ref(*int_queue));
  std::thread consumer2(ConsumerWaiter(), std::ref(*int_queue));

  /** producers and consumers are blocked, useless */
  producer1.join();
  producer2.join();

  consumer1.join();
  consumer2.join();

  return 0;
}
void test_multi() {
    sc::blocking_queue<MyMovableStr> queue{};
    auto take = [&](size_t count) {
        for (size_t i = 0; i < count; i++) {
            MyMovableStr el{""};
            bool success = queue.take(el);
            slassert(success);
            slassert(42 == el.get_val().size());
        }
    };
    auto put = [&](size_t count) {
        TestStringGenerator gen{};
        for (size_t i = 0; i < count; i++) {
            std::string str = gen.generate(42);
            queue.emplace(std::move(str));
        }
    };
    std::thread producer1(put, 100);
    std::thread producer2(put, 100);
    std::thread producer3(put, 100);
    std::thread consumer1(take, 50);
    std::thread consumer2(take, 50);
    std::thread consumer3(take, 50);
    std::thread consumer4(take, 50);
    std::thread consumer5(take, 50);
    std::thread consumer6(take, 50);
    producer1.join();
    producer2.join();
    producer3.join();
    consumer1.join();
    consumer2.join();
    consumer3.join();
    consumer4.join();
    consumer5.join();
    consumer6.join();
}