#include <pthread.h>
int pthread_join(pthread_t th, void *thread_return);
int pthread_tetach(pthread_t th);
#include <stdio.h>
#include <pthread.h>
void assisthread(void *arg)
{
printf("I am helping to do some jobs\n");
sleep(3);
pthread_exit(0);
}
int main(void)
{
pthread_t assistthid;
int status;
pthread_create(&assistthid, NULL, (void *)assisthread, NULL);
pthread_join(assistthid, (void *)&status);
printf("assistthid's exit is caused %d\n", status);
return 0;
}
beyes@linux-beyes:~/C/base> ./jointhread.exe
I am helping to do some jobs
assistthid's exit is caused 0
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *thread_malloc (char *parameter)
{
int *p;
p = (int *)malloc (10 * sizeof(int)); /*申请空间*/
printf ("传进来的参数是 %s\n", parameter);
printf ("该线程ID是 %u\n", pthread_self());
memset (p, 'c', 10);
printf ("刚才申请的空间首地址为: 0x%x\n", p);
pthread_exit (p); //退出线程,返回申请空间首址
}
int main (int argc, char **argv)
{
int error;
int *temptr;
pthread_t thread_id;
/*创建线程*/
pthread_create (&thread_id, NULL, (void *)*thread_malloc, "thread malloc");
/*线程结束时返回值存放在pthread_join()函数的第 2 个参数里*/
if (error = pthread_join (thread_id, (void **)&temptr)) {
perror ("thread_join");
exit (EXIT_FAILURE);
}
printf ("线程返回的指针地址是: 0x%x\n", temptr);
printf ("线程申请的空间没被回收,现在打印出此空间第一个字节的值:%c\n", *temptr);
*temptr = 'd'; //改变堆空间值,测试是否可用
printf ("更改后的值为: %c\n", *temptr);
free (temptr); //释放该堆空间
return (0);
}
./thread_exit_test
传进来的参数是 thread malloc
该线程ID是 3075955568
刚才申请的空间首地址为: 0x804b098
线程返回的指针地址是: 0x804b098
线程申请的空间没被回收,现在打印出此空间第一个字节的值:c
更改后的值为: d
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |