Subj : threads do not get cpa To : comp.programming.threads From : azb123 Date : Sun Aug 07 2005 04:09 am Hello, I've tried the code posted below on an SGI Origin (16 cpus) and a SGI Altix (32 cpus). The program runs and gives the expected results, but it always gets less cpus than active threads (on an otherwise empty machine). If three threads are requested, top shows about 200% activity, if 6 threads are requested, the activity goes up to 400%. I do not understand why the activity reported by top is less than the number of active threads. I would expect top reporting an activity of 300% for three active threads. BTW, I have exeprimented with changing the thread's scope and scheduling policy, but could not really change the above behaviour. Any ideas what is happening? Thanks, Markus <<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include #include #include #include typedef struct { int id; int called; int nqueries; } THREAD_DATA; int CurrentQuery; pthread_mutex_t CurrentQueryMutex; void* search_thread(void *arg) { int i, query, nqueries, id; double sum; nqueries = ((THREAD_DATA*)arg)->nqueries; id = ((THREAD_DATA*)arg)->id; while(1) { pthread_mutex_lock(&CurrentQueryMutex); query = CurrentQuery++; pthread_mutex_unlock(&CurrentQueryMutex); if (query >= nqueries) pthread_exit(NULL); fprintf(stderr,"Thread %d doing query %d\n", id, query); ((THREAD_DATA*)arg)->called++; /* do some work */ for (i=0, sum=0.0; i<1000000; i++) { sum += exp(drand48()); sum -= exp(drand48()); } fprintf(stderr,"Thread %d has result %f\n",id,sum); } } int main(int argc, char *argv[]) { int i, r, nthreads, nqueries = 100; pthread_t *threads; pthread_attr_t attr; THREAD_DATA *thread_data; nthreads = atoi(argv[1]); fprintf(stderr, "Starting %d threads.\n",nthreads); threads = malloc(nthreads * sizeof(*threads)); thread_data = malloc(nthreads * sizeof(*thread_data)); if (!threads || !thread_data) { fprintf(stderr, " Could not allocate memory!\n"); exit(EXIT_FAILURE); } pthread_attr_init(&attr); if ((r=pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE))) fprintf(stderr,"Could not set detach state: %d!\n",r); pthread_mutex_init(&CurrentQueryMutex, NULL); CurrentQuery = 0; for (i=0; i