Example #1
0
int main(int argc, char** argv)
{

    Communicator communicator(argv[1]);
    Waiter* waiter = new Waiter(SINGLE_MSG);

    InterAppMessage message3 = INCOMING_BEFORE_MSG;
    InterAppMessage message2 = APPLICATION_UP_MSG;

    InterAppMessage result;

    communicator.addWaiter(waiter);

    std::cout << "!!!!!!!!!!!!!!!!!!!! created" << std::endl;

//TEST1  
    result = waiter->waitMessage(message2, 20000);
    std::cout << "!!!!!!!!!!!!!!!!!!!! wait complete, result: " << result << std::endl;
    communicator.sendMessage(message3);
    std::cout << "!!!!!!!!!!!!!!!!!!!! sent" << std::endl;

//TEST2
    result = waiter->waitMessage(message2, 10000);
    std::cout << "!!!!!!!!!!!!!!!!!!!! wait complete, result: " << result << std::endl;
    result = waiter->waitMessage(message3, 30000);
    std::cout << "!!!!!!!!!!!!!!!!!!!! wait complete, result: " << result << std::endl;
    std::cout << "!!!!!!!!!!!!!!!!!!!! end: " << std::endl;
    sleep(10);
    communicator.sendMessage(message2);

    communicator.removeWaiter(waiter);

    return 0;
}
Example #2
0
void WorkerTable::Wait(int id) {
  // CHECK(waitings_.find(id) != waitings_.end());
  m_->lock();
  CHECK(waitings_[id] != nullptr);
  Waiter* w = waitings_[id];
  m_->unlock();

  w->Wait();

  m_->lock();
  delete waitings_[id];
  waitings_[id] = nullptr;
  m_->unlock();
}
Example #3
0
//客户端
int main()
{
	//店里添加烤肉师傅、菜单、服务员等顾客
	Barbucer* barbucer=new Barbucer();
	Command* cmd= new BakeMuttonCmd(barbucer);
	Command* cmd2=new ChickenWingCmd(barbucer);
	Waiter* girl = new Waiter();
	//点菜
	girl->SetCmd(cmd);
	girl->SetCmd(cmd2);
	//服务员通知
	girl->Notify();
	system("pause");
	return 0;
}
//小商贩通过waiter卖水果
void test3()
{
	Vendor *v = new Vendor;
	AppleCommand *ac = new AppleCommand(v);
	BananaCommand *bc = new BananaCommand(v);

	Waiter *w = new Waiter;
	w->setCommand(ac);
	w->sail();

	w->setCommand(bc);
	w->sail();

	delete w;
	delete bc;
	delete ac;
	delete v;

}
Example #5
0
void Mutex::Unlock()
{
    std::uint64_t head = 0;
    if (mWaiters.CompareAndSwap(head, MUTEX_FREE_BIT))
        return;

    ExponentialBackoff backoff;
    for (;;)
    {
        CRUNCH_ASSERT_MSG((head & MUTEX_FREE_BIT) == 0, "Attempting to release unlocked mutex");

        Waiter* headPtr = Detail::WaiterList::GetPointer(head);
        if (headPtr == nullptr)
        {
            CRUNCH_ASSERT((head & Detail::WaiterList::LOCK_BIT) == 0);

            // No waiters, attempt to set free bit.
            if (mWaiters.CompareAndSwap(head, MUTEX_FREE_BIT))
                return;
        }
        else if (head & Detail::WaiterList::LOCK_BIT)
        {
            // Wait for lock release.
            head = mWaiters.Load(MEMORY_ORDER_RELAXED);
        }
        else 
        {
            // Try to pop first waiter off list and signal
            std::uint64_t const newHead =
                Detail::WaiterList::SetPointer(head, headPtr->next) +
                Detail::WaiterList::ABA_ADDEND;

            if (mWaiters.CompareAndSwap(head, newHead))
            {
                headPtr->Notify();
                return;
            }
        }

        backoff.Pause();
    }
}
Example #6
0
					void Client::main(std::string args[])
					{
						//先要启动后台,让整个程序运行起来
						CookManager::runCookManager();

						//为了简单,直接用循环模拟多个桌号点菜
						for(int i = 0;i<5;i++)
						{
							//创建服务员
							Waiter *waiter = new Waiter();
							//创建命令对象,就是要点的菜
							Command *chop = new ChopCommand(i);
							Command *duck = new DuckCommand(i);
							//点菜,就是把这些菜让服务员记录下来
							waiter->orderDish(chop);
							waiter->orderDish(duck);
							//点菜完毕
							waiter->orderOver();
						}
					}
Example #7
0
int main() {
    Barbecuer* boy = new Barbecuer();
    Command* com1 = new BakeMuttonCommand(boy);
    Command* com2 = new BakeMuttonCommand(boy);
    Command* com3 = new BakeChickenWingCommand(boy);
    Waiter* waiter = new Waiter();
    waiter->setOrder(com1);
    waiter->setOrder(com2);
    waiter->setOrder(com3);
    waiter->excute();
    waiter->cancelOrder(com2);
    waiter->excute();
    return 0;
}
Example #8
0
void testBuilder()
{
	std::cout << "---------------------testBuilder.[start]" << std::endl;
	Waiter* waiter = new Waiter();
	PizzaBuilder* pb = new ChinesePizzaBuilder();

	std::cout << "-------Chinest Pizza---------" << std::endl;
	waiter->setPizzaBuilder(pb);
	waiter->constructPizza();

	delete pb;
	std::cout << "------America Pizza----------" << std::endl;
	pb = new AmericaPizzaBuilder();
	waiter->setPizzaBuilder(pb);
	waiter->constructPizza();

	Pizza * p = waiter->getPizza();

	std::cout << "---------------------testBuilder.[end]" << std::endl;
}
Example #9
0
void RunLoopPort::SignalWaiterLocked(Waiter & waiter,size_t myIndex) {
    if (m_signalled && waiter.Signal(myIndex)) {
        m_signalled=false;
    }
}