【C】线程控制
创始人
2024-05-27 08:43:34
0

创建线程

#include int pthread_create(pthread_t * thread,const pthread_attr_t * attr,void *(*start_routine)(void*), void * arg);

返回值:成功返回0,失败返回错误号。

thread:成功返回后,新创建的线程的id被填写到 thread参数所指向的内存单元。线程id的类型是pthread_t,它只在当前进程中保证是唯一的,在不同的系统中pthread_t这个类型有不同的实现,它可能是一个整数值,也可能是一个结构体,也可能是一个地址,所以不能简单地当成整数用printf打印,调用pthread_self()可以获取当前线程地id。

attr:表示线程属性,这里暂不讨论。

start_routine和arg:新线程所指向的代码由函数指针start_routine决定。start_routine函数接收一个参数arg,该参数的类型为void *,这个指针按什么类型解释由调用者自己定义,start_routine的返回值类型也是void *,这个指针的函数的含义同样由调用者自己定义。

#include 
#include 
#include 
#include static void thread_func(const char *arg)
{printf("arg=%s\n", arg);pthread_t tid = pthread_self();printf("new thread pid=%u\n", (uint32_t)tid);
}int main(void)
{pthread_t id;int ret = pthread_create(&id, NULL, thread_func, "i am iron man");if (0 != ret){printf("pthread_carete error=%s", strerror(ret));}printf("new thread pid=%u\n", (uint32_t)id);while (1){usleep(1000);}return 0;
}

输出结果为:

new thread pid=1
arg=i am iron man
new thread pid=1

可知,在window上,thread_t类型是一个整型值。

终止线程

如果需要终止某个线程而不终止整个进程,可以有三种方法:

  • 从线程函数return。这种方法对主线程不适用,从main函数return相当于调用exit
  • 一个线程可以调用pthread_cancel终止同一进程中地另一个线程。
  • 线程可以调用pthread_exit终止自己。

在介绍终止之前,我们先介绍一下pthread_join函数。

#include int pthread_join(pthread_t thread, void **value_ptr);//返回值:成功返回0,失败返回错误号。

调用该函数地线程将挂起等待,直到id为thread的线程终止。thread线程以不同的方法终止,通过pthread_join得到的终止状态时不同的,总结如下:

  • 如果thread线程通过return返回,value_ptr所指向的单元里存放的是thread线程函数的返回值。
  • 如果thread线程被别的线程调用thread_cancel异常终止掉,value_ptr所指向的单元里存放的是常数PTHREAD_CANCELED
  • 如果thread线程是自己调用pthread_exit终止的,value_ptr所指向的单元里存放的是传给pthread_exit的参数。

如果对thread线程的终止状态不感兴趣,可以传NULLvalue_ptr参数。

一般情况下,线程终止后,其终止状态一直保留到其他线程调用pthread_join获取它的状态为止。但是线程也可以被设置为detach状态,这样的线程一旦终止就立刻回收它占用的所有资源,而不保留终止状态。不能对一个已经处于detach状态的线程调用pthread_join,这样的调用将返回EINVAL。对一个尚未detach的线程调用pthread_joinpthread_detach都可以把该线程设置为detach状态,也就是说,不能对同一线程调用两次pthread_join,或者如果已经对一个线程调用了pthread_detach就不能再调用pthread_join了。

#include int pthread_detach(pthread_t tid);//返回值:成功返回0,失败返回错误号。
#include 
#include 
#include 
#include static void *thread1_func(const char *arg)
{printf("arg=%s\n", arg);pthread_t tid = pthread_self();printf("new thread pid=%u\n", (uint32_t)tid);return (void *)0xFF;
}static void *thread2_func(const char *arg)
{printf("arg=%s\n", arg);pthread_t tid = pthread_self();printf("new thread pid=%u\n", (uint32_t)tid);pthread_exit((void *)0xFE);
}static void *thread3_func(const char *arg)
{printf("arg=%s\n", arg);pthread_t tid = pthread_self();printf("new thread pid=%u\n", (uint32_t)tid);while (1){printf("%s running\n", arg);usleep(5000 * 100);}
}int main(void)
{pthread_t id1;int ret1 = pthread_create(&id1, NULL, thread1_func, "i am thread1");if (0 != ret1){printf("pthread_carete error=%s", strerror(ret1));}void *thread1_ret;pthread_join(id1, &thread1_ret);printf("thread1_ret=%d\n", (int)thread1_ret);pthread_t id2;int ret2 = pthread_create(&id2, NULL, thread2_func, "i am thread2");if (0 != ret2){printf("pthread_carete error=%s", strerror(ret2));}void *thread2_ret;pthread_join(id2, &thread2_ret);printf("thread2_ret=%d\n", (int)thread2_ret);pthread_t id3;int ret3 = pthread_create(&id3, NULL, thread3_func, "i am thread3");if (0 != ret3){printf("pthread_carete error=%s", strerror(ret3));}void *thread3_ret;sleep(1);pthread_cancel(id3);pthread_join(id3, &thread3_ret);printf("thread3_ret=%d\n", (int)thread3_ret);while (1){usleep(1000);}return 0;
}

输出为:

arg=i am thread1
new thread pid=1
thread1_ret=255
arg=i am thread2
new thread pid=3
thread2_ret=254
arg=i am thread3
new thread pid=4
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running
i am thread3 running

我们可以看到,线程1和线程2的终止与我们的预期一样,但是线程3使用pthread_cancel()似乎没有真正终止线程。那这里就需要引用另外两个概念了。

线程终止状态
	@state:PTHREAD_CANCEL_ENABLE:线程对cancel信号立即有反应,将设置为CANCEL状态 (默认)PTHREAD_CANCEL_DISABLE:如果线程state为不可取消,线程不理会信号,继续执行,而使用cancel函数的线程会一直阻塞到可取消状态@oldstate:NULL:state写入有效,即当前只想设置属性,而不关心原来的属性不为NULL:state不写入,保持原有的设定,且获取原来的属性return:成功返回0,错误返回errno	EINVAL:无效的stateint pthread_setcancelstate(int state,int *oldstate)
线程终止类型
	@type:PTHREAD_CANCEL_DEFERRED:运行到下一个取消点就退出 (默认)PTHREAD_CANCEL_ASYNCHRONOUS:直接退出@oldtyoe:NULL:type写入有效,即当前只想设置属性,而不关心原来的属性不为NULL:type不写入,保持原有的设定,且获取原来的属性return:成功返回0,错误返回errno	EINVAL:无效的type
int pthread_setcanceltype(int type,int *oldtype)

所以,如下想要使用pthread_cancel终止线程,那么我们必须在线程函数中调用

pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

当我们把线程函数改成:

static void *thread3_func(const char *arg)
{pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);printf("arg=%s\n", arg);pthread_t tid = pthread_self();printf("new thread pid=%u\n", (uint32_t)tid);while (1){printf("%s running\n", arg);usleep(5000 * 100);}
}

那么就可以正常终止了,输出结果如下:

arg=i am thread1
new thread pid=1
thread1_ret=255
arg=i am thread2
new thread pid=3
thread2_ret=254
arg=i am thread3
new thread pid=4
i am thread3 running
i am thread3 running
thread3_ret=-559038737

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...