中国铁建华南建设有限公司网站,wordpress 分类信息模板,上海公司核名查询,企业门户网站建设内容创建线程#xff1a;#xff08;好好记住 可能会叫写代码#xff09;
一般情况下,main函数所在的线程我们称之为主线程#xff08;main线程#xff09;#xff0c;其余创建的线程称之为子线程。 程序中默认只有一个进程#xff0c;fork()函数调用#xff0c;2进行 程序… 创建线程好好记住 可能会叫写代码
一般情况下,main函数所在的线程我们称之为主线程main线程其余创建的线程称之为子线程。 程序中默认只有一个进程fork()函数调用2进行 程序中默认只有一个线程pthread_create()函数调用2个线程。
#include pthread.h int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); - 功能创建一个子线程- 参数- thread传出参数线程创建成功后子线程的线程ID被写到该变量中。- attr : 设置线程的属性一般使用默认值NULL- start_routine : 函数指针这个函数是子线程需要处理的逻辑代码- arg : 给第三个参数使用传参- 返回值成功0失败返回错误号。这个错误号和之前errno不太一样。获取错误号的信息 char * strerror(int errnum);#include stdio.h
#include pthread.h
#include string.h
#include unistd.hvoid * callback(void * arg) {printf(child thread...\n);printf(arg value: %d\n, *(int *)arg);return NULL;
}int main() {pthread_t tid;int num 10;// 创建一个子线程int ret pthread_create(tid, NULL, callback, (void *)num);if(ret ! 0) {char * errstr strerror(ret);printf(error : %s\n, errstr);} for(int i 0; i 5; i) {printf(%d\n, i);}sleep(1);return 0; // exit(0);
}终止线程
/*#include pthread.hvoid pthread_exit(void *retval);功能终止一个线程在哪个线程中调用就表示终止哪个线程参数retval:需要传递一个指针作为一个返回值可以在pthread_join()中获取到。pthread_t pthread_self(void);功能获取当前的线程的线程IDint pthread_equal(pthread_t t1, pthread_t t2);功能比较两个线程ID是否相等不同的操作系统pthread_t类型的实现不一样有的是无符号的长整型有的是使用结构体去实现的。
*/
#include stdio.h
#include pthread.h
#include string.hvoid * callback(void * arg) {printf(child thread id : %ld\n, pthread_self());return NULL; // pthread_exit(NULL);
} int main() {// 创建一个子线程pthread_t tid;int ret pthread_create(tid, NULL, callback, NULL);if(ret ! 0) {char * errstr strerror(ret);printf(error : %s\n, errstr);}// 主线程for(int i 0; i 5; i) {printf(%d\n, i);}printf(tid : %ld, main thread id : %ld\n, tid ,pthread_self());// 让主线程退出,当主线程退出时不会影响其他正常运行的线程。pthread_exit(NULL);printf(main thread exit\n);return 0; // exit(0);
}连接已终止的线程
/*#include pthread.hint pthread_join(pthread_t thread, void **retval);- 功能和一个已经终止的线程进行连接回收子线程的资源这个函数是阻塞函数调用一次只能回收一个子线程一般在主线程中使用- 参数- thread需要回收的子线程的ID- retval: 接收子线程退出时的返回值- 返回值0 : 成功非0 : 失败返回的错误号
*/#include stdio.h
#include pthread.h
#include string.h
#include unistd.hint value 10;void * callback(void * arg) {printf(child thread id : %ld\n, pthread_self());// sleep(3);// return NULL; // int value 10; // 局部变量pthread_exit((void *)value); // return (void *)value;
} int main() {// 创建一个子线程pthread_t tid;int ret pthread_create(tid, NULL, callback, NULL);if(ret ! 0) {char * errstr strerror(ret);printf(error : %s\n, errstr);}// 主线程for(int i 0; i 5; i) {printf(%d\n, i);}printf(tid : %ld, main thread id : %ld\n, tid ,pthread_self());// 主线程调用pthread_join()回收子线程的资源int * thread_retval;ret pthread_join(tid, (void **)thread_retval);if(ret ! 0) {char * errstr strerror(ret);printf(error : %s\n, errstr);}printf(exit data : %d\n, *thread_retval);printf(回收子线程资源成功\n);// 让主线程退出,当主线程退出时不会影响其他正常运行的线程。pthread_exit(NULL);return 0;
}
线程的分离
/*#include pthread.hint pthread_detach(pthread_t thread);- 功能分离一个线程。被分离的线程在终止的时候会自动释放资源返回给系统。1.不能多次分离会产生不可预料的行为。2.不能去连接一个已经分离的线程会报错。- 参数需要分离的线程的ID- 返回值成功0失败返回错误号
*/
#include stdio.h
#include pthread.h
#include string.h
#include unistd.hvoid * callback(void * arg) {printf(chid thread id : %ld\n, pthread_self());return NULL;
}int main() {// 创建一个子线程pthread_t tid;int ret pthread_create(tid, NULL, callback, NULL);if(ret ! 0) {char * errstr strerror(ret);printf(error1 : %s\n, errstr);}// 输出主线程和子线程的idprintf(tid : %ld, main thread id : %ld\n, tid, pthread_self());// 设置子线程分离,子线程分离后子线程结束时对应的资源就不需要主线程释放ret pthread_detach(tid);if(ret ! 0) {char * errstr strerror(ret);printf(error2 : %s\n, errstr);}// 设置分离后对分离的子线程进行连接 pthread_join()// ret pthread_join(tid, NULL);// if(ret ! 0) {// char * errstr strerror(ret);// printf(error3 : %s\n, errstr);// }pthread_exit(NULL);return 0;
}