Ejemplo n.º 1
0
void player(int active)
{
    boost::unique_lock<boost::mutex> lock(mutex);

    int other = active == PLAYER_A ? PLAYER_B : PLAYER_A;

    while (state < GAME_OVER)
    {
        //std::cout << player_name(active) << ": Play." << std::endl;
        state = other;
        cond.notify_all();
        do
        {
            cond.wait(lock);
            if (state == other)
            {
                std::cout << "---" << player_name(active)
                          << ": Spurious wakeup!" << std::endl;
            }
        } while (state == other);
    }

    ++state;
    std::cout << player_name(active) << ": Gone." << std::endl;
    cond.notify_all();
}
Ejemplo n.º 2
0
    void deactivate_()
    {
      if ( active_)
	{
	  active_ = false;
	  not_empty_cond_.notify_all();
	  not_full_cond_.notify_all();
	}
    }
Ejemplo n.º 3
0
void
interpreter_lock_impl::unlock() {
	boost::mutex::scoped_lock lock(mutex_);
	assert(NULL != thread_state_);
	/* thread_state_ = */ PyEval_SaveThread();
	if (!--thread_lock_count_) {
		condition_.notify_all();
	}
}
Ejemplo n.º 4
0
 void SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
 {
     if (client_state != net::CLIENT_STATE_CONNECTED)
     {
         std::cout << "Disconnected from server." << std::endl;
         
         this->client_id_ = 0;
         finish = true;
         on_message_condition.notify_all();
     }
 }
Ejemplo n.º 5
0
void
interpreter_lock_impl::unlock_thread() {
	boost::mutex::scoped_lock lock(mutex_);
	while (thread_lock_count_ > 0) {
		condition_.wait(lock);
	}
	assert(NULL != thread_state_);
	PyEval_RestoreThread(thread_state_);
	thread_lock_count_ = -1;
	thread_state_ = NULL;
	condition_.notify_all();
}
    void release_read_lock()
    {
        boost::mutex::scoped_lock lock(mtx);
        --num_readers;

        if(num_readers == 0)
        {
            // must notify_all here, since if there are multiple waiting writers
            // they should all be woken (they continue to acquire the lock 
            // exclusively though)
            no_readers.notify_all();
        }
    }
Ejemplo n.º 7
0
int main()
{
    state = START;

    boost::thread thrda(&player, PLAYER_A);
    boost::thread thrdb(&player, PLAYER_B);

    boost::xtime xt;
    boost::xtime_get(&xt, boost::TIME_UTC_);
    xt.sec += 1;
    boost::thread::sleep(xt);
    {
        boost::unique_lock<boost::mutex> lock(mutex);
        std::cout << "---Noise ON..." << std::endl;
    }

    for (int i = 0; i < 10; ++i)
        cond.notify_all();

    {
        boost::unique_lock<boost::mutex> lock(mutex);
        std::cout << "---Noise OFF..." << std::endl;
        state = GAME_OVER;
        cond.notify_all();
        do
        {
            cond.wait(lock);
        } while (state != BOTH_PLAYERS_GONE);
    }

    std::cout << "GAME OVER" << std::endl;

    thrda.join();
    thrdb.join();

    return 0;
}
Ejemplo n.º 8
0
int main(int argc, char* argv[])
{
    state = START;

    boost::thread thrda(thread_adapter(&player, (void*)PLAYER_A));
    boost::thread thrdb(thread_adapter(&player, (void*)PLAYER_B));

    boost::xtime xt;
    boost::xtime_get(&xt, boost::TIME_UTC);
    xt.sec += 1;
    boost::thread::sleep(xt);
    {
        boost::mutex::scoped_lock lock(mutex);
        std::cout << "---Noise ON..." << std::endl;
    }

    for (int i = 0; i < 1000000; ++i)
        cond.notify_all();

    {
        boost::mutex::scoped_lock lock(mutex);
        std::cout << "---Noise OFF..." << std::endl;
        state = GAME_OVER;
        cond.notify_all();
        do
        {
            cond.wait(lock);
        } while (state != BOTH_PLAYERS_GONE);
    }

    std::cout << "GAME OVER" << std::endl;

    thrda.join();
    thrdb.join();

    return 0;
}
Ejemplo n.º 9
0
BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
{
  switch (ctrl_type)
  {
  case CTRL_C_EVENT:
  case CTRL_BREAK_EVENT:
  case CTRL_CLOSE_EVENT:
  case CTRL_SHUTDOWN_EVENT:
    {
      boost::mutex::scoped_lock terminationLock(terminationMutex);
      terminationRequested = true;
      terminationCondition.notify_all(); // should be just 1
      return TRUE;
    }
  default:
    return FALSE;
  }
}
Ejemplo n.º 10
0
 void release_write_lock()
 {        
     boost::mutex::scoped_lock lock(mtx);
     is_current_writer = false;
     writer_finished.notify_all();
 }
Ejemplo n.º 11
0
    void SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, common::Byteset data)
    {
        for (unsigned int n = 0; n < data.GetSize(); n++)
        {
            this->buffer_.push_back(data[n]);
        }

        while (this->buffer_.size() >= 8)
        {
            std::string command = "";
            command += (char)this->buffer_[0];
            command += (char)this->buffer_[1];
            command += (char)this->buffer_[2];
            command += (char)this->buffer_[3];

            std::string payload_length_str = "";
            payload_length_str += (char)this->buffer_[4];
            payload_length_str += (char)this->buffer_[5];
            payload_length_str += (char)this->buffer_[6];
            payload_length_str += (char)this->buffer_[7];
            
            unsigned int payload_length = boost::lexical_cast<unsigned int>(payload_length_str);
            
            if (this->buffer_.size() - 8 < payload_length)
            {
                return;
            }
         
            std::string payload = "";
            for (unsigned int n = 8; n < payload_length + 8; n++)
            {
                payload += (char)this->buffer_[n];
            }
            
            this->buffer_.erase(this->buffer_.begin(), this->buffer_.begin() + payload_length + 8);
            
            if (command == "TEXT")
            {
                std::cout << payload << std::flush;
            }
            else if (command == "PROM")
            {
                this->prompt_ = payload;
                on_message_condition.notify_all();
            }
            else if (command == "COMP")
            {
                autocomplete_list.clear();
                
                if (payload.length() > 0)
                {
                    boost::algorithm::split(autocomplete_list, payload, boost::is_any_of("\n"), boost::algorithm::token_compress_on);
                }
                
                on_message_condition.notify_all();
            }
            else
            {
                std::cerr << "Could not parse package." << std::endl;
                finish = true;
                on_message_condition.notify_all();
                break;
            }
        }
    }
Ejemplo n.º 12
0
void WServer::terminate()
{
    boost::mutex::scoped_lock terminationLock(terminationMutex);
    terminationRequested = true;
    terminationCondition.notify_all(); // should be just 1
}
Ejemplo n.º 13
0
inline void
boost_threaded_monitor::notify_all() {
	cond_.notify_all();
}