RInside Version 0.2.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
boostEx.cpp
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; -*-
2 
3 #include <boost/thread.hpp>
4 #include <boost/bind.hpp>
5 
6 #include <RInside.h>
7 
8 class Resource {
9 public:
10  Resource(): i(0), RR(RInside::instance()) { }
11 
12  void use() {
13  boost::mutex::scoped_lock lock(guard);
14  RR.parseEvalQ("cat(\"Hello, world from use()\\n\")");
15  ++i;
16  }
17  int getValue() { return i; }
18 
19 private:
20  int i;
21  RInside & RR; // reference to embedded R instance
22  boost::mutex guard;
23 };
24 
25 void thread_func(Resource& resource) {
26  resource.use();
27 }
28 
29 extern uintptr_t R_CStackLimit;
30 
31 int main(int argc, char *argv[]) {
32 
33  RInside R(argc, argv);
34  R.parseEvalQ("cat(\"Hello, world from main()\\n\")");
35 
36  Resource resource;
37  boost::thread_group thread_group;
38  thread_group.create_thread(boost::bind(thread_func, boost::ref(resource)));
39  thread_group.create_thread(boost::bind(thread_func, boost::ref(resource)));
40  thread_group.join_all();
41  std::cout << "At end value is " << resource.getValue() << std::endl;
42  return 0;
43 }
Resource()
Definition: boostEx.cpp:10
int i
Definition: boostEx.cpp:20
boost::mutex guard
Definition: boostEx.cpp:22
void parseEvalQ(const std::string &line)
Definition: RInside.cpp:366
void use()
Definition: boostEx.cpp:12
int getValue()
Definition: boostEx.cpp:17
RInside & RR
Definition: boostEx.cpp:21
int main(int argc, char *argv[])
Definition: boostEx.cpp:31
void thread_func(Resource &resource)
Definition: boostEx.cpp:25
uintptr_t R_CStackLimit