目录






Linux内核不提供线程,由线程库来实现。
#include
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*routine)(void *), void *arg);
成功返回0,失败时返回错误码
thread 线程对象
attr 线程属性,NULL代表默认属性
routine 线程执行的函数
arg 传递给routine的参数 ,参数是void * ,注意传递参数格式,
注意事项:1. 主进程的退出,它创建的线程也会退出。
线程创建需要时间,如果主进程马上退出,那线程不能得到执行
#include
void pthread_exit(void *retval);
结束当前线程
retval可被其他线程通过pthread_join获取
线程私有资源被释放
获取线程的id
通过pthread_create函数的第一个参数;通过在线程里面调用pthread_self函数
createP_t.c:8:5: error: invalid use of void expression
printf("input arg=%d\n",(int)*arg);
-
- #if 1
- #include
- #include
- #include
-
- void *testThread(void *arg){
- printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
- // return NULL;
- printf("input arg=%d\n",*(int*)arg);//(int)arg
- pthread_exit(NULL);
- printf("after pthread exit\n");
- }
- int main(){
- pthread_t tid;
- int ret;
- int arg = 5;
-
- ret = pthread_create(&tid,NULL,testThread,(void *)&arg);//(void*)arg
-
- printf("This is main thread,tid=%lu\n",tid);
- sleep(1);
- }
- #endif
- #if 0
- #include
- #include
- #include
-
- int *testThread(char *arg){
- printf("This is a thread test pid = %d tid = %lu\n",getpid(),pthread_self());
- pthread_exit(NULL);
- printf("after pthread exit");
- }
-
- int main(){
- pthread_t tid;
- int ret;
- ret = pthread_create(&tid,NULL,(void*)testThread,NULL);
- printf("This is main Thread pid = %d tid = %lu\n",getpid(),tid);
- sleep(1);
-
- }
-
- #endif

-
- #if 1
- #include
- #include
- #include
-
- void *testThread(void *arg){
- printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
- // return NULL;
- printf("input arg=%d\n",*(int*)arg);//(int)arg
- pthread_exit(NULL);
- printf("after pthread exit\n");
- }
- int main(){
- pthread_t tid[5];
- int ret,i;
- int arg = 5;
-
-
- for(i = 0;i < 5;i++){
- ret = pthread_create(&tid[i],NULL,testThread,(void *)&i);//(void*)arg
- printf("This is main thread,tid=%lu\n",tid[i]);
- }
- sleep(1);
- }
- #endif

#include
int pthread_join(pthread_t thread, void **retval);

注意:pthread_join 是阻塞函数,如果回收的线程没有结束,则一直等待
- #include
- #include
- #include
- void *func(void *arg){
- printf("This is child thread\n");
- //sleep(1);
- sleep(5);
- pthread_exit("thread return");
-
- }
-
- #if 0
- int main()
- {
- pthread_t tid;
- void *retv;
- pthread_create(&tid,NULL,func,NULL);
- pthread_join(tid,&retv);
- printf("thread ret=%s\n",(char*)retv);
- sleep(1);
- }
-
-
- #endif
-
-
- #if 1
- int main(){
- pthread_t tid[5];
- void *retv;
- int i;
- for(i=0;i<5;i++){
- pthread_create(&tid[i],NULL,func,NULL);
- }
- for(i=0;i<5;i++){
- pthread_join(tid[i],&retv);
- printf("thread ret=%s\n",(char*)retv);
- }
- //while(1){
- sleep(1);
- //}
-
- }
-
- #endif
-
-
- #if 0
- int main(){
- pthread_t tid[100];
- void *retv;
- int i;
- for(i=0;i<100;i++){
- pthread_create(&tid[i],NULL,func,NULL);
- }
- for(i=0;i<100;i++){
- // pthread_join(tid[i],&retv);
- // printf("thread ret=%s\n",(char*)retv);
- }
- while(1){
- sleep(1);
- }
-
- }
-
- #endif
-



程序:
- #include
- #include
- #include
- void *func(void *arg){
- pthread_detach(pthread_self());
- printf("This is child thread\n");
- sleep(5);
- pthread_exit("thread return");
-
- }
-
-
- int main(){
- pthread_t tid[5];
- void *retv;
- int i;
- for(i=0;i<5;i++){
- pthread_create(&tid[i],NULL,func,NULL);
- //pthread_detach(tid[i]);
- }
-
- while(1){
- sleep(1);
- }
-
- }
结果:

- #include
- #include
- #include
- void *func(void *arg){
- printf("This is child thread\n");
- sleep(5);
- pthread_exit("thread return");
-
- }
-
-
- int main(){
- pthread_t tid[5];
- void *retv;
- int i;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
-
- for(i=0;i<5;i++){
- pthread_create(&tid[i],&attr,func,NULL);
- // pthread_detach(tid);
- }
-
- while(1){
- sleep(1);
- }
-
- }

使用线程的分离:
两种方式:
1 使用pthread_detach
2 创建线程时候设置为分离属性
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
验证实验:
把创建线程增加到100个
通过join、detach回收可以明显看出内存的减少,不用则会不减反增。