ctor-9.C 990 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // { dg-do run }
  2. // { dg-require-effective-target tls_runtime }
  3. #include <omp.h>
  4. #include <assert.h>
  5. #define N 10
  6. #define THR 4
  7. struct B
  8. {
  9. B& operator=(const B &);
  10. };
  11. static B *base;
  12. static B *threadbase;
  13. static int singlethread;
  14. #pragma omp threadprivate(threadbase)
  15. static unsigned cmask[THR];
  16. B& B::operator= (const B &b)
  17. {
  18. unsigned sindex = &b - base;
  19. unsigned tindex = this - threadbase;
  20. assert(sindex < N);
  21. assert(sindex == tindex);
  22. cmask[omp_get_thread_num ()] |= 1u << tindex;
  23. return *this;
  24. }
  25. void foo()
  26. {
  27. #pragma omp parallel
  28. {
  29. B b[N];
  30. threadbase = b;
  31. #pragma omp single copyprivate(b)
  32. {
  33. assert(omp_get_num_threads () == THR);
  34. singlethread = omp_get_thread_num ();
  35. base = b;
  36. }
  37. }
  38. }
  39. int main()
  40. {
  41. omp_set_dynamic (0);
  42. omp_set_num_threads (THR);
  43. foo();
  44. for (int i = 0; i < THR; ++i)
  45. if (i == singlethread)
  46. assert(cmask[singlethread] == 0);
  47. else
  48. assert(cmask[i] == (1u << N) - 1);
  49. return 0;
  50. }