Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use standard __func__ instead of __FUNCTION__.
[simgrid.git] / examples / sthread / stdobject / stdobject.cpp
1 #include <iostream>
2 #include <thread>
3 #include <vector>
4
5 // shared collection object
6 std::vector<int> v = {1, 2, 3, 5, 8, 13};
7
8 extern "C" {
9 extern int sthread_access_begin(void* addr, const char* objname, const char* file, int line, const char* func)
10     __attribute__((weak));
11 extern void sthread_access_end(void* addr, const char* objname, const char* file, int line, const char* func)
12     __attribute__((weak));
13 }
14
15 #define STHREAD_ACCESS(obj)                                                                                            \
16   for (bool first = sthread_access_begin(static_cast<void*>(obj), #obj, __FILE__, __LINE__, __func__) || true; first;  \
17        sthread_access_end(static_cast<void*>(obj), #obj, __FILE__, __LINE__, __func__), first = false)
18
19 static void thread_code()
20 {
21   // Add another integer to the vector
22   STHREAD_ACCESS(&v) v.push_back(21);
23 }
24
25 int main()
26 {
27   std::cout << "starting two helpers...\n";
28   std::thread helper1(thread_code);
29   std::thread helper2(thread_code);
30
31   std::cout << "waiting for helpers to finish..." << std::endl;
32   helper1.join();
33   helper2.join();
34
35   // Print out the vector
36   std::cout << "v = { ";
37   for (int n : v)
38     std::cout << n << ", ";
39   std::cout << "}; \n";
40 }