Skip to contentSkip to conversation

threads

DISCUSSION Fabio Andres Miranda 2 participants 2 messages Nov 6, 2005

lore
Share & tools
lore
#0 lore raw

Why these threads are not executed:

main(){
pthread threads_array[25];
pthread_attr_init(&attr);
for (i=0;i<25;i++)
if ( pthread_create(threads_array[i],&attr,pthread_routine,(void

*)i) ){
           perror("pthread_create");
       }

while(1);
}

void
pthread_routine(void *arg){
printf("hello world\n");
}

$./a.out
$

I read somethg about the initial state is suspended, smthg like that, how to execute the thread as created then?

Thanks

fabiolini

#1 lore raw
Quoted 15 lines · Fabio Andres Miranda

Why these threads are not executed:

main(){
    pthread threads_array[25];
    pthread_attr_init(&attr);
    for (i=0;i<25;i++)
        if ( pthread_create(threads_array[i],&attr,pthread_routine,(void
*)i) ){
            perror("pthread_create");
        }
    while(1);
}
Quoted 4 lines

void

pthread_routine(void *arg){
    printf("hello world\n");
}

Fabio,

This code will not even compile. I suppose you did not post the complete program. Anyway, there are at least two points (probably more) broken in your code:

1. You have to provide pthread_create a pointer to the thread_t object:

	pthread_create(&threads_array[i], ...);

2. The thread function needs to be a pointer to a function:

	void *pthread_routine(void *arg);

3. Omit the while statement, since the program will run forever, although all threads have finished.

Try this:

#include <pthread.h>

void *pthread_routine(void *arg);
pthread_attr_t attr;

main(){
pthread_t threads_array[25];
pthread_attr_init(&attr);

int i;
for (i=0;i<25;i++)
if ( pthread_create(&threads_array[i],&attr,pthread_routine,(void

*)i) ) {
           perror("pthread_create");
       }

while(1);
}

void *pthread_routine(void *arg){
printf("hello world\n");
}

\Steve