OSDN Git Service

fcd491d967ca1556bb6535bbd12e89ab0c2f2ecf
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / thread / 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 Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING.  If not, write to the Free
20 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // USA.
22
23 // { dg-do run { target *-*-freebsd* *-*-linux* *-*-solaris* } }
24 // { dg-options "-pthread" { target *-*-freebsd* *-*-linux* } }
25 // { dg-options "-pthreads" { target *-*-solaris* } }
26
27 #include <vector>
28 #include <list>
29 #include <string>
30
31 // Do not include <pthread.h> explicitly; if threads are properly
32 // configured for the port, then it is picked up free from STL headers.
33
34 #if __GTHREADS
35 using namespace std;
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   int myid = *(int*) arg;
50   for (int i = 0; i < LOOPS; i++)
51     {
52       vector<tt_t> myvect1;
53
54       for (int j = 0; j < 2000; j++)
55         {
56           vector<tt_t> myvect2;
57           tt_t v;
58           v.i = j;
59           myvect1.push_back (v);
60           myvect2.push_back (v);
61           list<std::string *> mylist;
62           std::string string_array[4];
63           string_array[0] = "toto";
64           string_array[1] = "titi";
65           string_array[2] = "tata";
66           string_array[3] = "tutu";
67           for (int k = 0; k < 4; k++)
68             {
69               if (mylist.size ())
70                 {
71                   list<std::string *>::iterator aIt;
72                   for (aIt = mylist.begin (); aIt != mylist.end (); ++aIt)
73                     {
74                       if ((*aIt) == &(string_array[k]))
75                         abort ();
76                     }
77                 }
78               mylist.push_back (&(string_array[k]));
79             }
80         }
81     }
82
83   return arg;
84 }
85
86 int
87 main (int argc, char *argv[])
88 {
89   int worker;
90   pthread_t threads[NTHREADS];
91   int ids[NTHREADS];
92   void* status;
93
94 #if defined(__sun) && defined(__svr4__)
95   pthread_setconcurrency (NTHREADS);
96 #endif
97
98   pthread_attr_t tattr;
99   int ret = pthread_attr_init (&tattr);
100   ret = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
101
102   for (worker = 0; worker < NTHREADS; worker++)
103     {
104       ids[worker] = worker;
105       if (pthread_create(&threads[worker], &tattr,
106                          thread_function, &ids[worker]))
107         abort ();
108     }
109
110   for (worker = 0; worker < NTHREADS; worker++)
111     {
112       if (pthread_join(threads[worker], static_cast<void **>(&status)))
113         abort ();
114
115       if (*((int *)status) != worker)
116         abort ();
117     }
118
119   return (0);
120 }
121 #else
122 int main (void) {}
123 #endif