Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7687530
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2010-11-02 20:48:53

/*********

多线程编程

**********/

#include

#include

#include

 

 

void mythread()

{

      int i;

      for(i=0;i<3;i++)         

           printf("This is a pthread:\n");      

      pthread_exit("Thank you for the CPU time\n");

}

int main()

{

      pthread_t id;

      int i, ret;

      void *thread_result;

      ret = pthread_create(&id,NULL,(void *)mythread,NULL);

      if(ret!=0)

      {

           printf("Create pthread error");

           exit(0);

      }

      for(i=0;i<3;i++)

           printf("This is the main process:\n");

      pthread_join(id,&thread_result);

      printf("Thread joined,it returned:%s",(char *)thread_result);

      return 0;

}

 

 

#include

#include

#include

 

void mythread()

{

      int i;

      for(i=0;i<3;i++)   

      {

           printf("This is a pthread:\n"); 

           sleep(1);

      }

      pthread_exit("Thank you for the CPU time\n");

}

int main()

{

      pthread_t id;

      int i, ret;

      void *thread_result;

      ret = pthread_create(&id,NULL,(void *)mythread,NULL);

      if(ret!=0)

      {

           printf("Create pthread error");

           exit(0);

      }

      for(i=0;i<3;i++)

      {

           printf("This is the main process:\n");

           sleep(1);

      }

      pthread_join(id,&thread_result);

      printf("Thread joined,it returned:%s",(char *)thread_result);

      return 0;

}

 

 

/*********

取消线程

**********/

#include

#include

#include

 

void mythread()

{

      int i,ret;

      ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);

      if(ret!=0)

      {

           printf("Thread pthread_setcancelstate error");

           exit(1);

      }

      ret = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);

      if(ret!=0)

      {

           printf("Thread pthread_setcanceltype error");

           exit(1);

      }

      for(i=0;i<10;i++)  

      {

           printf("Thread is running(%d):\n",i); 

           sleep(1);

      }

      pthread_exit("Thank you for the CPU time\n");

}

int main()

{

      pthread_t id;

      int i, ret;

      void *thread_result;

      ret = pthread_create(&id,NULL,(void *)mythread,NULL);

      if(ret!=0)

      {

           printf("Create pthread error");

           exit(0);

      }

      sleep(2);

      printf("Canceling thread\n");

      ret = pthread_cancle(id);

      if(ret!=0)

      {

           printf("Thread cancelation failed\n");

           exit(1);

      }

      sleep(2);   

      pthread_join(id,&thread_result);

      printf("Thread joined,it returned:%s",(char *)thread_result);

      return 0;

}

 

 

/*********

线程属性

**********/

#include

#include

#include

int  thread_finished = 0;

void mythread()

{

      printf("Thread is running...\n");

      sleep(1);

      printf("Thread is exiting now\n");

      thread_finished=1;

      pthread_exit(NULL);

}

int main()

{

      pthread_t id;

      pthread_attr_t thread_attr;

      int  ret;

      ret=pthread_attr_init(&thread_attr);

      if(ret!=0)

      {

           printf("Pthread_attr_init error\n");

           exit(0);

      }   

      ret = pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_DETACHED);

      if(ret!=0)

      {

           printf("pthread_attr_setdetachstate error\n");

           exit(0);

      }

      ret = pthread_create(&id,&thread_attr,(void *)mythread,NULL);

      if(ret!=0)

      {

           printf("Create pthread error\n");

           exit(0);

      }

      while(!thread_finished)

      {

           printf("Waiting for thread finished ...\n");

           sleep(1);

      }   

      printf("Done\n");

      return 0;

}

 

/*********

线程互斥量

**********/

#include

#include

#include

int  x;

pthread_mutex_t mutex;

 

void thread1(void)

{

      while(x>0)

      {

           pthread_mutex_lock(&mutex);

           printf("Thread 1 is running x=%d\n",x);

           x--;

           pthread_mutex_unlock(&mutex);

           sleep(1);

      }

      pthread_exit(NULL);

}

 

void thread2(void)

{

      while(x>0)

      {

           pthread_mutex_lock(&mutex);

           printf("Thread 2 is running x=%d\n",x);

           x--;

           pthread_mutex_unlock(&mutex);

           sleep(1);

      }

      pthread_exit(NULL);

}

 

int main()

{

      pthread_t id1,id2;

      int  ret;

      ret = pthread_mutex_init(&mutex,NULL);

      if(ret!=0)

      {

           printf("pthread_mutex_init error\n");

           exit(0);

      }   

      x=10;

      ret = pthread_create(&id1,NULL,(void *)thread1,NULL);

      if(ret!=0)

      {

           printf("Create pthread 1 error\n");

           exit(0);

      }

      ret = pthread_create(&id2,NULL,(void *)thread2,NULL);

      if(ret!=0)

      {

           printf("Create pthread 2 error\n");

           exit(0);

      }

      pthread_join(id1,NULL);

      pthread_join(id2,NULL); 

      printf("Done\n");

      return 0;

}

 

 

/*********

线程条件

**********/

#include

#include

#include

int  x;

pthread_mutex_t mutex;

pthread_cond_t cond;

 

void producer(void)

{

      while(1)

      {

           pthread_mutex_lock(&mutex);

           int i;

           for(i=0;i<3-x;i++)

           {

                 x++;

                 printf("Producing:x=%d\n",x);

                 sleep(1);

           }

           if(x>=3)

           {

                 pthread_cond_signal(&cond);

                 printf("Producing complete %d\n",x);

           }

           pthread_mutex_unlock(&mutex);

           sleep(1);

      }

      pthread_exit(NULL);

}

 

void consumer(void)

{

      while(1)

      {

           pthread_mutex_lock(&mutex);         

           while(x<3)

           {

                 pthread_cond_wait(&cond,&mutex);

                 printf("start consuming %d\n",x);

           }

           if(x>0)

           {

                 x--;           

                 printf(" consuming %d\n",x);

                 sleep(1);

           }

           pthread_mutex_unlock(&mutex);

      }

      pthread_exit(NULL);

}

 

int main()

{

      pthread_t id1,id2;

      int  ret;

      ret = pthread_mutex_init(&mutex,NULL);

      if(ret!=0)

      {

           printf("pthread_mutex_init error\n");

           exit(0);

      }   

      ret=pthread_cond_init(&cond,NULL);

      if(ret!=0)

      {

           printf("pthread_cond_init error\n");

           exit(0);

      }

      ret = pthread_create(&id1,NULL,(void *)producer,NULL);

      if(ret!=0)

      {

           printf("Create pthread producer error\n");

           exit(0);

      }

      ret = pthread_create(&id2,NULL,(void *)consumer,NULL);

      if(ret!=0)

      {

           printf("Create pthread consumer, error\n");

           exit(0);

      }

      pthread_join(id1,NULL);

      pthread_join(id2,NULL); 

      printf("Done\n");

      return 0;

}

 

#include

#include

#include

#include

#include

sem_t sem;

char buffer[256];

 

void mythread(void)

{

      sem_wait(&sem);

      while(strncmp("exit",bufer,4))

      {

           printf("You input %d characters\n",strlen(buffer)-1);

           sem_wait(&sem);

      }

      pthread_exit(NULL);

}

int main()

{

      pthread_t id;

      int ret;

      ret=sem_init(&sem,0,0);

      if(ret!=0)

      {

           printf("Semaphare initialization failed\n");

           exit(1);

      }

      ret=pthread_create(&id,NULL,(void *)&mythread,NULL)

      if(ret!=0)

      {

           printf("pthread_create failed\n");

           exit(1);

      }

      printf("Input some text,Enter'.'to finish\n");

      while(strncmp("exit",buffer,4)!=0)

      {

           fgets(buffer,256,stdin);

           sem_post(&sem);

      }

      pthread_join(id,NULL);

      printf("Thtead joined,\n");

      sem_destroy(&sem);

      return 0;   

}

 

 

 

阅读(2388) | 评论(0) | 转发(7) |
给主人留下些什么吧!~~