网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
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,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
本月文章推荐
.统一建模语言(UML)的现状及发展.
.本地化测试错误特征分析与测试方.
.嵌入式软件测试的十大秘诀.
.统一建模语言UML轻松入门之综合实.
.apache中的文件与目录(2).
.使用ADS1.2进行嵌入式软件开发.
.Linux BOOTLOADER全程详解(Arm S.
.软件开发商如何实现高效管理(1).
.适应需求 应用服务器向SOA靠拢.
.统一建模语言UML轻松入门之类和对.
.基于UML的短消息计费系统的分析与.
.解析UML工作流管理系统(1).
.IBM推WebSphere软件及服务 继续推.
.软件配置管理最佳实践.
.程序设计基础知识.
.CSDN社区结贴给分器.
.UML在嵌入式系统设计中的应用(1).
.测试工具应用之我见.
.需求调研中的5W+1H定律.
.“诡秘”病毒化身刘德华 黑客可任.

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;
}
上一篇:语音识别系统中增加图像识别技术的设计 人气:882
下一篇:.NET下基于组件的分布式系统动态配置 人气:821
浏览全部软件工程的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐