File: boostEx.cpp

package info (click to toggle)
r-cran-rinside 0.2.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 668 kB
  • sloc: cpp: 3,310; ansic: 117; xml: 57; ruby: 34; makefile: 2
file content (43 lines) | stat: -rw-r--r-- 1,031 bytes parent folder | download | duplicates (5)
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
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; -*-

#include <boost/thread.hpp>
#include <boost/bind.hpp>

#include <RInside.h>

class Resource {
public:
    Resource(): i(0), RR(RInside::instance()) { }

    void use() {
	boost::mutex::scoped_lock lock(guard);
	RR.parseEvalQ("cat(\"Hello, world from use()\\n\")");
	++i;
    }
    int getValue() { return i; }

private:
    int i;
    RInside & RR;		// reference to embedded R instance
    boost::mutex guard;
};

void thread_func(Resource& resource) {
    resource.use();
}

extern uintptr_t R_CStackLimit;

int main(int argc, char *argv[]) {

    RInside R(argc, argv);
    R.parseEvalQ("cat(\"Hello, world from main()\\n\")");

    Resource resource;
    boost::thread_group thread_group;
    thread_group.create_thread(boost::bind(thread_func, boost::ref(resource)));
    thread_group.create_thread(boost::bind(thread_func, boost::ref(resource)));
    thread_group.join_all();
    std::cout << "At end value is " << resource.getValue() << std::endl;
    return 0;
}