pthread 基础篇 结束线程
2013-05-21 20:18
134 查看
一 结束线程
1 当线程回调函数执行完后,即在回调函数中调用return语句时,默认线程终止(自然死亡)
2 显示调用pthread_exit函数(自尽^_^)。
3 调用exit函数,结束进程,也包括进程下的所有线程(地球消失了,自然活在地球上的人也就挂掉了^_^)
二 pthread_exit与return的区别
在主线程中(以main为回调函数的线程):
1· 在main中
调用return语句,会结束整个进程,即使进程下还有其他线程在工作。
调用pthread_exit,仅仅是结束main所在的线程,如果进程下还有其他线程在工作,则进程继续存在,其他线程正常工作。
2· 在一般线程中
在一般线程的回调函数中,调用return和pthread_exit效果是一样的。
当想要在回调函数调用的方法中结束线程时只能用pthread_exit。
三 测试代码
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *thread1_proc_function(void *argements)
{
int nCount = 0;
while(1)
{
printf("thread1 nCount = %d\n", nCount);
nCount++;
sleep(2);
if(3 == nCount)
{
pthread_exit(NULL);
}
}
}
void *thread2_proc_function(void *argements)
{
int nCount = 0;
while(1)
{
printf("thread2 nCount == %d\n", nCount);
nCount++;
sleep(2);
if(8 == nCount)
{
pthread_exit(NULL);
}
}
}
void thread_sub_function(void)
{
pthread_exit(NULL);
}
void *thread3_proc_function(void *argements)
{
int nCount = 0;
while(1)
{
printf("thread3 nCount == %d\n", nCount);
nCount++;
sleep(2);
if(15 == nCount)
{
thread_sub_function();
}
}
}
void main(void)
{
pthread_t thread1, thread2, thread3;
int iret1 = 0;
int iret2 = 0;
int iret3 = 0;
int nCount = 0;
iret1 = pthread_create(&thread1, NULL, thread1_proc_function, NULL);
iret2 = pthread_create(&thread2, NULL, thread2_proc_function, NULL);
iret3 = pthread_create(&thread3, NULL, thread3_proc_function, NULL);
while(1)
{
printf("main nCount = %d\n", nCount);
nCount ++;
sleep(2);
if(10 == nCount)
{
//return;
pthread_exit(NULL);
}
}
exit(0);
}
参考资料:
1· http://zh.wikipedia.org/wiki/Native_POSIX_Thread_Library
2· http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#BASICS
1 当线程回调函数执行完后,即在回调函数中调用return语句时,默认线程终止(自然死亡)
2 显示调用pthread_exit函数(自尽^_^)。
3 调用exit函数,结束进程,也包括进程下的所有线程(地球消失了,自然活在地球上的人也就挂掉了^_^)
二 pthread_exit与return的区别
在主线程中(以main为回调函数的线程):
1· 在main中
调用return语句,会结束整个进程,即使进程下还有其他线程在工作。
调用pthread_exit,仅仅是结束main所在的线程,如果进程下还有其他线程在工作,则进程继续存在,其他线程正常工作。
2· 在一般线程中
在一般线程的回调函数中,调用return和pthread_exit效果是一样的。
当想要在回调函数调用的方法中结束线程时只能用pthread_exit。
三 测试代码
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *thread1_proc_function(void *argements)
{
int nCount = 0;
while(1)
{
printf("thread1 nCount = %d\n", nCount);
nCount++;
sleep(2);
if(3 == nCount)
{
pthread_exit(NULL);
}
}
}
void *thread2_proc_function(void *argements)
{
int nCount = 0;
while(1)
{
printf("thread2 nCount == %d\n", nCount);
nCount++;
sleep(2);
if(8 == nCount)
{
pthread_exit(NULL);
}
}
}
void thread_sub_function(void)
{
pthread_exit(NULL);
}
void *thread3_proc_function(void *argements)
{
int nCount = 0;
while(1)
{
printf("thread3 nCount == %d\n", nCount);
nCount++;
sleep(2);
if(15 == nCount)
{
thread_sub_function();
}
}
}
void main(void)
{
pthread_t thread1, thread2, thread3;
int iret1 = 0;
int iret2 = 0;
int iret3 = 0;
int nCount = 0;
iret1 = pthread_create(&thread1, NULL, thread1_proc_function, NULL);
iret2 = pthread_create(&thread2, NULL, thread2_proc_function, NULL);
iret3 = pthread_create(&thread3, NULL, thread3_proc_function, NULL);
while(1)
{
printf("main nCount = %d\n", nCount);
nCount ++;
sleep(2);
if(10 == nCount)
{
//return;
pthread_exit(NULL);
}
}
exit(0);
}
参考资料:
1· http://zh.wikipedia.org/wiki/Native_POSIX_Thread_Library
2· http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#BASICS
相关文章推荐
- NDK-JNI-pthread_kill结束线程
- linux多线程之pthread_cancel结束线程
- NDK-JNI-pthread_kill结束线程
- 网络编程(35)—— 利用pthread_join函数等待线程结束并获取线程函数返回值
- pthread_cancel() 能结束哪些线程-linux
- 初识pthread(一)-线程管理之创建/结束线程
- pthread 基础篇 创建线程
- Windows和Linux上分别怎样实现等待某个线程结束?---用WaitForSingleObject和pthread_join
- 初识pthread(二)-线程管理之线程同步入门(阻塞和分离式线程)
- java并发(一) CountDownLatch (在多个线程都结束后再去执行某个任务)
- Application.Exit()结束程序,但线程还在的解决方法
- 线程属性pthread_attr_t简介-转
- linux 创建线程pthread_create
- 关于线程的删除:pthread_cancel函数的替代
- 【C/C++】BOOST 线程完全攻略 - 基础篇
- posix 线程和pthread 系列函数
- 线程pthread_create,pthread_join
- 调用系统函数pthread_cancel取消进程的其他线程
- c线程中sleep()和pthread_join()函数之我见
- 结束线程的方法