营销型网站 策划运营网站办公室网络设计方案

当前位置: 首页 > news >正文

营销型网站 策划运营网站,办公室网络设计方案,查询域名注册信息,做网站百灵鸟目录 1.前言 2.创建线程
pthread_create函数 3.线程终止 pthread_exit函数 pthread_cancel函数 4.线程等待 5.线程分离 1.前言 在Linux系统中#xff0c;并不存在真正的线程#xff0c;只有轻量级进程。所以#xff0c;Linux系统只提供了操作轻量级进程的系统调用…目录 1.前言 2.创建线程  pthread_create函数 3.线程终止 pthread_exit函数 pthreadcancel函数 4.线程等待 5.线程分离 1.前言 在Linux系统中并不存在真正的线程只有轻量级进程。所以Linux系统只提供了操作轻量级进程的系统调用接口并不提供直接操作线程的系统调用接口。但是对于用户来说用户想要对线程进行操作只认线程相关的接口。于是有人对轻量级进程的系统调用接口进行封装转换成线程相关的接口语义给用户使用。 封装其实就是封装成库这个库被叫做Linux的原生线程库 与线程有关的函数构成了一个完整的系列绝大多数函数的名字都是以“pthread”开头。要使用这些函数库要通过引入头文pthread.h。链接这些线程函数库时要使用编译器命令的“-lpthread”选项  2.创建线程  pthread_create函数 pthread库中创建线程的函数为pthread_create创建出的新线程和主线程谁先运行时不确定的由调度器说了算。 功能用于创建一个新的线程。 函数原型 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,                                                             void *(start_routine) (void), void *arg); 参数 pthread_t *thread这是一个输出型参数用于存储新线程的标识符线程ID。线程创建成功后系统会将线程ID写入该指针指向的内存。原生线程库中还提供了一个让线程获取自己的线程id的方法pthread_t pthread_self(void) const pthread_attr_t *attr指向线程属性的指针用于设置线程的属性如栈大小、调度策略等如果为 NULL则使用默认属性。 void *(*start_routine) (void *): 线程启动后执行的函数指针。该函数必须返回 void * 并接受一个 void * 类型的参数。 线程从该函数的起始处开始执行函数返回时线程终止。 void arg传递给 start_routine 函数的参数。如果需要传递多个参数可以将它们封装在一个结构体中然后传递结构体的指针。 返回值 成功返回 0。 失败返回错误码非零值常见的错误码包括 EAGAIN系统资源不足无法创建线程。 EINVAL线程属性无效。 EPERM没有权限设置调度策略或参数。 使用示例 #include iostream #include pthread.h #include unistd.h using namespace std;void handler(void* arg) {int cnt 5;while(cnt–){cout I am a thread endl;sleep(1);}return nullptr; }int main() {pthread_t thread_id 0;int ret pthread_create(thread_id, NULL, handler, NULL);if(ret ! 0){cout create error endl;}sleep(6);return 0; } 运行结果 当我们的代码创建出一个线程之后新线程就会和主线程并发执行自己的代码。如果主线程先退表示进程退出新线程也会结束如果新线程先结束主线程不会直接结束会等自己的代码运行完之后再结束。 3.线程终止 终止一个线程的方法有三种 return使用return语句退出。pthread_exit线程可以调用pthread_exit函数终止自己。pthread_cancel一个线程可以调用pthread_cancel终止同一进程中的另一个线程。 pthread_exit函数 功能终止当前进程的执行。 函数原型void pthread_exit(void *retval) 参数 void retval: 线程的返回值通常是一个指向某个数据的指针。 如果不需要返回值可以传递 NULL。 该返回值可以被其他线程通过 pthread_join 获取。 返回值该函数没有返回值。 需要注意pthread_exit或者return返回的指针所指向的内存单元必须是全局的或者是用malloc分配的不能在线程函数的栈上分配因为当其它线程得到这个返回指针时线程函数已经退出了。 使用示例 #include iostream #include pthread.h #include unistd.h using namespace std;void handler(void* arg) {int cnt 3;while(cnt–){cout I am a thread endl;sleep(1);if(cnt 1){cout called pthead_exit endl;pthread_exit(NULL);}}return nullptr; }int main() {pthread_t thread_id 0;int ret pthread_create(thread_id, NULL, handler, NULL);if(ret ! 0){cout create error endl;}sleep(5);return 0; } 运行结果 pthread_cancel函数 功能用于请求取消终止指定的线程。 函数原型int pthread_cancel(pthread_t thread) 参数 pthread_t thread目标线程的标识符线程ID即需要取消的线程。 返回值 成功返回 0。 失败返回错误码非零值。 使用示例 #include iostream #include pthread.h #include unistd.h using namespace std;void* handler(void* arg) {int cnt 10;while(cnt–){cout new thread say: I am runnig endl;sleep(1);}return nullptr; }int main() {pthread_t tid 0;int ret pthread_create(tid, NULL, handler, NULL);if(ret ! 0){cout create thread error endl;}cout create thread success endl;sleep(5);ret pthread_cancel(tid);if(ret ! 0){cout cancel thread error endl;}cout cancel thread success endl;return 0; } 运行结果 4.线程等待 在进程控制中如果一个子进程退出父进程需要对子进程进行回收也就是需要进行进程等待不然就会造成僵尸进程而引发资源泄漏问题在线程这里一个线程退出后主线程需要对其进行回收不然也会产生类似的问题。 已经退出的线程其空间没有被释放仍然在进程的地址空间内。创建新的线程不会复用刚才退出线程的地址空间。 pthread_join函数 功能用于等待指定的线程终止并获取该线程的返回值。 函数原型int pthread_join(pthread_t thread, void value_ptr) 参数 thread: 要等待的线程的标识符pthread_t 类型。 value_ptr: 指向一个指针的指针用于存储目标线程的返回值。如果不需要返回值可以设置为 NULL。 返回值 成功时返回 0。 失败时返回一个错误码非零值常见的错误码包括 ESRCH: 没有找到与指定线程 ID 对应的线程。 EINVAL: 线程是分离的detached或者已经有其他线程在等待它。 EDEADLK: 检测到死锁例如线程试图等待自己。 需要注意 调用该函数的线程将挂起等待直到id为thread的线程终止。 thread线程以不同的方法终止通过pthreadjoin得到的终止状态是不同的总结如下: 1. 如果thread线程通过return返回value ptr所指向的单元里存放的是thread线程函数的返回值。2. 如果thread线程被别的线程调用pthread_ cancel异常终掉value_ ptr所指向的单元里存放的是常数 —— PTHREAD_ CANCELED。3. 如果thread线程是自己调用pthread_exit终止的value_ptr所指向的单元存放的是传给pthreadexit的参数。4. 如果对thread线程的终止状态不感兴趣可以传NULL给value ptr参数。 使用示例 #include pthread.h #include stdio.h #include stdlib.hvoid* thread_function(void* arg) {int* value (int*)arg;printf(Thread is running with value: %d\n, value);int result (int*)malloc(sizeof(int));*result *value * 2;pthread_exit(result); }int main() {pthread_t thread;int value 10;int* retval;if (pthread_create(thread, NULL, thread_function, value) ! 0) {perror(pthread_create);exit(EXIT_FAILURE);}if (pthread_join(thread, (void)retval) ! 0) {perror(pthread_join);exit(EXIT_FAILURE);}printf(Thread returned: %d\n, retval);free(retval);return 0; } 运行结果 5.线程分离 默认情况下新创建的线程是需要等待的新线程退出后需要主线程对其进行pthread_join操作否则无法释放资源从而造成系统资源泄漏。如果不关心线程的返回值等待就是一种负担这个时候我们可以将该线程分离当线程退出时操作系统会自动释放被分离的线程的资源。 注意线程分离只是主线程不用等待新线程的退出了并不是把新线程剥离下来了。 pthread_detach函数 功能用于将指定的线程标记为“分离状态”分离状态的线程在终止时会自动释放其资源而不需要其他线程调用 pthread_join 来回收资源。 函数原型int pthread_detach(pthread_t thread) 参数 thread要分离的线程的标识符pthread_t 类型 返回值 成功时返回 0。 失败时返回一个错误码非零值常见的错误码包括 ESRCH: 没有找到与指定线程 ID 对应的线程。 EINVAL: 线程已经是分离状态或者线程已经终止。 注意 一旦线程被分离就不能再调用 pthread_join 来等待它否则会导致未定义行为线程分离可以是线程组内其他线程对目标线程进行分离也可以是线程自己将自己分离。 使用示例 #include pthread.h #include stdio.h #include stdlib.h #include unistd.hvoid thread_function(void* arg) {int* value (int*)arg;printf(Thread is running with value: %d\n, *value);sleep(2); // 模拟线程执行一些任务printf(Thread is exiting\n);pthread_exit(NULL); }int main() {pthread_t thread;int value 10;if (pthread_create(thread, NULL, thread_function, value) ! 0) {perror(pthread_create);exit(EXIT_FAILURE);}// 分离线程if (pthread_detach(thread) ! 0) {perror(pthread_detach);exit(EXIT_FAILURE);}printf(Main thread continues to run…\n);sleep(3); // 确保分离的线程有足够时间完成return 0; } 运行结果