void ThreadMessages::wait()
	{
		MutexPthreadLock lock(_mutex);
		_replyLast(0);
		while (_events.empty())		
			pthread_cond_wait(&_cond, &_mutex);			
	}
    bool ThreadDispatcher::peek(peekMessage& ev, bool del)
    {
        if (!ev.num)
            return false;


        bool ret = false;
        {
#ifndef OX_NO_MT
            MutexPthreadLock lock(_mutex);
#endif
            if (ev.num == -1)
                ev.num = (int)_events.size();

            LOGDN("peeking message");

            _replyLast(0);

            if (!_events.empty() && ev.num > 0)
            {
                if (del)
                    _popMessage(ev);
                ev.num--;
                ret = true;
            }
        }

        _runCallbacks();

        return ret;
    }
    void ThreadDispatcher::reply(void* val)
    {
#ifndef OX_NO_MT
        MutexPthreadLock lock(_mutex);
#endif
        OX_ASSERT(_last.need_reply);
        _replyLast(val);
    }
    void ThreadDispatcher::_waitMessage()
    {
#ifndef OX_NO_MT
        _replyLast(0);

        while (_events.empty())
            pthread_cond_wait(&_cond, &_mutex);
#endif
    }
	void ThreadMessages::get(message &ev)
	{		
		MutexPthreadLock lock(_mutex);
		LOGDN("ThreadMessages::get");

		_replyLast(0);

		while (_events.empty())
		{
			LOGDN("ThreadMessages::get pthread_cond_wait");
			mywait(&_cond, &_mutex);
		}

		ev = _events.front();
		_events.erase(_events.begin());
		_last = ev;
		LOGDN("ThreadMessages::get received msgid=%d id=%d", _last.msgid, _last._id);
	}
	bool ThreadMessages::peek(message &ev, bool del)
	{
		bool has = false;

		MutexPthreadLock lock(_mutex);
		_replyLast(0);

		if (!_events.empty())
		{
			ev = _events.front();
			if (del)
				_events.erase(_events.begin());
			has = true;
			_last = ev;
		}		

		return has;
	}
	void ThreadMessages::reply(void *val)
	{		
		MutexPthreadLock lock(_mutex);
		_replyLast(val);		
	}