[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
 

Mærkeligt problem med threads



Hej,

Jeg sidder og leger med tråde i C (GCC 2.95, 2.4.18), men der er noget som virkelig undrer mig. Jeg har forsøgt at lede på google for at se om 2.95 af gcc er bugget, men jeg har ikke fundet noget som relaterede til mit problem. Jeg håber I måske har oplevet noget lignende. Jeg paster lidt kode, men jeg håber I overlever. Jeg har skåret alt væk i et forsøg på at debugge.

Jeg starter mine tråde således:

...

 pthread_t threads[NUM_THREADS];
 int i;

 for( i=0 ; i<NUM_THREADS; i++ ) 
 {
  printf("CREATING THREAD: %d\n", i);
  pthread_create(&threads[i], NULL, fetch_start, (void*)&i);
 }

 for( i=0 ; i<NUM_THREADS ; i++ ) 
  pthread_join(threads[i], NULL);

...

Min fetch_start() ser således ud:

void *fetch_start(void *arg)
{
 int *thread_id, status=0;

 thread_id = (int*)arg;
 printf("THREAD %d STARTED...\n", *thread_id);
  
 printf("THREAD %d EXITING...\n", *thread_id);
 pthread_exit(&status);
 return NULL;
}

Umiddelbart burde det jo virke, men outputtet er meget underligt.

CREATING THREAD: 0
CREATING THREAD: 1
THREAD 1 STARTING...
THREAD 1 EXITING...
CREATING THREAD: 2
THREAD 2 STARTING...
THREAD 2 EXITING...
CREATING THREAD: 3
THREAD 3 STARTING...
THREAD 3 EXITING...
THREAD 3 STARTING...
THREAD 3 EXITING...

Hvor er min tråd 0 blevet af? Hvorfor bliver tråd 3 startet to gange? Så vidt jeg kan regne ud, så burde min thread_id altid være korrekt, da jeg giver det tal videre fra hvor jeg starter trådene.

et andet eksempel:

CREATING THREAD: 0
THREAD 0 STARTING...
THREAD 0 EXITING...
CREATING THREAD: 1
CREATING THREAD: 2
CREATING THREAD: 3
THREAD 3 STARTING...
THREAD 3 EXITING...
THREAD 3 STARTING...
THREAD 3 EXITING...
THREAD 3 STARTING...
THREAD 3 EXITING...

her er tråd 1+2 forsvundet helt? Outputtet er forskelligt fra gang til gang. Det er i sig selv jo til at forstå, fordi jeg ikke kan være garanteret at trådene bliver startet i samme rækkefølge hver gang. Jeg forstår dog ikke hvorfor mit tread_id kan ændre sig.

Jeg er totalt lost. Er der noget jeg overser? :-)

Mvh
Thomas








 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 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] *