青岛免费建网站,windows优化大师官方下载,青岛seo关键词,建设部的网站文章目录pthread_equal()函数原型参数返回值线程参数传递传递指针优点注意事项数据生命周期管理多线程并发访问传递整型参数方式一#xff1a;直接强制转换方式2#xff1a;使用 intptr_t / uintptr_t#xff08;C99标准推荐#xff09;方式3#xff1a;为单个整数动态分配…文章目录pthread_equal()函数原型参数返回值线程参数传递传递指针优点注意事项数据生命周期管理多线程并发访问传递整型参数方式一直接强制转换方式2使用 intptr_t / uintptr_tC99标准推荐方式3为单个整数动态分配内存传递复杂数据方式1定义结构体方式2为每个线程动态分配独立结构传参选择依据pthread_equal()POSIX标准只定义了线程相关数据类型的接口行为没有规定其内部实现。在不同的操作系统或架构上pthread_t 可能是unsigned longLinux x86_64、结构体指针某些BSD系统、整数索引等互斥锁、条件变量等可能有不同的内部布局pthread_equal() 是 Pthreads 标准中专门用于比较两个线程 IDpthread_t 类型是否相等的函数解决了 pthread_t 类型跨平台兼容性问题避免直接用 比较的风险“不透明”的数据类型数据类型描述pthread_t线程 IDpthread_mutex_t互斥对象mutexpthread_mutexattr_t互斥属性对象pthread_cond_t条件变量condition variablepthread_condattr_t条件变量的属性对象pthread_key_t线程特有数据的键Keypthread_once_t一次性初始化控制上下文control contextpthread_attr_t线程的属性对象函数原型#includepthread.hintpthread_equal(pthread_tt1,pthread_tt2);参数要比较的线程 ID如pthread_self()返回值、pthread_create输出的 ID返回值非 0 值t1 和 t2 指向同一个线程0两个线程 ID 不相等// 错误不可移植if(tid1tid2){...}// 正确可移植if(pthread_equal(tid1,tid2)!0){...}// 检查当前线程是否是特定线程if(pthread_equal(pthread_self(),main_thread_id)!0){// 这是主线程}线程参数传递线程参数传递主要涉及两种方式传递指针传递变量的地址或字符串常量地址传递整数值将整数值直接转换为指针传递传递指针#includepthread.h#includestdio.h#includestdlib.h#includeerrno.hvoid*func(void*arg){char*str(char*)arg;printf(%s\n,str);pthread_exit(NULL);}intmain(intargc,constchar*argv[]){pthread_ttid;char*strHello Thread!;intretpthread_create(tid,NULL,func,str);if(ret!0){errnoret;perror(pthread_create);exit(EXIT_FAILURE);}retpthread_join(tid,NULL);if(ret!0){errnoret;perror(pthread_create);exit(EXIT_FAILURE);}return0;}优点可以传递任意复杂的数据结构传递效率高只传指针不拷贝数据符合 void* 指针的设计初衷注意事项数据生命周期管理// 危险传递局部变量的地址voidcreate_thread(){charbuffer[64];// 栈上分配sprintf(buffer,Thread data);pthread_create(tid,NULL,func,buffer);// 函数返回时buffer被释放但线程可能还在访问}// - 这里buffer的栈内存被回收// 安全做法1传递全局/静态变量staticcharbuffer[64];// 或全局变量voidcreate_thread(){sprintf(buffer,Thread data);pthread_create(tid,NULL,func,buffer);}// 安全做法2动态分配voidcreate_thread(){char*buffermalloc(64);sprintf(buffer,Thread data);pthread_create(tid,NULL,func,buffer);// 在线程函数中必须free}// 安全做法3传递字符串常量只读段pthread_create(tid,NULL,func,Constant string);多线程并发访问// 危险多个线程共享同一数据指针intshared_data0;for(inti0;i5;i){// 所有线程都收到 shared_data 的指针pthread_create(tids[i],NULL,func,shared_data);}// 线程间会竞争修改shared_data需要同步机制传递整型参数方式一直接强制转换#includepthread.h#includestdio.h#includestdlib.h#includeerrno.hvoid*func(void*arg){printf(arg:%d\n,(int)arg);pthread_exit(NULL);}intmain(intargc,constchar*argv[]){pthread_ttid;inta10;intretpthread_create(tid,NULL,func,a);if(ret!0){errnoret;perror(pthread_create);exit(EXIT_FAILURE);}retpthread_join(tid,NULL);if(ret!0){errnoret;perror(pthread_create);exit(EXIT_FAILURE);}return0;}可以工作但是编译会报警告可移植性问题如果int和指针大小不同如int是32位指针是64位转换可能丢失精度类型安全问题绕过了类型系统编译器无法检查标准未定义C标准不保证这种转换的行为严格说来对于int与void*之间相互强制转换的后果C语言标准并未加以定义。不过大部分C语言编译器允许这样的操作并且也能达成预期的目的方式2使用 intptr_t / uintptr_tC99标准推荐#includestdint.h// 包含intptr_t的定义// 传递inta10;pthread_create(tid,NULL,func,(void*)(intptr_t)a);// 接收void*func(void*arg){intvalue(int)(intptr_t)arg;// 先转intptr_t再转int}C99标准定义的有符号整数类型intptr_t保证可以安全地存储指针值转换为整数再转回指针不变大小与指针相同解决了可移植性问题方式3为单个整数动态分配内存过渡设计但安全// 传递int*pmalloc(sizeof(int));*p10;pthread_create(tid,NULL,func,p);// 在线程函数中free(p)// 接收void*func(void*arg){intvalue*(int*)arg;free(arg);// 得释放// ...}传递复杂数据方式1定义结构体typedefstruct{intid;constchar*name;intpriority;void*device_handle;}ThreadParams;// 传递ThreadParams params{1,SensorReader,10,dev_handle};pthread_create(tid,NULL,sensor_thread,params);// 注意params必须是全局的或动态分配的// 如果params是局部变量确保线程在函数返回前已使用完数据方式2为每个线程动态分配独立结构for(inti0;iNUM_THREADS;i){ThreadParams*paramsmalloc(sizeof(ThreadParams));params-idi;params-namethread_names[i];params-prioritypriorities[i];pthread_create(tids[i],NULL,worker_thread,params);// 在线程函数中free(params)}传参选择依据数据类型推荐方法代码示例注意事项字符串常量直接传递pthread_create(..., string)只读安全单个整数intptr_t转换(void*)(intptr_t)value可移植简洁单个指针直接传递(void*)data注意生命周期多个参数结构体指针(void*)params结构体需全局或动态分配大量数据传递数据指针(void*)large_buffer考虑拷贝开销 vs 共享风险