This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the EGCS project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

vector<bool> oddness



  Hi.  Following is a program that has been striped down to the bones
that demonstrates what I believe is a problem with vector<bool>.  The
program creates two threads each of which load a descent amount of data
into a private vector<vector<bool> >.  These threads then start
executing random_shuffle() on their private vectors which is where the
program fails.  If instead of vector<bool>, vector<unsigned char> is
used everything seems to be fine (more memory is consumed but the
program runs).  I added a a mutex around the call to random_shuffle()
but this did not help.  This program fails with egcs 1.2 and gcc 2.95 
on a Slackware Linux 4.0 system.  Is the problem with my code or
the compiler?

  Thanks.


#include <vector>
#include <algorithm>

#include <pthread.h>

static pthread_mutex_t mutex_random = PTHREAD_MUTEX_INITIALIZER;

//----------------------------------------------------------------------

typedef vector<bool> pattern_t;
typedef vector<pattern_t> patterns_t;

//----------------------------------------------------------------------

void
train(patterns_t &Xi) {
  while(1) {
    pthread_mutex_lock(&mutex_random);
    random_shuffle(Xi.begin(), Xi.end());
    pthread_mutex_unlock(&mutex_random);
  }
}

//----------------------------------------------------------------------

bool
load_data(patterns_t &Xi) {
  Xi.reserve(58143);
  for(int k=0; k<58143; k++) {
    pattern_t tmp;
    tmp.resize(273);
    for(int i=0; i<273; i++) {
	tmp[i] =  true;
    }

    Xi.push_back(tmp);
  }

  return true;
}

//----------------------------------------------------------------------

void *
thread_start(void *varg) {
  patterns_t *Xi = (patterns_t *)varg;
  train(*Xi);
  return NULL;
}

//----------------------------------------------------------------------

int
main(int argc, char *argv[]) {
  const char n_threads = 2;
  pthread_t threads[n_threads];
  
  for(int i=0; i<n_threads; i++) {
    patterns_t *Xi = new patterns_t;
    
    load_data(*Xi);

    pthread_create(&threads[i], NULL, thread_start, Xi);
  }

  for(int i=0; i<n_threads; i++) {
    pthread_join(threads[i], NULL);
  }
  
  return 0;
}

//----------------------------------------------------------------------

---
Kent Vander Velden
kent@iastate.edu


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]