首页 技术 正文
技术 2022年11月15日
0 收藏 487 点赞 4,414 浏览 11940 个字

1、pthread_create以及pthread_self函数

 /*************************************************************************
> File Name: pthread1.c
> Summary: 两个函数的使用:pthread_create() 以及函数 pthread_self()
> Author: xuelisheng
> Created Time: 2018年12月13日
************************************************************************/ #include<stdio.h>
#include<stdlib.h>
#include<pthread.h> void *callBack()
{
// pthread_self():用来获得线程id。返回值为线程id,没有参数。
printf("pthread_self return value = %lu", pthread_self());
return NULL;
} int main()
{
pthread_t pid;
/*
pthread_create():成功返回值0,失败返回一个错误号(非0的值)。
参数1:pthread_t tid; 传出参数,指向线程标识符的指针。 &tid
参数2:属性 NULL
参数3:回调函数的函数指针。
参数4:回调函数的参数列表。没有的话传NULL
*/
int ret = pthread_create(&pid,NULL,callBack,NULL);
if(ret != )
{
printf("pthread_create fail\n");
exit(-);
}
//阻塞主线程一会
sleep();
return ;
}

运行结果:

pthread_self return value = 

2、循环创建多个子线程

第一种情况:

 /*************************************************************************
> File Name: pthread2.c
> Summary: pthread_create() 循环创建多个线程
> Author: xuelisheng
> Created Time: 2018年12月13日
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h> void *callBack(void *arg)
{
int i = (int)arg;
sleep(i);
// pthread_self():用来获得线程id。返回值为线程id,没有参数。
printf("i am %d th thread,getpid() = %d,pthread_self return value = %lu\n", i+, getpid(), pthread_self());
return NULL;
} int main()
{
pthread_t pid;
/*
pthread_create():成功返回值0,失败返回一个错误号(非0的值)。
参数1:pthread_t tid; 传出参数,指向线程标识符的指针。 &tid
参数2:属性 null
参数3:回调函数的函数指针。
参数4:回调函数的参数列表。没有的话传NULL
*/
int i;
for(i = ; i<; i++)
{
int ret = pthread_create(&pid,NULL,callBack, (void *)i);
if(ret != )
{
printf("pthread_create fail\n");
exit(-);
}
} //阻塞主线程一会
sleep(i);
printf("i am main thread,getpid() = %d,pthread_self return value = %lu\n", getpid(), pthread_self());
return ;
}

运行结果:

 i am  th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am main thread,getpid() = ,pthread_self return value =

第二种情况:

 /*************************************************************************
> File Name: pthread3.c
> Summary: pthread_create() 循环创建多个线程(第二种情况)
> Author: xuelisheng
> Created Time: 2018年12月13日
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h> //void *callBack(void *arg)
//{
// int i = (int)arg;
void *callBack(void *arg)
{
int i = *((int *)arg);
sleep(i);
// pthread_self():用来获得线程id。返回值为线程id,没有参数。
printf("i am %d th thread,getpid() = %d,pthread_self return value = %lu\n", i+, getpid(), pthread_self());
return NULL;
} int main()
{
pthread_t pid;
/*
pthread_create():成功返回值0,失败返回一个错误号(非0的值)。
参数1:pthread_t tid; 传出参数,指向线程标识符的指针。 &tid
参数2:属性 null
参数3:回调函数的函数指针。
参数4:回调函数的参数列表。没有的话传NULL
*/
int i;
for(i = ; i<; i++)
{
//int ret = pthread_create(&pid,NULL,callBack, (void *)i);
int ret = pthread_create(&pid,NULL,callBack, (void *)&i); //注意这里如果取地址,主线程的i不断变化,所以取到的i值出现混乱
if(ret != )
{
printf("pthread_create fail\n");
exit(-);
}
} //阻塞主线程一会
sleep(i);
printf("i am main thread,getpid() = %d,pthread_self return value = %lu\n", getpid(), pthread_self());
return ;
}

运行结果:

 i am  th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am main thread,getpid() = ,pthread_self return value =

3、线程间全局变量共享

 /*************************************************************************
> File Name: pthread4.c
> Summary: 线程间全局变量共享 验证
> Author: xuelisheng
> Created Time: 2018年12月13日
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h> // 定义全局变量
int var = ;
void *callBack()
{
var = ;
printf("child thread running now var = %d\n", var);
return NULL;
} int main()
{
printf("before child thread create var = %d\n", var);
pthread_t pid;
/*
pthread_create():成功返回值0,失败返回一个错误号(非0的值)。
参数1:pthread_t tid; 传出参数,指向线程标识符的指针。 &tid
参数2:属性 null
参数3:回调函数的函数指针。
参数4:回调函数的参数列表。没有的话传NULL
*/
int ret = pthread_create(&pid,NULL,callBack, NULL);
if(ret != )
{
printf("pthread_create fail\n");
exit(-);
}
//阻塞主线程一会
sleep();
printf("after child thread over now var = %d\n", var);
return ;
}

运行结果:

 before child thread create var =
child thread running now var =
after child thread over now var =

4、函数pthread_exit()

情形一:

 /*************************************************************************
> File Name: pthread5.c
> Summary: pthread_exit函数之 使用exit函数退出线程
> Author: xuelisheng
> Created Time: 2018年12月13日
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h> void *callBack(void *arg)
{
int i = (int)arg;
sleep(i);
19 if(i == 2){
20 exit(0);
21 }

// pthread_self():用来获得线程id。返回值为线程id,没有参数。
printf("i am %d th thread,getpid() = %d,pthread_self return value = %lu\n", i+, getpid(), pthread_self());
return NULL;
} int main()
{
pthread_t pid;
/*
pthread_create():成功返回值0,失败返回一个错误号(非0的值)。
参数1:pthread_t tid; 传出参数,指向线程标识符的指针。 &tid
参数2:属性 null
参数3:回调函数的函数指针。
参数4:回调函数的参数列表。没有的话传NULL
*/
int i;
for(i = ; i<; i++)
{
int ret = pthread_create(&pid,NULL,callBack, (void *)i);
if(ret != )
{
printf("pthread_create fail\n");
exit(-);
}
} //阻塞主线程一会
sleep(i);
printf("i am main thread,getpid() = %d,pthread_self return value = %lu\n", getpid(), pthread_self());
return ;
}

运行结果:

i am  th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =

使用exit函数退出线程:exit()进程退出,如果在线程函数中调用exit,那改线程的进程也就挂了,会导致该线程所在进程的其他线程也挂掉,比较严重。

情形2-1:使用return 退出线程

 /*************************************************************************
> File Name: pthread6.c
> Summary: pthread_exit函数之 使用return 退出线程
> Author: xuelisheng
> Created Time: 2018年12月13日
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h> void *callBack(void *arg)
{
int i = (int)arg;
sleep(i);
19 if(i == 2){
20 return NULL;
21 }

// pthread_self():用来获得线程id。返回值为线程id,没有参数。
printf("i am %d th thread,getpid() = %d,pthread_self return value = %lu\n", i+, getpid(), pthread_self());
return NULL;
} int main()
{
pthread_t pid;
/*
pthread_create():成功返回值0,失败返回一个错误号(非0的值)。
参数1:pthread_t tid; 传出参数,指向线程标识符的指针。 &tid
参数2:属性 null
参数3:回调函数的函数指针。
参数4:回调函数的参数列表。没有的话传NULL
*/
int i;
for(i = ; i<; i++)
{
int ret = pthread_create(&pid,NULL,callBack, (void *)i);
if(ret != )
{
printf("pthread_create fail\n");
exit(-);
}
} //阻塞主线程一会
sleep(i);
printf("i am main thread,getpid() = %d,pthread_self return value = %lu\n", getpid(), pthread_self());
return ;
}

运行结果:(正常)

 i am  th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am main thread,getpid() = ,pthread_self return value =

情形2-2:使用return 退出线程(嵌套)

 /*************************************************************************
> File Name: pthread7.c
> Summary: pthread_exit函数之 使用return 退出线程(嵌套)
> Author: xuelisheng
> Created Time: 2018年12月13日
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>15 void *fun(){
16 return NULL;
17 }

void *callBack(void *arg)
{
int i = (int)arg;
sleep(i);
23 if(i == 2){
24 fun();
25 }

// pthread_self():用来获得线程id。返回值为线程id,没有参数。
printf("i am %d th thread,getpid() = %d,pthread_self return value = %lu\n", i+, getpid(), pthread_self());
return NULL;
} int main()
{
pthread_t pid;
/*
pthread_create():成功返回值0,失败返回一个错误号(非0的值)。
参数1:pthread_t tid; 传出参数,指向线程标识符的指针。 &tid
参数2:属性 null
参数3:回调函数的函数指针。
参数4:回调函数的参数列表。没有的话传NULL
*/
int i;
for(i = ; i<; i++)
{
int ret = pthread_create(&pid,NULL,callBack, (void *)i);
if(ret != )
{
printf("pthread_create fail\n");
exit(-);
}
} //阻塞主线程一会
sleep(i);
printf("i am main thread,getpid() = %d,pthread_self return value = %lu\n", getpid(), pthread_self());
return ;
}

运行结果:(不正常)

i am  th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am main thread,getpid() = ,pthread_self return value =

结论:return是函数返回,不一定是线程函数哦(情形2-2)! 只有线程函数(情形2-1)return,线程才会退出。

情形3-1:使用pthread_exit函数退出

 /*************************************************************************
> File Name: pthread8.c
> Summary: pthread_exit函数 情形1
> Author: xuelisheng
> Created Time: 2018年12月13日
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h> void *callBack(void *arg)
{
int i = (int)arg;
sleep(i);
19 if(i == 2){
20 // 函数的作用是,终止调用它的线程并返回一个指向某个对象的指针。函数无返回值,参数为传出参数。
21 pthread_exit(NULL);
22 }

// pthread_self():用来获得线程id。返回值为线程id,没有参数。
printf("i am %d th thread,getpid() = %d,pthread_self return value = %lu\n", i+, getpid(), pthread_self());
return NULL;
} int main()
{
pthread_t pid;
/*
pthread_create():成功返回值0,失败返回一个错误号(非0的值)。
参数1:pthread_t tid; 传出参数,指向线程标识符的指针。 &tid
参数2:属性 null
参数3:回调函数的函数指针。
参数4:回调函数的参数列表。没有的话传NULL
*/
int i;
for(i = ; i<; i++)
{
int ret = pthread_create(&pid,NULL,callBack, (void *)i);
if(ret != )
{
printf("pthread_create fail\n");
exit(-);
}
} //阻塞主线程一会
sleep(i);
printf("i am main thread,getpid() = %d,pthread_self return value = %lu\n", getpid(), pthread_self());
return ;
}

运行结果:(正常)

i am  th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am main thread,getpid() = ,pthread_self return value =

情形3-2:使用pthread_exit函数退出(嵌套)

 /*************************************************************************
> File Name: pthread9.c
> Summary: pthread_exit函数 情形2 (嵌套)
> Author: xuelisheng
> Created Time: 2018年12月13日
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>15 void *fun()
16 {
17 pthread_exit(NULL);
18 }

void *callBack(void *arg)
{
int i = (int)arg;
sleep(i);
24 if(i == 2){
25 // 函数的作用是,终止调用它的线程并返回一个指向某个对象的指针。函数无返回值,参数为传出参数。
26 //pthread_exit(NULL);
27 fun();
28 }

// pthread_self():用来获得线程id。返回值为线程id,没有参数。
printf("i am %d th thread,getpid() = %d,pthread_self return value = %lu\n", i+, getpid(), pthread_self());
return NULL;
} int main()
{
pthread_t pid;
/*
pthread_create():成功返回值0,失败返回一个错误号(非0的值)。
参数1:pthread_t tid; 传出参数,指向线程标识符的指针。 &tid
参数2:属性 null
参数3:回调函数的函数指针。
参数4:回调函数的参数列表。没有的话传NULL
*/
int i;
for(i = ; i<; i++)
{
int ret = pthread_create(&pid,NULL,callBack, (void *)i);
if(ret != )
{
printf("pthread_create fail\n");
exit(-);
}
} //阻塞主线程一会
sleep(i);
printf("i am main thread,getpid() = %d,pthread_self return value = %lu\n", getpid(), pthread_self());
return ;
}

运行结果:(正常)

i am  th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am main thread,getpid() = ,pthread_self return value =

pthread_exit()用于线程退出,可以指定返回值,以便其他线程通过pthread_join()函数获取该线程的返回值。

情形4:主函数中使用pthread_exit替代sleep:

 /*************************************************************************
> File Name: pthread10.c
> Summary: pthread_create() 循环创建多个线程 主函数中使用pthread_exit替代sleep
> Author: xuelisheng
> Created Time: 2018年12月13日
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h> void *callBack(void *arg)
{
int i = (int)arg;
sleep(i);
// pthread_self():用来获得线程id。返回值为线程id,没有参数。
printf("i am %d th thread,getpid() = %d,pthread_self return value = %lu\n", i+, getpid(), pthread_self());
return NULL;
} int main()
{
pthread_t pid;
/*
pthread_create():成功返回值0,失败返回一个错误号(非0的值)。
参数1:pthread_t tid; 传出参数,指向线程标识符的指针。 &tid
参数2:属性 null
参数3:回调函数的函数指针。
参数4:回调函数的参数列表。没有的话传NULL
*/
int i;
for(i = ; i<; i++)
{
int ret = pthread_create(&pid,NULL,callBack, (void *)i);
if(ret != )
{
printf("pthread_create fail\n");
exit(-);
}
} //阻塞主线程一会
//sleep(i);
printf("i am main thread,getpid() = %d,pthread_self return value = %lu\n", getpid(), pthread_self());
// 这里使用pthread_exit替换sleep()
pthread_exit(NULL);
return ; // 主函数中使用return退出,相当于使用exit函数
}

运行结果:

i am main thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
i am th thread,getpid() = ,pthread_self return value =
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,488
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289