Esempio n. 1
0
		/**
		* @brief Copy constructor
		* @param in_sem original semaphore
		*/
		Semaphore(const Semaphore &in_sem){
			int value = in_sem.value();
#ifdef WIN32
			_sem = CreateSemaphore(0,value,LMAXIMUMCOUNT,0);
#else
			sem_init(&_sem,0,value);
#endif
		}
Esempio n. 2
0
TEST(EventCount, Simple) {
  // We're basically testing for no deadlock.
  static const size_t count = 300000;

  enum class Op {
    UP,
    DOWN
  };
  std::vector<std::pair<Op, int>> ops;
  std::mt19937 rnd(randomNumberSeed());
  randomPartition(rnd, Op::UP, count, ops);
  size_t uppers = ops.size();
  randomPartition(rnd, Op::DOWN, count, ops);
  size_t downers = ops.size() - uppers;
  VLOG(1) << "Using " << ops.size() << " threads: uppers=" << uppers
          << " downers=" << downers << " sem_count=" << count;

  std::shuffle(ops.begin(), ops.end(), std::mt19937(std::random_device()()));

  std::vector<std::thread> threads;
  threads.reserve(ops.size());

  Semaphore sem;
  for (auto& op : ops) {
    int n = op.second;
    if (op.first == Op::UP) {
      auto fn = [&sem, n] () mutable {
        while (n--) {
          sem.up();
        }
      };
      threads.push_back(std::thread(fn));
    } else {
      auto fn = [&sem, n] () mutable {
        while (n--) {
          sem.down();
        }
      };
      threads.push_back(std::thread(fn));
    }
  }

  for (auto& thread : threads) {
    thread.join();
  }

  EXPECT_EQ(0, sem.value());
}
Esempio n. 3
0
		/**
		* @brief Copy method
		* @param in_sem original semaphore
		*/
		void operator=(const Semaphore &in_sem){
			reset(in_sem.value());
		}