xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/testsuite/thread/pthread5.cc (revision ae44bc93cdc3e99800494195f6559488456acd2d)
103a78d15Sespie // 2002-01-23  Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
203a78d15Sespie // Adpated from libstdc++/5464 submitted by jjessel@amadeus.net
303a78d15Sespie // Jean-Francois JESSEL (Amadeus SAS Development)
403a78d15Sespie //
503a78d15Sespie // Copyright (C) 2002, 2003 Free Software Foundation, Inc.
603a78d15Sespie //
703a78d15Sespie // This file is part of the GNU ISO C++ Library.  This library is free
803a78d15Sespie // software; you can redistribute it and/or modify it under the
903a78d15Sespie // terms of the GNU General Public License as published by the
1003a78d15Sespie // Free Software Foundation; either version 2, or (at your option)
1103a78d15Sespie // any later version.
1203a78d15Sespie //
1303a78d15Sespie // This library is distributed in the hope that it will be useful,
1403a78d15Sespie // but WITHOUT ANY WARRANTY; without even the implied warranty of
1503a78d15Sespie // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1603a78d15Sespie // GNU General Public License for more details.
1703a78d15Sespie //
1803a78d15Sespie // You should have received a copy of the GNU General Public License along
1903a78d15Sespie // with this library; see the file COPYING.  If not, write to the Free
2003a78d15Sespie // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
2103a78d15Sespie // USA.
2203a78d15Sespie 
23*ae44bc93Skurt // { dg-do run { target *-*-openbsd* *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* } }
24*ae44bc93Skurt // { dg-options "-pthread" { target *-*-openbsd* *-*-freebsd* *-*-netbsd* *-*-linux* } }
2503a78d15Sespie // { dg-options "-pthreads" { target *-*-solaris* } }
2603a78d15Sespie 
2703a78d15Sespie #include <vector>
2803a78d15Sespie #include <list>
2903a78d15Sespie #include <string>
3003a78d15Sespie 
3103a78d15Sespie // Do not include <pthread.h> explicitly; if threads are properly
3203a78d15Sespie // configured for the port, then it is picked up free from STL headers.
3303a78d15Sespie 
3403a78d15Sespie #if __GTHREADS
3503a78d15Sespie #ifdef _GLIBCPP_HAVE_UNISTD_H
3603a78d15Sespie #include <unistd.h>	// To test for _POSIX_THREAD_PRIORITY_SCHEDULING
3703a78d15Sespie #endif
3803a78d15Sespie 
3903a78d15Sespie using namespace std;
4003a78d15Sespie 
4103a78d15Sespie #define NTHREADS 8
4203a78d15Sespie #define LOOPS 20
4303a78d15Sespie 
4403a78d15Sespie struct tt_t
4503a78d15Sespie {
4603a78d15Sespie   char buf[100];
4703a78d15Sespie   int  i;
4803a78d15Sespie };
4903a78d15Sespie 
5003a78d15Sespie void*
thread_function(void * arg)5103a78d15Sespie thread_function (void* arg)
5203a78d15Sespie {
5303a78d15Sespie   int myid = *(int*) arg;
5403a78d15Sespie   for (int i = 0; i < LOOPS; i++)
5503a78d15Sespie     {
5603a78d15Sespie       vector<tt_t> myvect1;
5703a78d15Sespie 
5803a78d15Sespie       for (int j = 0; j < 2000; j++)
5903a78d15Sespie 	{
6003a78d15Sespie 	  vector<tt_t> myvect2;
6103a78d15Sespie 	  tt_t v;
6203a78d15Sespie 	  v.i = j;
6303a78d15Sespie 	  myvect1.push_back (v);
6403a78d15Sespie 	  myvect2.push_back (v);
6503a78d15Sespie 	  list<std::string *> mylist;
6603a78d15Sespie 	  std::string string_array[4];
6703a78d15Sespie 	  string_array[0] = "toto";
6803a78d15Sespie 	  string_array[1] = "titi";
6903a78d15Sespie 	  string_array[2] = "tata";
7003a78d15Sespie 	  string_array[3] = "tutu";
7103a78d15Sespie 	  for (int k = 0; k < 4; k++)
7203a78d15Sespie 	    {
7303a78d15Sespie 	      if (mylist.size ())
7403a78d15Sespie 		{
7503a78d15Sespie 		  list<std::string *>::iterator aIt;
7603a78d15Sespie 		  for (aIt = mylist.begin (); aIt != mylist.end (); ++aIt)
7703a78d15Sespie 		    {
7803a78d15Sespie 		      if ((*aIt) == &(string_array[k]))
7903a78d15Sespie 			abort ();
8003a78d15Sespie 		    }
8103a78d15Sespie 		}
8203a78d15Sespie 	      mylist.push_back (&(string_array[k]));
8303a78d15Sespie 	    }
8403a78d15Sespie 	}
8503a78d15Sespie     }
8603a78d15Sespie 
8703a78d15Sespie   return arg;
8803a78d15Sespie }
8903a78d15Sespie 
9003a78d15Sespie int
main(int argc,char * argv[])9103a78d15Sespie main (int argc, char *argv[])
9203a78d15Sespie {
9303a78d15Sespie   int worker;
9403a78d15Sespie   pthread_t threads[NTHREADS];
9503a78d15Sespie   int ids[NTHREADS];
9603a78d15Sespie   void* status;
9703a78d15Sespie 
9803a78d15Sespie #if defined(__sun) && defined(__svr4__)
9903a78d15Sespie   pthread_setconcurrency (NTHREADS);
10003a78d15Sespie #endif
10103a78d15Sespie 
10203a78d15Sespie   pthread_attr_t tattr;
10303a78d15Sespie   int ret = pthread_attr_init (&tattr);
10403a78d15Sespie #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
10503a78d15Sespie   ret = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
10603a78d15Sespie #endif
10703a78d15Sespie 
10803a78d15Sespie   for (worker = 0; worker < NTHREADS; worker++)
10903a78d15Sespie     {
11003a78d15Sespie       ids[worker] = worker;
11103a78d15Sespie       if (pthread_create(&threads[worker], &tattr,
11203a78d15Sespie 			 thread_function, &ids[worker]))
11303a78d15Sespie 	abort ();
11403a78d15Sespie     }
11503a78d15Sespie 
11603a78d15Sespie   for (worker = 0; worker < NTHREADS; worker++)
11703a78d15Sespie     {
11803a78d15Sespie       if (pthread_join(threads[worker], static_cast<void **>(&status)))
11903a78d15Sespie 	abort ();
12003a78d15Sespie 
12103a78d15Sespie       if (*((int *)status) != worker)
12203a78d15Sespie 	abort ();
12303a78d15Sespie     }
12403a78d15Sespie 
12503a78d15Sespie   return (0);
12603a78d15Sespie }
12703a78d15Sespie #else
main(void)12803a78d15Sespie int main (void) {}
12903a78d15Sespie #endif
130