文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C 多线程编程

C 多线程编程

时间:2010-11-02  来源:luozhiyong131

/*********

多线程编程

**********/

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

 

 

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 <stdio.h>

#include <stdlib.h>

#include <pthread.h>

 

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 <stdio.h>

#include <stdlib.h>

#include <pthread.h>

 

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 <stdio.h>

#include <stdlib.h>

#include <pthread.h>

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 <stdio.h>

#include <stdlib.h>

#include <pthread.h>

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 <stdio.h>

#include <stdlib.h>

#include <pthread.h>

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 <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <pthread.h>

#include <semaphore.h>

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;   

}

 

 

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载