[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Thread testprog segfaulter?!!



hej!

jeg sidder lige og prøver at få threads og C++ til at svinge, og jeg
fandt til formålet et testprogram (kildetekst includeret)
men programmet segfaulter på mit RH6.0 system, her er hvad den siger:

Thread id1026: x is now 1
Thread id2051: x is now 2
Thread id1026: x is now 3
Thread id3076: x is now 4
Thread id4101: x is now 5
Thread id2051: x is now 6
Thread id1026: x is now 7
Thread id3076: x is now 8
Thread id4101: x is now 9
Segmentation fault

Det svinger lidt hvor langt x når, men den burde nå op til 4000 og det
gjorde den
så vidt jeg husker også første gang jeg prøvede?
hvad kan der være galt? kildeteksten kompiler uden problemer med
følgende kommando:

gcc -o thread thread.c -lm /usr/lib/libpthreads.a


--
        -Jacob Kolding
        sslug@sslug

/*      The ultimate paranoia, MS and NSA working together      *\


/*
 * Filename:  thread.c
 */

/* You can compile this program with:
 * gcc -Wall -D_REENTRANT -o thread thread.c -lpthread
*/

/* We always need to include this header file for the threads */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

/* this is the prototype for our thread function */
void *mythread(void *data);
/* We must initialize our mutex */
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
/* This will be a shared variable between all of the threads */
int x = 0;
int main(void)
{
 /* This will be an array holding the threads ids for each thread */
 
 /* We keep track of all the tids so that we can call
  * pthread_join() later to retrieve the return value
  * from the thread
 */
 pthread_t *tids;
 int i;
 /* We will now create the 10 threads. Each thread will
  * increment x until x is 4000.
  * On our last argument ot pthread_create we could
  * have passed an argument to the thread function
 */
 for(i=0;i<10;i++)
 { 
    pthread_create(&tids[i], NULL, mythread, NULL);
 }
 /* We will now wait for each thread to terminate */
 for(i=0;i<10;i++)
 {
 /* this will block until the specified thread finishes execution.
  * Our second argument to pthread_join can be a pointer that will have the return
  * value of thread stored in it
 */
    pthread_join(tids[i], NULL);
    printf("Thread id %ld returned\n", tids[i]);
 }
 return(1);
}

/* This is our actual thread function */
void *mythread(void *data)
{
 while(x<4000)
 {
    /* We will try to lock the mutex. If another thread already has it locked, we
     * will block until it is available again. After you first run this program,
     * you should comment out the lock/unlock lines in this function so that you
     * can see why you need mutexes.
    */
    pthread_mutex_lock(&count_mutex);
    x++;
    /* We will have it print out the thread ID and the value of x */
    printf("Thread id%ld: x is now %d\n",pthread_self(), x);
    /* We will now release the mutex so that another thread gets the chance to run */
    pthread_mutex_unlock(&count_mutex);
 }
 /* We can return a pointer. Whatever pointer we return can later be retrieved using
  * the pthread_join function
 */
 pthread_exit(NULL);
}


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:07 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *