From: Steve Graegert <graegerts@gmail.com>
To: Fabio Andres Miranda <fabiomiranda@racsa.co.cr>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: threads
Date: Sun, 6 Nov 2005 09:41:18 +0100 [thread overview]
Message-ID: <6a00c8d50511060041o4ddfd20fj41a088ed1037922d@mail.gmail.com> (raw)
In-Reply-To: <436D9FF8.1000902@racsa.co.cr>
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
next prev parent reply other threads:[~2005-11-06 8:41 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-11-06 6:17 threads Fabio Andres Miranda
2005-11-06 8:41 ` Steve Graegert [this message]
-- strict thread matches above, loose matches on Subject: below --
2003-12-26 15:54 threads Albert Cahalan
2003-12-29 15:37 ` threads Stephen Smalley
2004-01-21 1:11 ` threads Albert Cahalan
2004-01-21 5:09 ` threads Russell Coker
2004-01-21 13:47 ` threads Stephen Smalley
2001-03-06 23:55 Ying Chen
2001-03-07 0:07 ` threads J . A . Magallon
2000-11-10 15:03 threads M.Kiran Babu
2000-11-10 15:39 ` threads Matti Aarnio
2000-11-10 16:05 ` threads Reto Baettig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6a00c8d50511060041o4ddfd20fj41a088ed1037922d@mail.gmail.com \
--to=graegerts@gmail.com \
--cc=fabiomiranda@racsa.co.cr \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.