File: threading_sanity_check.cpp

package info (click to toggle)
scipy 1.16.0-1exp7
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 234,820 kB
  • sloc: cpp: 503,145; python: 344,611; ansic: 195,638; javascript: 89,566; fortran: 56,210; cs: 3,081; f90: 1,150; sh: 848; makefile: 785; pascal: 284; csh: 135; lisp: 134; xml: 56; perl: 51
file content (61 lines) | stat: -rw-r--r-- 1,221 bytes parent folder | download | duplicates (11)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright John Maddock, 2021
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/math/tools/config.hpp>

#if !defined(BOOST_MATH_NO_THREAD_LOCAL_WITH_NON_TRIVIAL_TYPES) && defined(BOOST_HAS_THREADS)

#include <vector>
#include <atomic>
#include <thread>
#include <random>
#include <algorithm>
#include <iostream>

std::atomic<int> counter{ 0 };


void thread_proc()
{
   static thread_local std::vector<double> list;

   std::default_random_engine rnd;
   std::uniform_real_distribution<> dist;

   for (unsigned i = 0; i < 1000; ++i)
   {
      list.push_back(dist(rnd));
      ++counter;
   }
   std::sort(list.begin(), list.end());
}

int main()
{
   std::thread f1(thread_proc);
   std::thread f2(thread_proc);
   std::thread f3(thread_proc);
   std::thread f4(thread_proc);
   std::thread f5(thread_proc);
   std::thread f6(thread_proc);

   f1.join();
   f2.join();
   f3.join();
   f4.join();
   f5.join();
   f6.join();

   std::cout << "Counter value was: " << counter << std::endl;

   return counter - 6000;
}

#else

int main() { return 0; }

#endif