void signalHandler(int num) { printf("Server shutting down !\n"); stopThreadsMutex.lock(); stopThreads = true; stopThreadsMutex.unlock(); }
//this is a private worker thread function. Each thread picks the next ungenerated //row of pixels, generates it, then starts the next one void MandelbrotViewer::genLine() { int iter, row, column; sf::Vector2<double> point; double x_inc = interpolate(area.width, res_width); double y_inc = interpolate(area.height, res_height); sf::Color color; while(!restart_gen) { //the mutex avoids multiple threads writing to variables at the same time, //which can corrupt the data mutex1.lock(); row = nextLine++; //get the next ungenerated line mutex1.unlock(); //return when it finishes the last row if (row >= res_height) break; for (column = 0; column < res_width; column++) { iter = escape(row, column); //mutex this too so that the image is not accessed multiple times simultaneously mutex2.lock(); image.setPixel(column, row, findColor(iter)); image_array[row][column] = iter; mutex2.unlock(); } } }
bool checkStopThreads() { stopThreadsMutex.lock(); bool result = stopThreads; stopThreadsMutex.unlock(); return result; }
static void Add(TScript* script) { if (m_scripts.find(script->GetScriptName()) != m_scripts.end()) Assert(true, FS("ScriptRegistry<T>::Add Script '%%%' is already in holder!", script->GetScriptName())); // Check pointer has? m_mutex.lock(); m_scripts[script->GetScriptName()] = script; m_mutex.unlock(); }
void popAll() { std::vector<Invocation> others; mutex.Lock(); others.reserve(q.size()); for (int i = 0, n = q.size(); i < n; i++) { others.push_back(q.front()); q.pop(); } mutex.Unlock(); for (Invocation& invok : others) { invok.invoke(); } }
int main() { sf::RenderWindow window(sf::VideoMode(800, 600), "Sistragatiwe"); window.setFramerateLimit(30); World world(window, "resources/maps/test/test.xml"); // not that eveything is initialized start the // game logic loop sf::Thread logic_thread(&World::gameLogic, &world); logic_thread.launch(); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { window.close(); } else if (event.type == sf::Event::Resized) { sf::View view(window.getView()); view.setSize(event.size.width, event.size.height); window.setView(view); } else if (event.type == sf::Event::MouseButtonReleased) { sf::Vector2i pos = sf::Mouse::getPosition(window); sf::Vector2f v = window.mapPixelToCoords(pos); } mutex.lock(); world.events.push(event); mutex.unlock(); } mutex.lock(); window.clear(sf::Color(25, 130, 225)); window.draw(world.map); window.draw(world.gui); window.display(); mutex.unlock(); } logic_thread.terminate(); return 0; }
virtual void Run() { mapIsUpdating = true; // std::cout << "maxAfterWallPos: " << maxAfterWallPos << ", wallPos: " << wallPos << std::endl; GlobalMutex.Lock(); while (true) { if (direction == util::Key::LEFT && leftWallPos > maxAfterWallPos && leftWallPos > 0) leftWallPos--; else if (direction == util::Key::RIGHT && leftWallPos < maxAfterWallPos && leftWallPos < mapModel->getMapWidth() - subMapWidth) leftWallPos++; else if (direction == util::Key::DOWN && topWallPos < maxAfterWallPos && topWallPos < mapModel->getMapHeight() - subMapHeight) topWallPos++; else if (direction == util::Key::UP && topWallPos > maxAfterWallPos && topWallPos > 0) topWallPos--; else break; sf::Sleep(0.1f); } GlobalMutex.Unlock(); mapIsUpdating = false; }
static void Remove(TScript* script) { m_mutex.lock(); m_scripts.erase(script->GetScriptName()); m_mutex.unlock(); }