Welcome to mirror list, hosted at ThFree Co, Russian Federation.

random.cc « util - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b1eea2b50663bdc62be30c662975f21c7a377df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "util/random.hh"

#include <cstdlib>

#include <boost/thread/locks.hpp>
// #include <boost/thread/lock_guard.hpp>
#include <boost/thread/mutex.hpp>

namespace util
{
namespace
{
/** Lock to protect randomizer.
 *
 * This module is implemented in terms of rand()/srand() from <cstdlib>.
 * These functions are standard C, but they're not thread-safe.  Scalability
 * is not worth much complexity here, so just slap a mutex around it.
 */
boost::mutex rand_lock;
} // namespace

void rand_init(unsigned int seed)
{
  boost::lock_guard<boost::mutex> lock(rand_lock);
  srand(seed);
}


void rand_init()
{
  rand_init(time(NULL));
}

namespace internal
{
// This is the one call to the actual randomizer.  All else is built on this.
int rand_int()
{
  boost::lock_guard<boost::mutex> lock(rand_lock);
  return std::rand();
}
} // namespace internal
} // namespace util