ChaiScript / ChaiScript
1
#include <iostream>
2

3
#include <algorithm>
4

5
#ifdef CHAISCRIPT_NO_DYNLOAD
6
#include <chaiscript/chaiscript.hpp>
7
#endif
8
#include <chaiscript/chaiscript_basic.hpp>
9
#include <chaiscript/language/chaiscript_parser.hpp>
10

11 1
int expected_value(int num_iters)
12
{
13 1
  int i = 0;
14 1
  for (int k = 0; k<num_iters * 10; ++k)
15
  {
16 1
    i += k;
17
  }
18

19 1
  return i;
20
}
21

22 1
void do_work(chaiscript::ChaiScript_Basic &c, const size_t id)
23
{
24
  try{
25 1
    std::stringstream ss;
26 1
    ss << "MyVar" << rand();
27 1
    c.add(chaiscript::var(5), ss.str());
28 1
    ss.str("");
29 1
    ss << id;
30 1
    c.use("multithreaded_work.inc");
31 1
    c("do_chai_work(4000, " + ss.str() + ");");
32 0
  } catch (const std::exception &e) {
33 0
    std::cout << "exception: " << e.what() << " thread:  " << id;
34
  }
35
}
36

37 1
int main()
38
{
39
  // Disable deprecation warning for getenv call.
40
#ifdef CHAISCRIPT_MSVC
41
#ifdef max // Why Microsoft? why?
42
#undef max
43
#endif
44
#pragma warning(push)
45
#pragma warning(disable : 4996)
46
#endif
47

48 1
  const char *usepath = getenv("CHAI_USE_PATH");
49 1
  const char *modulepath = getenv("CHAI_MODULE_PATH");
50

51
#ifdef CHAISCRIPT_MSVC
52
#pragma warning(pop)
53
#endif
54

55 1
  std::vector<std::string> usepaths;
56 1
  usepaths.push_back("");
57 1
  if (usepath)
58
  {
59 1
    usepaths.push_back(usepath);
60
  }
61

62 1
  std::vector<std::string> modulepaths;
63

64
#ifdef CHAISCRIPT_NO_DYNLOAD
65
  chaiscript::ChaiScript chai(/* unused */modulepaths, usepaths);
66
#else
67 1
  modulepaths.push_back("");
68 1
  if (modulepath)
69
  {
70 1
    modulepaths.push_back(modulepath);
71
  }
72
  
73
  // For this test we are going to load the dynamic stdlib
74
  // to make sure it continues to work
75
  chaiscript::ChaiScript_Basic chai(
76 1
      std::make_unique<chaiscript::parser::ChaiScript_Parser<chaiscript::eval::Noop_Tracer, chaiscript::optimizer::Optimizer_Default>>(),
77 1
      modulepaths,usepaths);
78
#endif
79

80 1
  std::vector<std::shared_ptr<std::thread> > threads;
81

82
  // Ensure at least two, but say only 7 on an 8 core processor
83 1
  size_t num_threads = static_cast<size_t>(std::max(static_cast<int>(std::thread::hardware_concurrency()) - 1, 2));
84

85 1
  std::cout << "Num threads: " << num_threads << '\n';
86

87 1
  for (size_t i = 0; i < num_threads; ++i)
88
  {
89 1
    threads.push_back(std::make_shared<std::thread>(do_work, std::ref(chai), i));
90
  }
91

92 1
  for (size_t i = 0; i < num_threads; ++i)
93
  {
94 1
    threads[i]->join();
95
  }
96

97

98

99 1
  for (size_t i = 0; i < num_threads; ++i)
100
  {
101 1
    std::stringstream ss;
102 1
    ss << i;
103 1
    if (chai.eval<int>("getvalue(" + ss.str() + ")") != expected_value(4000))
104
    {
105 0
      return EXIT_FAILURE;
106
    }
107

108 1
    if (chai.eval<int>("getid(" + ss.str() + ")") != static_cast<int>(i))
109
    {
110 0
      return EXIT_FAILURE;
111
    }
112
  }
113

114 1
  return EXIT_SUCCESS;
115
}
116

Read our documentation on viewing source code .

Loading