pthreadcreate用法(pthread_create讲解)
pthread_create,传两个参数,在函数里面怎么设置?
涉及多参数传递给线程的,都需要使用结构体将参数封装后,将结构体指针传给线程
定义一个结构体
struct mypara
{
var para1;//参数1
var para2;//参数2
}
将这个结构体指针,作为void *形参的实际参数传递
struct mypara pstru;
pthread_create(ntid, NULL, thr_fn, (pstru));
函数中需要定义一个mypara类型的结构指针来引用这个参数
void *thr_fn(void *arg)
{
mypara *pstru;
pstru = (* struct mypara) arg;
pstru-para1;//参数1
pstru-para2;//参数2
}
c语言怎么创建线程和使用
1、添加线程相关的头文件:#includepthread.h
2、线程创建函数是pthread_create()函数,该函数的原型为:
int?pthread_create(pthread_t?*thread,pthread_attr_t?*attr,void*?(*start_routine)(void*),void?*arg);
3、线程退出函数是pthread_exit()函数,该函数的原型为:
void?pthread_exit(void?*retval);
创建线程的示例程序如下:
/*
**程序说明:创建线程函数pthread_create()函数的使用。
*/
#include?stdio.h
#include?pthread.h
#include?unistd.h
#include?stdlib.h
#include?string.h
//打印标识符的函数
void?print_ids(const?char?*str)
{
pid_t?pid; //进程标识符
pthread_t?tid; //线程标识符
pid=getpid(); //获得进程号
tid=pthread_self(); //获得线程号
printf("%s?pid:%u?tid:%u?(0x%x)\n",
str,(unsigned?int)pid,(unsigned?int)tid,(unsigned?int)tid);?//打印进程号和线程号
}
//线程函数
void*?pthread_func(void?*arg)
{
print_ids("new?thread:"); //打印新建线程号
return?((void*)0);
}
//主函数
int?main()
{
int?err;
pthread_t?ntid; //线程号
err=pthread_create(ntid,NULL,pthread_func,NULL); //创建一个线程
if(err?!=?0)
{
printf("create?thread?failed:%s\n",strerror(err));
exit(-1);
}
print_ids("main?thread:"); //打印主线程号
sleep(2);
return?0;
}
linux下如何使用pthread_create创建2线程
printf("1\n");
printf("2\n");
不可能不执行吧,除非你的程序其它地方有问题,比如juzhen1或juzhen2有问题,导致程序挂掉了
还有顺便说一句,你这个时间统计根本就是创建两个线程的所消耗的时间,而不是这两个线程执行的时间,如果你要统计这两个线程执行时间,你应该在
pthread_join(pid1, NULL);
pthread_join(pid2, NULL);
end=clock();
这样只有在两个线程执行完后才会执行end=clock();这一句
还有 pthread_t pid1, pid2这两句最好改成 pthread_t tid1, tid2;要学会良好的变量命名习惯,这样对你以后有好处。
linux c编程中关于ptheread_create 的用法解释
只是强制转换返回值类型.
int?pthread_create(pthread_t?*tidp,const?pthread_attr_t?*attr,void?*(*start_rtn)(void),void?*arg)
第三个参数接收一个函数的地址
按普通变量地址应该这样调用
pthread_create(id1,NULL,(void*)(mythread),NULL);
注意取地址符
但是函数比较特殊,
函数名本来就代表了函数的入口地址。
例如下面这段代码:
??
#include?stdio.h?
#include?string.h?
#include?stdlib.h?
?
void?test()?
{?
? ? printf("test\n");?
}?
?
int?main(int?argc,?char?*argv[])?
{?
? ? printf("%p\n",test);?
? ? printf("%p\n",test);?
}
输出结果为:
0x8048414
0x8048414
可见,其实两者是一样的,都指向着这个函数的入口地址。
所以可以直接不加
而前面的(void?*)只是函数传参数时的一个强制转换,是关于返回值类型的
Pthread线程使用详解
文中先讲解函数,再运行实例,以及一些注意事项。
函数 pthread_create ,使用 man 3 pthread_create 查看介绍。
函数描述:
通过 pthread_create 创建的新线程,有收下四种方法退出线程:
attr参数 是一个 pthread_attr_t 结构体,它在线程被创建时被用来设定新线程的属性。这个结构体的初始化是通过 pthread_attr_init() 函数。如果该参数为空,那么新线程会使用默认的属性参数。
在 pthread_create 函数调用返回之前,新线程的内存指针会赋给 thread 参数,表示线程的ID,这个ID的作用是在后续可以调用针对该线程的其它pthreads函数。
返回值
成功返回0,失败返回一个错误编号,同时 thread 参数也不会被赋值。
函数描述
pthread_join 函数会等待指定的线程结束,如果指定的线程已经线束,那么它会立即返回。指定的线程必须是joinable的。也就是说, pthread_join() 函数会 一直阻塞调用线程,直到指定的线程tid终止 。当 pthread_join() 返回之后,应用程序可回收与已终止线程关联的任何数据存储空间 ,(另外也可设置线程attr属性,当线程结束时直接回收资源)如果没有必要等待特定的线程终止之后才进行其他处理,则应当将该线程分离 pthread_detach() 。
如果 retval 不为空,那么该函数会拷贝退出状态值到 retval 指向的内存中,如果目标thread被cancel了, retval 的值为 PTHREAD_CANCELED
返回值
成功返回0,错误返回错误码
linux 下pthread_create使用的问题
首先:你要知道,线程参数的类型为void * ,但真正传给线程里面的参数不限,可以是int、char 、char *、struct 等等,只要在传给pthread_create函数时强制为void *类型就不会有警告了。在线程处理函数内需要时再强制为你需要的类型即可。
至于你的问题:
1. 在你的这个程序中你确实可以这么理解:这个参数的实际类型由你调用pthread_create函数时传进来的参数决定。
2. message = (char *)ptr; 的意思是:把参数ptr强制为char *(即字符串)类型。(char *)是强制类型转换运算符标志,使用原型为:(elementType) element。这里的elementType代表参数类型,比如int、char、char * 等都可以;而element则表示你要强制为elementType类型的变量。
不知道我这么说你明白了吗