ralf@uni-koblenz.de writes:
> On Mon, Apr 27, 1998 at 07:37:42PM -0400, Dong Liu wrote:
>
> > Sorry, I didn't install it properly, now my program links, but it
> > still give segementation fault.
>
> Ok. I have to admit that I never tested any kind of multithread application.
> If you could provide the code for the program in question, that'd be
> helpful.
>
> Ralf
>
How about this
---- create-thread.c----
#include <pthread.h>
#include <stdio.h>
void* new_thread(void* arg)
{
int i;
printf("Thread[%s] stack at %x\n",arg, &i);
for (i = 0; i< 4; i++) {
printf("Thread[%s] %d\n", arg, i);
sched_yield();
}
return(NULL);
}
#define NUM_OF_THREAD 100
main(int argc, char **argv)
{
int num = NUM_OF_THREAD;
pthread_t thread[NUM_OF_THREAD];
char *args[NUM_OF_THREAD];
int i;
int last;
void *status;
if (argc > 1)
num = atoi(argv[1]);
if (num>100)
num = 100;
printf("Original thread stack at %x\n", &i);
for (i = 0 ; i < num; i++) {
args[i] = (char *)malloc(80);
sprintf(args[i], "%04d", i);
if (pthread_create(&thread[i],
NULL,
new_thread, (void *)args[i])) {
printf("Error: creating new thread[%d]\n", i);
break;
}
}
last = i;
for (i = 0 ; i < last; i++) {
pthread_join(thread[i], &status);
printf("thread[%d] return status' address %p\n",i, status);
}
printf("%d threads created\n", last);
exit(0);
}
|