File: TestUIDGenerator2.cxx

package info (click to toggle)
gdcm 3.0.24-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 27,560 kB
  • sloc: cpp: 203,722; ansic: 76,471; xml: 48,131; python: 3,473; cs: 2,308; java: 1,629; lex: 1,290; sh: 334; php: 128; makefile: 97
file content (83 lines) | stat: -rw-r--r-- 2,334 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*=========================================================================

  Program: GDCM (Grassroots DICOM). A DICOM library

  Copyright (c) 2006-2011 Mathieu Malaterre
  All rights reserved.
  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "gdcmUIDGenerator.h"

#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <iterator>

#include <pthread.h>

const unsigned int nuids = 100;

void* func (void* argc)
{
  gdcm::UIDGenerator g;
  std::set<std::string> *uids= reinterpret_cast< std::set<std::string>* >(argc);
  for(unsigned int i = 0; i < nuids; i++)
    {
    const char *s = g.Generate();
    //std::cout << s << std::endl;
    if ( uids->count(s) == 1 )
      {
      std::cerr << "Already found: " << s << std::endl;
      //pthread_exit(); // How do I say this is an error...
      }
    uids->insert( s );
   }
  return nullptr;
}

int TestUIDGenerator2(int , char *[])
{
  const unsigned int nthreads = 10; // multiple of 2 please
  pthread_t th[nthreads];
  std::set<std::string> uids[nthreads];
  unsigned int i;
  for (i = 0; i < nthreads; i++)
    {
    const int ret = pthread_create (&th[i], nullptr, func, (void*)(uids+i));
    if( ret ) return 1;
    }
  for (i = 0; i < nthreads; i++)
    pthread_join (th[i], nullptr);

  std::vector<std::string> v_one(nuids*nthreads);
  std::vector<std::string>::iterator it = v_one.begin();
  for(i = 0; i < nthreads; i+=2)
    {
    std::set_union(uids[i].begin(), uids[i].end(),
      uids[i+1].begin(), uids[i+1].end(), it);
    it += nuids*2;
    }
  std::cout << v_one.size() << std::endl;
  assert( v_one.size() == nuids * nthreads ); // programmer error

  std::copy(v_one.begin(), v_one.end(), std::ostream_iterator<std::string>(std::cout, "\n"));

  std::set<std::string> global;
  for(it = v_one.begin(); it != v_one.end(); ++it)
    {
    global.insert( *it );
    }
  std::cout << "set:" << global.size() << std::endl;
  if( global.size() != nuids * nthreads )
    {
    return 1;
    }

  return 0;
}