[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
 

Re: [CPROG] Thread testprog segfaulter?!!



Jacob Kolding wrote:
> 
> 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;
   ^^^^^^^^^^^^^^^^

Hmm, sjov måde at lave et array på...

>  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);
                     ^^^^^^^^

bluuuurgh.... det må man vist ikke..

Jeg mener det har været rettet før, eksemplet er vist fra LinuxJournal.

Mvh Morten


 
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] *