国土网站建设自查报告,秀米编辑器官网,广州线下培训机构停课,福州网站怎么做seo线程号
就像每个进程都有一个进程号一样#xff0c;每个线程也有一个线程号。进程号在整个系统中是唯一的#xff0c;但线程号不同#xff0c;线程号只在它所属的进程环境中有效。
进程号用 pid_t 数据类型表示#xff0c;是一个非负整数。线程号则用 pthread_t 数据类型…线程号
就像每个进程都有一个进程号一样每个线程也有一个线程号。进程号在整个系统中是唯一的但线程号不同线程号只在它所属的进程环境中有效。
进程号用 pid_t 数据类型表示是一个非负整数。线程号则用 pthread_t 数据类型来表示Linux 使用无符号长整数表示。
有的系统在实现pthread_t 的时候用一个结构体来表示所以在可移植的操作系统实现不能把它做为整数处理。
pthread_self函数 #include pthread.hpthread_t pthread_self(void);功能获取线程号。参数无返回值调用线程的线程 ID 。
线程的创建
pthread_create函数 #include pthread.hint pthread_create(pthread_t *thread,const pthread_attr_t *attr,void *(*start_routine)(void *),void *arg );功能创建一个线程。参数thread线程标识符地址。attr线程属性结构体地址通常设置为 NULL。start_routine线程函数的入口地址。arg传给线程函数的参数。返回值成功0失败非 0
在一个线程中调用pthread_create()创建新的线程后当前线程从pthread_create()返回继续往下执行而新的线程所执行的代码由我们传给pthread_create的函数指针start_routine决定。
由于pthread_create的错误码不保存在errno中因此不能直接用perror()打印错误信息可以先用strerror()把错误码转换成错误信息再打印。 参考程序 // 回调函数void *thread_fun(void * arg){sleep(1);int num *((int *)arg);printf(int the new thread: num %d\n, num);return NULL;}int main(){pthread_t tid;int test 100;// 返回错误号int ret pthread_create(tid, NULL, thread_fun, (void *)test);if (ret ! 0){printf(error number: %d\n, ret);// 根据错误号打印错误信息printf(error information: %s\n, strerror(ret));}while (1);return 0;}
线程资源回收
pthread_join函数 #include pthread.hint pthread_join(pthread_t thread, void **retval);功能等待线程结束此函数会阻塞并回收线程资源类似进程的 wait() 函数。如果线程已经结束那么该函数会立即返回。参数thread被等待的线程号。retval用来存储线程退出状态的指针的地址。返回值成功0失败非 0
参考程序 void *thead(void *arg){static int num 123; //静态变量printf(after 2 seceonds, thread will return\n);sleep(2);return num;}int main(){pthread_t tid;int ret 0;void *value NULL;// 创建线程pthread_create(tid, NULL, thead, NULL);// 等待线程号为 tid 的线程如果此线程结束就回收其资源// value保存线程退出的返回值pthread_join(tid, value);printf(value %d\n, *((int *)value));return 0;}
调用该函数的线程将挂起等待直到id为thread的线程终止。thread线程以不同的方法终止通过pthread_join得到的终止状态是不同的总结如下 如果thread线程通过return返回retval所指向的单元里存放的是thread线程函数的返回值。 如果thread线程被别的线程调用pthread_cancel异常终止掉retval所指向的单元里存放的是常数PTHREAD_CANCELED。 如果thread线程是自己调用pthread_exit终止的retval所指向的单元存放的是传给pthread_exit的参数。
线程分离
一般情况下线程终止后其终止状态一直保留到其它线程调用pthread_join获取它的状态为止。但是线程也可以被置为detach状态这样的线程一旦终止就立刻回收它占用的所有资源而不保留终止状态。
不能对一个已经处于detach状态的线程调用pthread_join这样的调用将返回EINVAL错误。也就是说如果已经对一个线程调用了pthread_detach就不能再调用pthread_join了。
pthread_detach函数 #include pthread.hint pthread_detach(pthread_t thread);功能使调用线程与当前进程分离分离后不代表此线程不依赖与当前进程线程分离的目的是将线程资源的回收工作交由系统自动来完成也就是说当被分离的线程结束之后系统会自动回收它的资源。所以此函数不会阻塞。参数thread线程号。返回值成功0失败非0
线程退出
在进程中我们可以调用exit函数或_exit函数来结束进程在一个线程中我们可以通过以下三种在不终止整个进程的情况下停止它的控制流。 线程从执行函数中返回。 线程调用pthread_exit退出线程。 线程可以被同一进程中的其它线程取消。
pthread_exit函数 #include pthread.hvoid pthread_exit(void *retval);功能退出调用线程。一个进程中的多个线程是共享该进程的数据段因此通常线程退出后所占用的资源并不会释放。参数retval存储线程退出状态的指针。返回值无
参考程序: void *thread(void *arg){static int num 123; //静态变量int i 0;while (1){printf(I am runing\n);sleep(1);i;if (i 3){pthread_exit((void *)num);// return num;}}return NULL;}int main(int argc, char *argv[]){int ret 0;pthread_t tid;void *value NULL;pthread_create(tid, NULL, thread, NULL);pthread_join(tid, value);printf(value %d\n, *(int *)value);return 0;}
线程取消
#include pthread.h
int pthread_cancel(pthread_t thread); 功能 杀死(取消)线程 参数 thread : 目标线程ID。 返回值 成功0 失败出错编号
注意线程的取消并不是实时的而又一定的延时。需要等待线程到达某个取消点(检查点)。
类似于玩游戏存档必须到达指定的场所(存档点如客栈、仓库、城里等)才能存储进度。
杀死线程也不是立刻就能完成必须要到达取消点。
取消点是线程检查是否被取消并按请求进行动作的一个位置。通常是一些系统调用creatopenpauseclosereadwrite..... 执行命令**man 7 pthreads**可以查看具备这些取消点的系统调用列表。
可粗略认为一个系统调用(进入内核)即为一个取消点。
参考程序: void *thread_cancel(void *arg) { while (1) { pthread_testcancel(); //设置取消点 } return NULL; }
int main() { pthread_t tid; pthread_create(tid, NULL, thread_cancel, NULL); //创建线程 sleep(3); //3秒后 pthread_cancel(tid); //取消tid线程 pthread_join(tid, NULL); return 0; } 线程取消 #include pthread.h
int pthread_cancel(pthread_t thread); 功能 杀死(取消)线程 参数 thread : 目标线程ID。 返回值 成功0 失败出错编号 注意线程的取消并不是实时的而又一定的延时。需要等待线程到达某个取消点(检查点)。
类似于玩游戏存档必须到达指定的场所(存档点如客栈、仓库、城里等)才能存储进度。
杀死线程也不是立刻就能完成必须要到达取消点。
取消点是线程检查是否被取消并按请求进行动作的一个位置。通常是一些系统调用creatopenpauseclosereadwrite..... 执行命令**man 7 pthreads**可以查看具备这些取消点的系统调用列表。
可粗略认为一个系统调用(进入内核)即为一个取消点。
参考程序: void *thread_cancel(void *arg) { while (1) { pthread_testcancel(); //设置取消点 } return NULL; }
int main() { pthread_t tid; pthread_create(tid, NULL, thread_cancel, NULL); //创建线程 sleep(3); //3秒后 pthread_cancel(tid); //取消tid线程 pthread_join(tid, NULL); return 0; } 文章转载自: http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.hqgxz.cn.gov.cn.hqgxz.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.lnbyk.cn.gov.cn.lnbyk.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.neletea.com.gov.cn.neletea.com http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.sbyhj.cn.gov.cn.sbyhj.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.plxhq.cn.gov.cn.plxhq.cn http://www.morning.sgwr.cn.gov.cn.sgwr.cn http://www.morning.seoqun.com.gov.cn.seoqun.com http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.rtpw.cn.gov.cn.rtpw.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.ytfr.cn.gov.cn.ytfr.cn http://www.morning.xnflx.cn.gov.cn.xnflx.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.frqtc.cn.gov.cn.frqtc.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.jhrlk.cn.gov.cn.jhrlk.cn http://www.morning.srnth.cn.gov.cn.srnth.cn http://www.morning.ynryz.cn.gov.cn.ynryz.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.zxwqt.cn.gov.cn.zxwqt.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.khdw.cn.gov.cn.khdw.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.lzsxp.cn.gov.cn.lzsxp.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.lwmzp.cn.gov.cn.lwmzp.cn http://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.rkfwr.cn.gov.cn.rkfwr.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.msxhb.cn.gov.cn.msxhb.cn http://www.morning.gtqws.cn.gov.cn.gtqws.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.mpxbl.cn.gov.cn.mpxbl.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.mfct.cn.gov.cn.mfct.cn http://www.morning.kdnrp.cn.gov.cn.kdnrp.cn http://www.morning.myxps.cn.gov.cn.myxps.cn http://www.morning.dnqliv.cn.gov.cn.dnqliv.cn http://www.morning.joinyun.com.gov.cn.joinyun.com http://www.morning.pqnkg.cn.gov.cn.pqnkg.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.pdmsj.cn.gov.cn.pdmsj.cn http://www.morning.kybpj.cn.gov.cn.kybpj.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.qgbfx.cn.gov.cn.qgbfx.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.jphxt.cn.gov.cn.jphxt.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.qgjxt.cn.gov.cn.qgjxt.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn