动态网站制作指南
[  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
电脑书籍下载:程序设计书籍 | 数据库教程书籍 | 平面与多媒体书籍 | 网络通讯书籍 | 系统管理书籍 | 网络安全书籍 | 认证考试书籍
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > 软件工程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ 软件工程 ]的信息

本月文章推荐
.在运行时使用 UDDI.
.Tor 技术和 Torpark 浏览器.
.如何编写高质量软件需求说明书.
.走出一般性的建模误区.
.多相晶粒图像分析中复杂晶界的提.
.界面需求的分析方法.
.品质管理简介.
.软件开发中项目需求管理简述.
.深入探讨.NET中的钩子技术.
.浅谈Object Pascal的指针.
.软件项目在变化的需求中获得成功.
.谈谈软件开发中的调研对象与被调.
.对.Net事件委托的深入分析.
.游戏引擎剖析(九).
.用UML描述工作流管理(1).
.关于异常的使用心得.
.微软加强面向服务架构SOA开发.
.Windows工作流活动技术概览.
.解析UML的动态建模机制.
.领域模型驱动设计(DDD)之模型提炼.

Linux编程之多线程常用函数使用实例

文章类别:软件工程 | 发表日期:2008-3-23 |



  Paul Chen     2006-03-16         BeiJing

//以下代码均来自《Linux C 编程》一书,本人对其中代码做了一些改进,使之更有利于系统学习GNU C标准库中Pthread。

//非凡注重:makefile中的编译选项中一定要包含-lpthread(即连接pthread库)

makefile:

demo:demo1.o
    gcc -o demo demo1.o -lpthread
   
demo1.o:demo1.c   
    gcc -c demo1.c
   
clean:
    rm demo demo1.c   


demo1.c:

#include <stdio.h>
#include <pthread.h>

//used in test1
pthread_once_t once=PTHREAD_ONCE_INIT;

//used in test2
pthread_mutex_t mutex;
pthread_cond_t cond;

//used in test3
pthread_key_t key; 

void once_run(void)
{
  printf("once_run in thread %d\n",pthread_self());
}

void * child1(void *arg)
{
  int tid=pthread_self();
  printf("thread child1 %d enter\n",tid);
  pthread_once(&once,once_run);
  //sleep(3);
  printf("thread child1 %d returns\n",tid);
}

void * child2(void *arg)
{
  int tid=pthread_self();
  printf("thread child2 %d enter\n",tid);
  pthread_once(&once,once_run);
  //sleep(3);
  printf("thread child2 %d returns\n",tid);
}

void * child3(void *arg)
{
   pthread_cleanup_push(pthread_mutex_unlock,&mutex); /* comment 1 */
   while(1){
      printf("thread child3 get running \n");
      printf("thread child3 pthread_mutex_lock returns %d\n",
      pthread_mutex_lock(&mutex));
      pthread_cond_wait(&cond,&mutex);
      printf("thread child3 condition applied\n");
      pthread_mutex_unlock(&mutex);
      sleep(5);
   } 
   pthread_cleanup_pop(0); /* comment 2 */
}

void *child4(void *arg)
{
   while(1){
      sleep(3); /* comment 3 */
      printf("thread child4 get running.\n");
    
  printf("thread child4 pthread_mutex_lock returns %d\n",
      pthread_mutex_lock(&mutex));
      pthread_cond_wait(&cond,&mutex);
      printf("thread child4 condition applied\n");
      pthread_mutex_unlock(&mutex);
      sleep(1);
   }
}

void echomsg(int t)
{
   printf("destrUCtor excuted in thread %d,param=%d\n",pthread_self(),t);
}

void * child5(void *arg)
{
    int tid=pthread_self();
    printf("thread child5 %d enter\n",tid);
    pthread_setspecific(key,(void *)tid);
    sleep(2);
    printf("thread child5 %d returns %d\n",tid,pthread_getspecific(key));
    sleep(5);
}

void * child6(void *arg)
{
    int tid=pthread_self();
    printf("thread child6 %d enter\n",tid);
    pthread_setspecific(key,(void *)tid);
    sleep(1);
    printf("thread child6 %d returns %d\n",tid,pthread_getspecific(key));
    sleep(5);
}

int main(void)
{
  int tid1,tid2,tid3,tid4,tid5,tid6;
  printf("hello\n");
 
  printf("\n\n\n");
 
  //test1:pthread_once,pthread_self
  pthread_create(&tid1,NULL,child1,NULL);
  pthread_create(&tid2,NULL,child2,NULL);
  pthread_join(tid1,NULL);
  pthread_join(tid2,NULL);
 
  printf("\n\n\n");

  //test3:pthread_key_create,pthread_setspecific,pthread_getspecific
  pthread_key_create(&key,echomsg);
  pthread_create(&tid5,NULL,child5,NULL);
  pthread_create(&tid6,NULL,child6,NULL);
  pthread_join(tid5,NULL);
  pthread_join(tid6,NULL); 
  pthread_key_delete(key);  
 
  printf("\n\n\n");
   
  //test2:pthread_mutex_lock,pthread_mutex_unlock,pthread_cond_wait,pthread_cleanup_pop \
          pthread_cond_signal,pthread_exit
  pthread_create(&tid3,NULL,child3,NULL);
  pthread_create(&tid4,NULL,child4,NULL);
  pthread_join(tid3,NULL);
  pthread_join(tid4,NULL);
  do{
     sleep(2); /* comment 4 */
     pthread_cancel(tid3); /* comment 5 */
 
    sleep(2); /* comment 6 */
     pthread_cond_signal(&cond);
  }while(1);
  sleep(100);
  pthread_exit(0);
 
  printf("\n\n\n");
 
  printf("main thread exit\n");
  return 0;
}

上一篇:语音识别系统中增加图像识别技术的设计 人气:189
下一篇:.NET下基于组件的分布式系统动态配置 人气:164
点击此处浏览全部软件工程的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-5-16 乘风多用户PHP统计系统 v3.4
2008-5-16 轩溪下载系统 v3.78 build 0515
2008-5-16 普沙B2B 浙江省商贸网 v2.0
2008-5-16 asp抓蜘蛛的小程序 v1.0
2008-5-16 齐齐乐网私服发布站 仿haosf新版
2008-5-16 IssTech信息反馈系统 v1.0
2008-5-16 自由领域大头贴(js接口版) 修正版
2008-5-16 医院网站系统
2008-5-16 智拓-分类信息管理系统 v5.0
2008-5-7 Windows XP SP3 官方英文版
2008-5-7 Windows XP SP3 官方香港中文版
2008-5-7 Windows XP SP3 官方繁体中文版
2008-5-7 Windows XP SP3 官方简体中文版
2008-4-30 Multiple Unzip Wizard 1.02
2008-4-30 Multiple Unrar Wizard 1.0.0
2008-4-30 WinZip Install/Try/Uninstall a
2008-4-30 ZIP压缩文件修复器WzipFix 2.0
2008-4-30 Pentazip 6.01 Build 189 For Wi
  发表评论
姓 名: 验证码: [ 全部贴吧 ] [ 浏览评论 ]
内 容:
[ 汉字翻译拼音 ] [ 广告代码 ] [ 符号对照表 ] [ 进制转换 ] [ 经典小工具 ] [ 个税计算 ] [ 汉字简繁转换 ] [ 普通单位换算 ] [ 公制单位换算 ]
[ 生辰老黄历 ] [ 国内电话区号 ] [ 国家代码与域名缩写 ] [ 文字加密解密 ] [ 健康查询 ] [ 万年历 ] [ 手机号码查询 ] [ ip搜索 ] [ Google PR查询 ]
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報