Exemple #1
0
bool Game_of_life::step() {
  std::swap(_current_grid, _old_grid);
  if (_threads < 2) {
    for (int y = 0; y < _height; y++) {
      calculate_line(y, _current_grid, _old_grid, false);
    }
  } else {
    std::vector<std::thread> running{};
    std::map<int, std::vector<int>> to_calculate{};

    for (int current_line = 0; current_line < _height; current_line++) {
      to_calculate[util::positive_modulo(current_line, _threads)].push_back(
          current_line);
    }

    for (auto &l : to_calculate) {
      running.emplace_back(thread_helper, std::ref(*this), std::ref(l.second),
                           std::ref(_current_grid), std::ref(_old_grid));
    }

    for (auto &t : running) {
      t.join();
    }
    running.clear();
  }
  return !std::equal(_current_grid.begin(), _current_grid.end(),
                     _old_grid.begin());
}
Exemple #2
0
Fichier : io.c Projet : aosm/X11
int
LispFread(LispFile *file, void *data, int size)
{
    int bytes, length;
    char *buffer;

    if (!file->readable)
	return (EOF);

    if (size <= 0)
	return (size);

    length = 0;
    buffer = (char*)data;

    /* check if there is an unget character */
    if (file->available) {
	*buffer++ = file->unget;
	file->available = 0;
	if (--size == 0) {
	    if (file->unget == '\n' && !file->binary)
		++file->line;

	    return (1);
	}

	length = 1;
    }

    if (file->buffered) {
	void *base_data = (char*)data - length;

	if (file->writable) {
	    LispFflush(file);
	    bytes = read(file->descriptor, buffer, size);
	    if (bytes < 0)
		bytes = 0;
	    if (!file->binary)
		file->line += calculate_line(base_data, length + bytes);

	    return (length + bytes);
	}

	/* read anything that is in the buffer */
	if (file->offset < file->length) {
	    bytes = file->length - file->offset;
	    if (bytes > size)
		bytes = size;
	    memcpy(buffer, file->buffer + file->offset, bytes);
	    buffer += bytes;
	    file->offset += bytes;
	    size -= bytes;
	}

	/* if there is still something to read */
	if (size) {
	    bytes = read(file->descriptor, buffer, size);
	    if (bytes < 0)
		bytes = 0;

	    length += bytes;
	}

	if (!file->binary)
	    file->line += calculate_line(base_data, length);

	return (length);
    }

    bytes = read(file->descriptor, buffer, size);
    if (bytes < 0)
	bytes = 0;
    if (!file->binary)
	file->line += calculate_line(buffer - length, length + bytes);

    return (length + bytes);
}