* threads
@ 2005-11-06 6:17 Fabio Andres Miranda
2005-11-06 8:41 ` threads Steve Graegert
0 siblings, 1 reply; 2+ messages in thread
From: Fabio Andres Miranda @ 2005-11-06 6:17 UTC (permalink / raw)
To: linux-c-programming
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
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: threads
2005-11-06 6:17 threads Fabio Andres Miranda
@ 2005-11-06 8:41 ` Steve Graegert
0 siblings, 0 replies; 2+ messages in thread
From: Steve Graegert @ 2005-11-06 8:41 UTC (permalink / raw)
To: Fabio Andres Miranda; +Cc: linux-c-programming
On 11/6/05, Fabio Andres Miranda <fabiomiranda@racsa.co.cr> wrote:
> 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");
> }
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2005-11-06 8:41 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-11-06 6:17 threads Fabio Andres Miranda
2005-11-06 8:41 ` threads Steve Graegert
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).