C语言

c语言线程终止练习示例

时间:2023-04-01 19:03:23 C语言 我要投稿
  • 相关推荐

c语言线程终止练习示例

  c语言线程

  代码如下:

  #include

  #include

  #include

  void *t1(void *args)

  {

  return (void *) 0;

  }

  void *t2(void *args)

  {

  printf("thread 2 param[args] = %dn", args);

  pthread_exit((void *) 3);

  }

  void *t3(void *args) {

  while(1)

  {

  printf("thread 3 is workingn");

  sleep(1);

  }

  }

  int main(int argc, char *argv[])

  {

  pthread_t thread;

  int err;

  void *status;

  printf("creating thread 1n");

  err = pthread_create(&thread, NULL, t1, NULL);

  if(err)

  {

  printf("Can not created thread 1n");

  exit(-1);

  }

  pthread_join(thread, &status);

  printf("thread 1 exit return code %dnn", status);

  printf("creating thread 2n");

  err = pthread_create(&thread, NULL, t2, (void *) 9);

  if(err)

  {

  printf("Can not created thread 2n");

  exit(-2);

  }

  pthread_join(thread, &status);

  printf("thread 2 exit return code %dnn", status);

  printf("creating thread 3n");

  err = pthread_create(&thread, NULL, t3, NULL);

  if(err)

  {

  printf("Can not created thread 3n");

  exit(-3);

  }

  sleep(10);

  pthread_cancel(thread);

  pthread_join(thread, &status);

  printf("thread 3 exit return code %dn", status);

  return 1;

  }

【c语言线程终止练习示例】相关文章:

C语言模拟试题练习12-09

计算机二级C语言模拟练习题10-14

怎样学习c++c语言编程04-28

计算机二级C语言考试冲刺练习题11-15

计算机二级《C语言》基础练习题及答案02-06

C语言的基本构成12-05

c语言的优点介绍04-13

C语言试题训练10-20

C语言入门知识07-20

C语言的编码规范02-10