#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
struct tag_thrattr
{
int ID;
char name[128];
};
void *func(void *arg)
{
struct tag_thrattr *p = (struct tag_thrattr *) arg;
int id = p->ID;
char s[128];
strcpy(s, p->name);
free(p);
printf("pthread start%d\n", id);
printf("ID=%d\n", id);
printf("name=%s\n", s);
printf("pthread end%d\n", id);
return NULL;
}
int main(void)
{
printf("start\n");
pthread_t thr_d;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int i;
for (i = 0; i < 10; i++)
{
struct tag_thrattr *thrattr = malloc(sizeof(struct tag_thrattr));
thrattr->ID = i;
sprintf(thrattr->name, "thread%d", i);
pthread_create(&thr_d, &attr, func, thrattr);
}
pthread_attr_destroy(&attr);
sleep(6);
printf("end\n");
return EXIT_SUCCESS;
}
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
struct tag_thrattr
{
int ID;
char name[128];
};
void *func(void *arg)
{
struct tag_thrattr *p = (struct tag_thrattr *) arg;
int id = p->ID;
char s[128];
strcpy(s, p->name);
free(p);
printf("pthread start%d\n", id);
printf("ID=%d\n", id);
printf("name=%s\n", s);
printf("pthread end%d\n", id);
return NULL;
}
int main(void)
{
printf("start\n");
pthread_t thr_d;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int i;
for (i = 0; i < 10; i++)
{
struct tag_thrattr *thrattr = malloc(sizeof(struct tag_thrattr));
thrattr->ID = i;
sprintf(thrattr->name, "thread%d", i);
pthread_create(&thr_d, &attr, func, thrattr);
}
pthread_attr_destroy(&attr);
sleep(6);
printf("end\n");
return EXIT_SUCCESS;
}