当前位置: 首页 > news >正文

建站公司论坛线上推广员是做什么的

建站公司论坛,线上推广员是做什么的,安徽城乡建设 厅网站,手机网站制作工具线程退出时自动释放资源 今天碰到一个问题:主线程pthread_create一个子线程A,子线程pthread_mutex_lock,然后调用其他的函数fun,最后从fun返回后再pthread_mutex_unlock. 但是如果在fun中调用了pthread_exit异常退出,那么岂不是没释放锁就退出了,这肯定会引起死锁. 解决办法一…线程退出时自动释放资源 今天碰到一个问题:主线程pthread_create一个子线程A,子线程pthread_mutex_lock,然后调用其他的函数fun,最后从fun返回后再pthread_mutex_unlock. 但是如果在fun中调用了pthread_exit异常退出,那么岂不是没释放锁就退出了,这肯定会引起死锁. 解决办法一: 在fun中调用pthread_exit之前都先调用pthread_mutex_lock释放锁, 但是这就需要吧mutex作为参数传给fun,如果fun再调用了其他函数,就得一层一层的把mutex传下去, 而且要找到fun及其调用的函数中的pthread_exit然后再修改是很麻烦的,如果fun是一个第三方程序,而且退出是调用了exit而不是pthread_exit,那就更麻烦了. 解决办法二: POSIX线程API中提供了一个pthread_cleanup_push()/pthread_cleanup_pop()函数对用于自动释放资源. 从pthread_cleanup_push()的调用点到pthread_cleanup_pop()之间的程序段中的终止动作包括调用 pthread_exit()和取消点终止都将执行pthread_cleanup_push()所指定的清理函数 API定义如下: void pthread_cleanup_push(void (*routine) (void *), void *arg) void pthread_cleanup_pop(int execute) 代码示例如下: #include stdio.h #include stdlib.h #include unistd.h #include pthread.h#define THREAD_NUMBER 2 pthread_mutex_t mutex;void aa() {pthread_exit(NULL); }void* hello1(void *arg) { char *hello_str (char *)arg;int oldtype;pthread_detach (pthread_self ()); pthread_cleanup_push(pthread_mutex_unlock, (void *) mutex);pthread_mutex_lock(mutex);sleep(2);printf(%s\n, hello_str);aa();pthread_mutex_unlock(mutex);pthread_cleanup_pop(0); }void* hello2(void *arg) {char *hello_str (char *)arg;pthread_detach (pthread_self ()); pthread_mutex_lock(mutex);sleep(1);printf(%s\n, hello_str);pthread_mutex_unlock(mutex); }int main(int argc, char *argv[]) {int i;int ret_val;pthread_t pt[THREAD_NUMBER];const char *arg[THREAD_NUMBER];arg[0] hello world from thread1;arg[1] hello world from thread2;pthread_mutex_init(mutex,NULL);printf(Begin to create threads...\n);ret_val pthread_create(pt[0], NULL, hello1, (void *)arg[0]);if (ret_val ! 0 ) {printf(pthread_create error!\n);exit(1);}ret_val pthread_create(pt[1], NULL, hello2, (void *)arg[1]);if (ret_val ! 0 ) {printf(pthread_create error!\n);exit(1);}sleep(5);printf(Now, the main thread returns.\n);return 0; }$ gcc -o a.out test.c -lpthread $ ./a.out Begin to create threads… hello world from thread1 hello world from thread2 Now, the main thread returns. 可以看出hello1()-aa()-pthread_exit(),当线程hello1退出后锁已经释放了 其实不光是释放锁,还可以释放其他资源. 当然上述pthread_cleanup_push()/pthread_cleanup_pop()是有缺陷的, 比如线程处于PTHREAD_CANCEL_ASYNCHRONOUS状态上述代码段就有可能出错 因为CANCEL事件有可能在 pthread_cleanup_push()和pthread_mutex_lock()之间发生或者在 pthread_mutex_unlock()和pthread_cleanup_pop()之间发生从而导致清理函数unlock一个并没有加锁的 mutex变量造成错误。 因此在使用清理函数的时候都应该暂时设置成PTHREAD_CANCEL_DEFERRED模式。为此POSIX的 Linux实现中还提供了一对不保证可移植的 pthread_cleanup_push_defer_np()/pthread_cleanup_pop_defer_np()扩展函数
http://www.pierceye.com/news/825732/

相关文章:

  • 九龙坡区网站建设外贸是什么工作
  • 贵州省住房和城乡建设厅网站报名网网站开发入职转正申请书
  • 外贸平台哪个网站好做dede网站白屏
  • 可信的手机网站建设服装网站ui设计
  • 江苏网站建设效果好技术支持 英铭网站建设
  • 很多网站开发没有框架如何制作的网站模板制作与安装教程视频教程
  • 小说网站建设目的360如何做网站
  • 永安市住房与城乡建设局网站腾讯邮箱企业邮箱入口登录
  • 手机和wap网站建设wordpress链接 数据库
  • 1688网站简介青岛网站建设系统
  • 优秀网站的特点wordpress 腾讯云oss
  • 深圳专业做网站公司怎么做网站跳转
  • 设计教学网站推荐重庆大渝网
  • 网站建设询价邀请函二级建造师报名的官网
  • 沈阳个人网站建设表白网站制作软件
  • 开封+网站建设+网络推广网站建设及托管合同模板
  • 怎么看公司网站建设的时间苏州展厅设计公司排名
  • 新电商平台电脑优化软件哪个好用
  • 个人可以建网站咨询公司简介
  • 关于网站开发怎么找到做外贸的国内公司
  • 国外优秀摄影作品网站互联网项目推广方案
  • icp网站备案查询美容整形网站建设
  • 广州网站建设八爪鱼湖南网络公司排名
  • 网站公司名称大全龙元建设集团有限公司网站
  • 网站开发技术可行性分析邢台建设企业网站
  • 有一个网站 人物模型可以做各种动作沈阳军成网站建设
  • h5移动网站开发南京企业自助建站系统
  • 开发一个企业网站报价wordpress副标题字数
  • 芜湖哪家公司做网站不错江宁网站建设方案
  • 做网站公司赚钱吗?百度搜索下载