Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libstdc++-v3 / testsuite / thread / pthread5.cc
blob30086d4584d6e9492d1176db99c8aa31d1c54417
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 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.
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.
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.
23 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* } }
24 // { dg-options "-pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* } }
25 // { dg-options "-pthreads" { target *-*-solaris* } }
27 #include <vector>
28 #include <list>
29 #include <string>
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.
34 #ifdef _GLIBCXX_HAVE_UNISTD_H
35 #include <unistd.h> // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
36 #endif
38 using namespace std;
40 #define NTHREADS 8
41 #define LOOPS 20
43 struct tt_t
45 char buf[100];
46 int i;
49 void*
50 thread_function (void* arg)
52 int myid __attribute__((unused)) = *(int*) arg;
53 for (int i = 0; i < LOOPS; i++)
55 vector<tt_t> myvect1;
57 for (int j = 0; j < 2000; j++)
59 vector<tt_t> myvect2;
60 tt_t v;
61 v.i = j;
62 myvect1.push_back (v);
63 myvect2.push_back (v);
64 list<std::string *> 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++)
72 if (mylist.size ())
74 list<std::string *>::iterator aIt;
75 for (aIt = mylist.begin (); aIt != mylist.end (); ++aIt)
77 if ((*aIt) == &(string_array[k]))
78 abort ();
81 mylist.push_back (&(string_array[k]));
86 return arg;
89 #if !__GXX_WEAK__ && _MT_ALLOCATOR_H
90 // Explicitly instantiate for systems with no COMDAT or weak support.
91 template class __gnu_cxx::__mt_alloc<tt_t>;
92 template class __gnu_cxx::__mt_alloc<std::_List_node<std::string*> >;
93 #endif
95 int
96 main ()
98 int worker;
99 pthread_t threads[NTHREADS];
100 int ids[NTHREADS];
101 void* status;
103 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
104 pthread_setconcurrency (NTHREADS);
105 #endif
107 pthread_attr_t tattr;
108 int ret = pthread_attr_init (&tattr);
109 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
110 ret = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
111 #endif
113 for (worker = 0; worker < NTHREADS; worker++)
115 ids[worker] = worker;
116 if (pthread_create(&threads[worker], &tattr,
117 thread_function, &ids[worker]))
118 abort ();
121 for (worker = 0; worker < NTHREADS; worker++)
123 if (pthread_join(threads[worker], static_cast<void **>(&status)))
124 abort ();
126 if (*((int *)status) != worker)
127 abort ();
130 return (0);