Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1490238
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: LINUX

2013-01-15 14:06:11

/*----------------------------------------------------------------------------------------
实现功能: 开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,
要求输出结果必须按ABC的顺序显示,如ABCABC....依次递推。


#编译和运行
gcc -lpthread -o thread1 thread1.c
./thread1


#调试信息
thread 1 waiting, n = 0
thread 2 waiting, n = 0
A , -->n + 1 = 1
thread 0 waiting, n = 1
thread 1 wait success, n = 1
B , -->n + 1 = 2
thread 1 waiting, n = 2
thread 2 wait success, n = 2
C , -->n + 1 = 0
thread 2 waiting, n = 0
thread 0 wait success, n = 0
A , -->n + 1 = 1
thread 0 waiting, n = 1
thread 2 wait success, n = 1
thread 2 waiting, n = 1
thread 1 wait success, n = 1
B , -->n + 1 = 2
thread 1 waiting, n = 2
thread 0 wait success, n = 2
thread 0 waiting, n = 2
thread 2 wait success, n = 2
C , -->n + 1 = 0
thread 2 waiting, n = 0
thread 1 wait success, n = 0
thread 1 waiting, n = 0
thread 0 wait success, n = 0
A , -->n + 1 = 1
thread 0 waiting, n = 1
thread 2 wait success, n = 1
thread 2 waiting, n = 1
thread 1 wait success, n = 1
B , -->n + 1 = 2
thread 1 waiting, n = 2
thread 0 wait success, n = 2
thread 0 waiting, n = 2
thread 2 wait success, n = 2
C , -->n + 1 = 0
thread 1 wait success, n = 0
thread 1 waiting, n = 0
thread 0 wait success, n = 0
A , -->n + 1 = 1
thread 0 waiting, n = 1
thread 2 waiting, n = 1
thread 1 wait success, n = 1
B , -->n + 1 = 2
thread 1 waiting, n = 2
thread 0 wait success, n = 2
thread 0 waiting, n = 2
thread 2 wait success, n = 2
C , -->n + 1 = 0
thread 2 waiting, n = 0
thread 1 wait success, n = 0
thread 1 waiting, n = 0
thread 0 wait success, n = 0
A , -->n + 1 = 1
thread 0 waiting, n = 1
thread 2 wait success, n = 1
thread 2 waiting, n = 1
thread 1 wait success, n = 1
B , -->n + 1 = 2
thread 1 waiting, n = 2
thread 0 wait success, n = 2
thread 0 waiting, n = 2
thread 2 wait success, n = 2
C , -->n + 1 = 0
thread 2 waiting, n = 0
thread 1 wait success, n = 0
thread 1 waiting, n = 0
thread 0 wait success, n = 0
A , -->n + 1 = 1
thread 0 waiting, n = 1
thread 2 wait success, n = 1
thread 2 waiting, n = 1
thread 1 wait success, n = 1
B , -->n + 1 = 2
thread 1 waiting, n = 2
thread 0 wait success, n = 2
thread 0 waiting, n = 2
thread 2 wait success, n = 2
C , -->n + 1 = 0
thread 2 waiting, n = 0
thread 1 wait success, n = 0
thread 1 waiting, n = 0
thread 0 wait success, n = 0
A , -->n + 1 = 1
thread 2 wait success, n = 1
thread 2 waiting, n = 1
thread 0 waiting, n = 1
thread 1 wait success, n = 1
B , -->n + 1 = 2
thread 1 waiting, n = 2
thread 2 wait success, n = 2
C , -->n + 1 = 0
thread 2 waiting, n = 0
thread 0 wait success, n = 0
A , -->n + 1 = 1
thread 2 wait success, n = 1
thread 2 waiting, n = 1
thread 0 waiting, n = 1
thread 1 wait success, n = 1
B , -->n + 1 = 2
thread 1 waiting, n = 2
thread 2 wait success, n = 2
C , -->n + 1 = 0
thread 2 waiting, n = 0
thread 0 wait success, n = 0
A , -->n + 1 = 1
thread 2 wait success, n = 1
thread 2 waiting, n = 1
thread 0 waiting, n = 1
thread 1 wait success, n = 1
B , -->n + 1 = 2
thread 1 waiting, n = 2
thread 2 wait success, n = 2
C , -->n + 1 = 0
thread 2 waiting, n = 0
thread 0 wait success, n = 0
A , -->n + 1 = 1
thread 1 wait success, n = 1
B , -->n + 1 = 2
thread 2 wait success, n = 2
C , -->n + 1 = 0

-----------------------------------------------------------------------------------------*/

#include
#include
#include
#include
#include

#define THREADNUM 3 //进程数量
#define DEBUGP printf

//------------------------------------------------------------------------

int n = 0; //当前工作的线程编号
pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t qready  = PTHREAD_COND_INITIALIZER;

void *thread_func(void *arg)
{
  int param = (int)arg; //获取当前线程参数
  char cID = 'A' + param;
  int ret,i=0;

  for (i=0; i<10; i++) {
    pthread_mutex_lock(&mylock);
    //非工作线程则等待
    while (param != n) {
      DEBUGP("thread %d waiting, n = %d\n", param, n);

      ret = pthread_cond_wait(&qready, &mylock);
      if (ret == 0) {
      DEBUGP("thread %d wait success, n = %d\n", param, n);
      }
      else {
      DEBUGP("thread %d wait failed:%s\n", param, strerror(ret));
      }
    }
    printf("%c ", cID);

    n = (n+1) % THREADNUM;
    DEBUGP(", -->n + 1 = %d\n", n);
    pthread_mutex_unlock(&mylock);

    pthread_cond_broadcast(&qready);
  }

  return (void *)0;
}


int main(int argc, char** argv)
{
  int i = 0, err;
  pthread_t tid[THREADNUM];
  //void *tret;

  for(i=0; i   //创建时定义线程的参数 (void *)i 以方便 thread_func 中分析判断
    err = pthread_create(&tid[i], NULL, thread_func, (void *)i);
    if(err != 0) {
      printf("thread_create error:%s\n",strerror(err));
      exit(-1);
    }
  }

  for (i=0; i     //err = pthread_join(tid[i], &tret);
    err = pthread_join(tid[i], NULL);
    if (err != 0) {
      printf("can not join with thread %d:%s\n", i,strerror(err));
      exit(-1);
    }
  }
  printf("\n");

  return 0;
}

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