百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

Linux C 编程 - 线程 linux c++ 线程

yuyutoo 2024-10-12 01:36 8 浏览 0 评论

  • 概念

线程库函数是由POSIX标准定义的,称为POSIX thread或者pthread。在Liunx上线程函数位于libpthread共享库中,因此在编译时要加上-lpthread选项。

  • 线程控制
  1. 创建线程
#include <pthread.h>
 
int pthread_create(pthread_t *restrict thread,
    const pthread_attr_t *restrict attr,
    void *(*start_routine)(void*), void *restrict arg);

返回值:成功返回0,失败返回错误号。以前学过的系统函数都是成功返回0,失败返回-1,而错误号保存在全局变量errno中,而pthread库的函数都是通过返回值返回错误号,虽然每个线程也都有一个errno, 但这是为了兼容其它函数接口而提供的,pthread库本身并没有使用它,通过返回值返回错误码更加清晰。

pthread_create成功返回后,新创建的线程id被填写到thread参数所指向的内存单元。进程id的类型是pid_t, 每个进程的id在整个系统中是唯一的,调用getpid可以获得当前进程的id, 是一个正整数值。线程id的类型是thread_t, 它只在当前进程中保证是唯一的,在不同的系统中thread_t 这个类型有不同的实现,它可能是一个整数值,也可能是一个结构体,也可能是一个地址,所以不能简单地当成整数用printf打印,调用pthread_self可以获取当前线程的id。

attr参数表示线程属性,传NULL给attr参数,表示线程属性取缺省值。

#include<stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>

pthread_t c_pid;

void *printargs(void *msg)
{
  char *str = (char *)msg;
  printf("pthread_self() = %u\n", (unsigned int)pthread_self());
  printf("str = %s\n", str);

  return NULL;
}

int main()
{
  int ret = 0;
  pid_t pid;

  /* get process id */
  pid = getpid();

  ret = pthread_create(&c_pid, NULL, printargs, "hello world");
  if (ret)
  {
    printf("create thread failed!!!\n");
  }

  printf("pid = %d\n", pid);
  sleep(1);

  return 0;
}

编译运行结果:

$ gcc thread_test.c -lpthread -o thread_test
$ ./thread_test
pid = 29534
str = hello world
#include<stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>

pthread_t c_pid;

void *printargs(void *msg)
{
  char *str = (char *)msg;
  printf("str = %s\n", str);

  printf("pthread_self() = %u\n", (unsigned int)pthread_self());
  return NULL;
}

int main()
{
  int ret = 0;
  pid_t pid;

  /* get process id */
  pid = getpid();

  ret = pthread_create(&c_pid, NULL, printargs, "hello world");
  if (ret)
  {
    printf("create thread failed!!!\n");
  }

  printf("pid = %u\n", (unsigned int)pid);
  printf("c_pid = %u\n", (unsigned int)c_pid);
  sleep(1);

  return 0;
}

编译运行结果:

$ gcc thread_test.c -lpthread -o thread_test
$ ./thread_test
pid = 31001
c_pid = 2695579392
str = hello world
pthread_self() = 2695579392

存在全局变量中ntid中保存的新创建的线程id, 和在新创建的线程中使用pthread_self获取到的线程id 是完全相同的。

如果任意一个线程调用了exit或_exit,则整个进程的所有线程都都终止了,由于从main函数return 也相当于调用exit, 为了防止新创建的线程还没有得到执行就终止,我们在main函数return 之前延时1秒,即使主线程等待1秒,内核也不一定会调度新创建的线程执行,下面我们进行以下优化。

#include<stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>

pthread_t c_pid;

void *printargs(void *msg)
{
  char *str = (char *)msg;
  printf("str = %s\n", str);

  printf("pthread_self() = %u\n", (unsigned int)pthread_self());
  return NULL;
}

int main()
{
  int ret = 0;
  pid_t pid;

  /* get process id */
  pid = getpid();

  ret = pthread_create(&c_pid, NULL, printargs, "hello world");
  if (ret)
  {
    printf("create thread failed!!!\n");
  }

  printf("pid = %u\n", (unsigned int)pid);
  printf("c_pid = %u\n", (unsigned int)c_pid);

  pthread_join(c_pid, NULL);

  return 0;
}
  • 终止线程

如果需要终止某个线程而不终止整个进程,可以有三种方法:

  1. 从线程函数return, 这种方法对主线程不适用,从main函数return相当于调用exit。
  2. 一个线程可以调用pthread_cancel 终止同一个进程中的另外一个线程。
  3. 线程可以调用pthread_exit 终止自己。
#include <pthread.h>
 void pthread_exit(void *value_ptr);

value_ptr是void *类型,和线程函数返回值的用法一样,其它线程可以调用pthread_join获得这个指针。

pthread_exit 或者return 返回的指针所指向的内存单元必须是全局的或者用malloc 分配的,不能在线程函数的栈上分配,因为当其它线程得到这个返回指针时线程函数已经退出了。

#include <pthread.h>
int pthread_join(pthread_t thread, void **value_ptr);

返回值:成功返回0,失败返回错误码

调用该函数的线程将挂起等待,直到id为thread的线程终止。thread 线程以不同的方法终止,通过pthread_join得到的终止状态是不同的,总结如下:

  1. 如果thread 线程通过return返回,value_ptr所指向的单元里存放的是thread 线程函数的返回值。
  2. 如果thread线程被别的线程调用pthread_cancel异常终止掉,value_ptr所指向的单元里存放的是常数PTHREAD_CANCELED。
  3. 如果thread线程是自己调用pthread_exit终止的,value_ptr 所指向的单元存放的是传给pthread_exit的参数。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

void UNUSED_PARAMS(void *arg)
{
  if (arg != NULL)
    printf("%s\n", (char*)(arg));

  return;
}

void *thr_fn1(void *arg)
{
  UNUSED_PARAMS(arg);

  printf("thread 1 returning\n");
  return (void *)1;
}

void *thr_fn2(void *arg)
{
  UNUSED_PARAMS(arg);

  printf("thread 2 exiting\n");
  pthread_exit((void *)2);
}

void *thr_fn3(void *arg)
{
  UNUSED_PARAMS(arg);

  while(1)
  {
    printf("thread 3 writing\n");
    sleep(1);
  }
}

int main(void)
{
  pthread_t   tid;
  void        *tret;

  pthread_create(&tid, NULL, thr_fn1, NULL);
  pthread_join(tid, &tret);
  printf("thread 1 exit code %d\n", (int)(tret));

  pthread_create(&tid, NULL, thr_fn2, NULL);
  pthread_join(tid, &tret);
  printf("thread 2 exit code %d\n", (int)(tret));

  pthread_create(&tid, NULL, thr_fn3, NULL);
  sleep(3);
  pthread_cancel(tid);
  pthread_join(tid, &tret);
  printf("thread 3 exit code %d\n", (int)(tret));

  return 0;
}

在pthread库中常数PTHREAD_CANCELED的值为-1,可以在头文件pthread.h 找到它的定义:

#define PTHREAD_CANCELED((void *) -1)

线程终止后,其终止状态一直保留到其它线程调用pthread_join获取它的状态为止。

编译运行结果:

$ gcc thread_test.c -lpthread -o thread_test
$ ./thread_test
thread 1 returning
thread 1 exit code 1
thread 2 exiting
thread 2 exit code 2
thread 3 writing
thread 3 writing
thread 3 writing
thread 3 exit code -1

相关推荐

Python操作Word文档神器:python-docx库从入门到精通

Python操作Word文档神器:python-docx库从入门到精通动动小手,点击关注...

Python 函数调用从入门到精通:超详细定义解析与实战指南 附案例

一、函数基础:定义与调用的核心逻辑定义:函数是将重复或相关的代码块封装成可复用的单元,通过函数名和参数实现特定功能。它是Python模块化编程的基础,能提高代码复用性和可读性。定义语法:...

等这么长时间Python背记手册终于来了,入门到精通(视频400集)

本文毫无套路!真诚分享!前言:无论是学习任何一门语言,基础知识一定要扎实,基础功非常的重要,找一个有丰富编程经验的老师或者师兄带着你会少走很多弯路,你的进步速度也会快很多,无论我们学习的目的是什么,...

图解Python编程:从入门到精通系列教程(附全套速查表)

引言本系列教程展开讲解Python编程语言,Python是一门开源免费、通用型的脚本编程语言,它上手简单,功能强大,它也是互联网最热门的编程语言之一。Python生态丰富,库(模块)极其丰富,这使...

Python入门教程(非常详细)从零基础入门到精通,看完这一篇就够

本书是Python经典实例解析,采用基于实例的方法编写,每个实例都会解决具体的问题和难题。主要内容有:数字、字符串和元组,语句与语法,函数定义,列表、集、字典,用户输入和输出等内置数据结构,类和对象,...

Python函数全解析:从入门到精通,一文搞定!

1.为什么要用函数?函数的作用:封装代码,提高复用性,减少重复,提高可读性。...

Python中的单例模式:从入门到精通

Python中的单例模式:从入门到精通引言单例模式是一种常用的软件设计模式,它保证了一个类只有一个实例,并提供一个全局访问点。这种模式通常用于那些需要频繁创建和销毁的对象,比如日志对象、线程池、缓存等...

【Python王者归来】手把手教你,Python从入门到精通!

用800个程序实例、5万行代码手把手教你,Python从入门到精通!...

Python从零基础入门到精通:一个月就够了

如果想从零基础到入门,能够全职学习(自学),那么一个月足够了。...

Python 从入门到精通:一个月就够了

要知道,一个月是一段很长的时间。如果每天坚持用6-7小时来做一件事,你会有意想不到的收获。作为初学者,第一个月的月目标应该是这样的:熟悉基本概念(变量,条件,列表,循环,函数)练习超过30个编...

Python零基础到精通,这8个入门技巧让你少走弯路,7天速通编程!

Python学习就像玩积木,从最基础的块开始,一步步搭建出复杂的作品。我记得刚开始学Python时也是一头雾水,走了不少弯路。现在回头看,其实掌握几个核心概念,就能快速入门这门编程语言。来聊聊怎么用最...

神仙级python入门教程(非常详细),从0到精通,从看这篇开始!

python入门虽然简单,很多新手依然卡在基础安装阶段,大部分教程对一些基础内容都是一带而过,好多新手朋友,对一些基础知识常常一知半解,需要在网上查询很久。...

Python类从入门到精通,一篇就够!

一、Python类是什么?大家在生活中应该都见过汽车吧,每一辆真实存在、能在路上跑的汽车,都可以看作是一个“对象”。那这些汽车是怎么生产出来的呢?其实,在生产之前,汽车公司都会先设计一个详细的蓝图...

学习Python从入门到精通:30天足够了,这才是python基础的天花板

当年2w买的全套python教程用不着了,现在送给有缘人,不要钱,一个月教你从入门到精通1、本套视频共487集,本套视频共分4季...

30天Python 入门到精通(3天学会python)

以下是一个为期30天的Python入门到精通学习课程,专为零基础新手设计。课程从基础语法开始,逐步深入到面向对象编程、数据处理,最后实现运行简单的大语言模型(如基于HuggingFace...

取消回复欢迎 发表评论: