OSDN Git Service

57411eef3f0ee5a4cd2816790d0ec736b2076cbe
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 23_containers / list / pthread5.cc
1 // 2002-01-23  Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
2 // Adpated from libstdc++/5464 submitted by jjessel@amadeus.net
3 // Jean-Francois JESSEL (Amadeus SAS Development) 
4 //
5 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009
6 // Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library.  This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3.  If not see
21 // <http://www.gnu.org/licenses/>.
22
23 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
24 // { dg-options "-pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
25 // { dg-options "-pthreads" { target *-*-solaris* } }
26
27 #include <vector>
28 #include <list>
29 #include <string>
30 #include <cstdlib>
31 #include <pthread.h>
32
33 #ifdef _GLIBCXX_HAVE_UNISTD_H
34 #include <unistd.h>     // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
35 #endif
36
37 #define NTHREADS 8
38 #define LOOPS 20
39
40 struct tt_t
41 {
42   char buf[100];
43   int  i;
44 };
45
46 void*
47 thread_function (void* arg)
48 {
49   typedef std::vector<tt_t>             vector_type;
50   typedef std::list<std::string*>       list_type;
51
52   int myid __attribute__((unused)) = *(int*) arg;
53   for (int i = 0; i < LOOPS; i++)
54     {
55       vector_type myvect1;
56
57       for (int j = 0; j < 2000; j++)
58         {
59           vector_type myvect2;
60           tt_t v;
61           v.i = j;
62           myvect1.push_back (v);
63           myvect2.push_back (v);
64           list_type mylist;
65           std::string string_array[4];
66           string_array[0] = "toto";
67           string_array[1] = "titi";
68           string_array[2] = "tata";
69           string_array[3] = "tutu";
70           for (int k = 0; k < 4; k++)
71             {
72               if (mylist.size ())
73                 {
74                   list_type::iterator aIt;
75                   for (aIt = mylist.begin (); aIt != mylist.end (); ++aIt)
76                     {
77                       if ((*aIt) == &(string_array[k]))
78                         abort ();
79                     }
80                 }
81               mylist.push_back (&(string_array[k]));
82             }
83         }
84     }
85
86   return arg;
87 }
88
89 int
90 main ()
91 {
92   int worker;
93   pthread_t threads[NTHREADS];
94   int ids[NTHREADS];
95   void* status;
96
97 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
98   pthread_setconcurrency (NTHREADS);
99 #endif
100
101   pthread_attr_t tattr;
102   int ret = pthread_attr_init (&tattr);
103 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
104   ret = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
105 #endif
106
107   for (worker = 0; worker < NTHREADS; worker++)
108     {
109       ids[worker] = worker;
110       if (pthread_create(&threads[worker], &tattr,
111                          thread_function, &ids[worker]))
112         abort ();
113     }
114
115   for (worker = 0; worker < NTHREADS; worker++)
116     {
117       if (pthread_join(threads[worker], static_cast<void **>(&status)))
118         abort ();
119
120       if (*((int *)status) != worker)
121         abort ();
122     }
123
124   return (0);
125 }