c语言线程终止练习示例

时间:2025-12-05 01:17:00 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语言基本语法示例11-02

C语言练习02-14

C语言的基础练习10-25

C语言作业练习10-02

C语言练习试题10-20

c语言实现多线程动画程序范例10-29

C语言socket编程开发应用示例02-25

C语言计算日期差的方法示例09-17

C语言练习题09-11