多线程学习笔记

多线程程序设计


头文件:pthread.h
连接时需要用到的库文件:libpthread.a


创建线程


函数原型
#include <pthread.h>
int pthread_creat(pthread_t *tidp,const pthread_attr_t *attr,void *(*start_rtn)(void),void *arg)


tidp:线程id
attr:线程属性
start_trn:线程要执行的函数
arg: start_rtn的参数


编译
由于pthread库不是linux系统的库,编译时要加上-lpthread
示例代码如下:
gcc filename -lthread


示例代码如下:
thread_create.c 线程的创建
#include <pthread.h>
#include <stdio.h>


void *myThread1(void){
int i;
for(i=0;i<100;i++){
printf("this is the first pthread,created by zieckey.\n");
sleep(1);
}
}


void *myThread2(void){
int i;
for(i=0;i<100;i++){
printf("this is the second pthread,created by zieckey.\n");
sleep(1);
}
}
int main(){
int i=0,ret=0;
pthread_t id1,id2;


/*创建线程1*/
ret=pthread_create(&id1,NULL,(void*)myThread1,NULL);
if(ret){
printf("create pthread error!\n");
return 1;
}


/*创建线程2*/
ret=pthread_create(&id2,NULL,(void*)myThread2,NULL);
if(ret){
printf("create pthread error!\n");
return 1;
}
pthread_join(id1,NULL);
pthread_join(id2,NULL);
return 0;
}


编译执行结果:
[retacn@localhost app]$ gcc thread_create.c -lpthread -o thread_create
[retacn@localhost app]$ ./thread_create
this is the first pthread,created by zieckey.
this is the second pthread,created by zieckey.
this is the first pthread,created by zieckey.
this is the second pthread,created by zieckey.






thread_int.c 传递一个整型参数


#include <stdio.h>
#include <pthread.h>
#include <unistd.h>


void *create(void *arg){
int *num;
num=(int *)arg;
printf("create parameter is %d \n",*num);
return (void *)0;
}
int main(int argc,char *argv[]){
pthread_t tidp;
int error;


int test=4;
int *attr=&test;


error=pthread_create(&tidp,NULL,create,(void *)attr);


if(error){
printf("pthread_create is create is not createed ...\n");
return -1;
}
sleep(1);
printf("pthread_create is created...\n");
return 0;
}


编译执行结果:
[retacn@localhost app]$ gcc thread_int.c -lpthread -o thread_int
[retacn@localhost app]$ ./thread_int
create parameter is 4
pthread_create is created...








thread_string.c
thread_struct.c




thread_share.c
示例代码如下:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>


int a=1;//变量位于数据段中


void *create(void *arg){
printf("new pthread...\n");
printf("a=%d \n",a);
return (void *)0;
}
int main(int argc,char *argv[]){
pthread_t tidp;
int error;


int a=5;//变量位于栈中
printf("main first a=%d \n",a);


error=pthread_create(&tidp,NULL,create,NULL);
if(error!=0){
printf("new thread is not create...\n");
return -1;
}
sleep(1);
printf("main second a=%d\n",a);
printf("new thread is created...\n");
return 0;
}


编译执行结果为:
[retacn@localhost app]$ gcc thread_share.c -lpthread -o thread_share
[retacn@localhost app]$ ./thread_share
main first a=5
new pthread...
a=1
main second a=5
new thread is created...




线程的终止


进程中的任何一个线程调用exit或_exit,整个进程都会终止
线程的正常退出方式有:


线程从启动例程中返回
线程可以被另一个进程终止
线程自已调用pthread_exit函数
函数原型:
#include <pthread.h>
void pthread_exit(void *rval_ptr);
val_ptr:线程退出返回值的指针




示例代码如下:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>


void *create(void *arg){
printf("new thread is create!\n");
return (void *)0;
}
int main(int agrc,char *argv[]){
pthread_t tid;
int error;
void *temp;


error=pthread_create(&tid,NULL,create,NULL);
printf("main thread!\n");


if(error){
printf("thread is not created!\n");
return -1;
}
error=pthread_join(tid,&temp);


if(error){
printf("thread is not exit!\n");
return -2;
}
printf("thread is exit code %d\n",(int)temp);
return 0;
}


编译执行程序结果如下:


[retacn@localhost app]$ gcc thread_exit.c -lpthread -o thread_exit
[retacn@localhost app]$ ./thread_exit
main thread!
new thread is create!
thread is exit code 0




线程等待
阻塞调用线程,直到指定的线程终止
#include <pthread.h>
int pthread_join(pthread_t tid,void **rval_ptr)
tid: 等待退出的线程id
rval_ptr: 线程退出的返回值的指针




示例代码如下:
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>


void *thread(void *str){
int i;
for(i=0;i<10;i++){
sleep(2);
printf("this in the thread :%d\n",i);
}
return NULL;
}


int main(){
pthread_t pth;
int i;
int ret=pthread_create(&pth,NULL,thread,(void *)(i));


pthread_join(pth,NULL);
printf("123\n");
for(i=0;i<10;++i){
sleep(1);
printf("this in the main:%d\n",i);
}
return 0;
}


编译执行结果如下:
[retacn@localhost app]$ gcc thread_join.c -lpthread -o thread_join
[retacn@localhost app]$ ./thread_join
this in the thread :0
this in the thread :1
this in the thread :2
this in the thread :3
this in the thread :4
this in the thread :5
this in the thread :6
this in the thread :7
this in the thread :8
this in the thread :9
123
this in the main:0
this in the main:1
this in the main:2
this in the main:3
this in the main:4
this in the main:5
this in the main:6
this in the main:7
this in the main:8
this in the main:9






线程标识:
取得调用线程的thread identifier
函数原型如下:
#include <pthread.h>
pthread_t pthread_self(void)



示例代码如下:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>


void *create(void *arg){
printf("new thread...\n");
printf("this thread's id is %u\n",(unsigned int)pthread_self());
printf("the process's pid is %d\n",getpid());
return (void *)0;
}
int main(int argc,char *argv[]){
pthread_t tid;
int error;


printf("main thread is starting...\n");
error=pthread_create(&tid,NULL,create,NULL);


if(error){
printf("thread is not created..\n");
return -1;
}
printf("the main process's pid is %d\n",getpid());
sleep(1);
return 0;
}


编译执行结果如下:
[retacn@localhost app]$ gcc thread_id.c -lpthread -o thread_id
[retacn@localhost app]$ ./thread_id
main thread is starting...
the main process's pid is 22475
new thread...
this thread's id is 3086752656
the process's pid is 22475






清除
线程终止有两种方式:
正常终止
非正常终止
以上两种方式终止前都要对占用资源进行释放


从pthread_cleanup_push的调用点到pthread_cleanup_pop之间的程序段中的终止
动作都会执行pthread_cleanup_push()所指定的清理函数
注:return 不会执行pthread_cleanup_push()所指定的清理函数
有点try catch的味道


函数原型
将清除函数压入清除栈
#include <pthread.h>
void pthread_cleanup_push(void (*rtn)(void *),void *arg)
rtn:清除函数
arg:清除函数的参数


将清除函数弹出清除栈
#include <pthread.h>
void pthread_cleanup_pop(int execute)
execute:是否在弹出清理函数的同时执行该函数
非0:执行
0: 不执行




示例代码如下:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>


void *clean(void *arg){
printf("cleanup: %s\n",(char *)arg);
return (void *)0;
}


void *thr_fn1(void *arg){
printf("threan 1 start\n");
pthread_cleanup_push((void *)clean,"thread 1 first handler");
pthread_cleanup_push((void *)clean,"thread 1 second handler");
printf("thread 1 push complete\n");
if(arg){
return ((void *)1);
}
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return (void *)1;
}


void *thr_fn2(void *arg){
printf("thread 2 is start!\n");
pthread_cleanup_push((void *)clean,"thread 2 first handler");
pthread_cleanup_push((void *)clean,"thread 2 second handler");
printf("thread 2 push complete\n");


if(arg){
pthread_exit((void *)2);
}
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
pthread_exit((void *)2);
}




int main(void){
int err;
pthread_t tid1,tid2;
void *tret;


err=pthread_create(&tid1,NULL,thr_fn1,(void *)1);
if(err!=0){
printf("error...\n");
return -1;
}
err=pthread_create(&tid2,NULL,thr_fn2,(void *)1);
if(err!=0){
printf("error...\n");
return -1;
}
err=pthread_join(tid1,&tret);
if(err!=0){
printf("error...\n");
return -1;
}
printf("thread 1 exit code:%d\n",(int)tret);


err=pthread_join(tid2,&tret);
if(err!=0){
printf("error...\n");
return -1;
}
printf("thread 2 exit code:%d\n",(int)tret);


return 0;
}




编译执行结果如下:
[retacn@localhost app]$ gcc thread_clean.c -lpthread -o thread_clean
[retacn@localhost app]$ ./thread_clean
threan 1 start
thread 1 push complete
thread 1 exit code:1
thread 2 is start!
thread 2 push complete
cleanup: thread 2 second handler
cleanup: thread 2 first handler
thread 2 exit code:2

你可能感兴趣的:(多线程)