Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sthread: Add a way to verify accesses to non-reentrant data structures
[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) __attribute__((weak));
10 extern void sthread_access_end(void* addr, const char* objname, const char* file, int line) __attribute__((weak));
11 }
12
13 #define STHREAD_ACCESS(obj)                                                                                            \
14   for (bool first = sthread_access_begin(static_cast<void*>(obj), #obj, __FILE__, __LINE__) || true; first;            \
15        sthread_access_end(static_cast<void*>(obj), #obj, __FILE__, __LINE__), first = false)
16
17 static void thread_code()
18 {
19   // Add another integer to the vector
20   STHREAD_ACCESS(&v) v.push_back(21);
21 }
22
23 int main()
24 {
25   std::cout << "starting two helpers...\n";
26   std::thread helper1(thread_code);
27   std::thread helper2(thread_code);
28
29   std::cout << "waiting for helpers to finish..." << std::endl;
30   helper1.join();
31   helper2.join();
32
33   // Print out the vector
34   std::cout << "v = { ";
35   for (int n : v)
36     std::cout << n << ", ";
37   std::cout << "}; \n";
38 }