unix高级环境编程——线程详解

本期主题:
线程


往期地址:



1.线程概念

典型的UNIX进程可以看成只有一个控制线程,一个进程在某一时刻只能做一件事情,但是在单进程的环境中可以创建多个线程,让这多个线程各自执行自己的任务。

1.1 为什么有了进程还需要线程?

当有多个任务的场景发生时,可以选择多进程也可以选择多线程,那么多线程相比于多进程的优点在于哪里?

1.多线程可以将异步事件简化为同步事件来处理,这样同步编码比异步编码更为简单;
2.多进程之间的 文件描述符和内存等 是不能共享的,而多线程可以;
3.有时,让程序看起来好像是在同时做两件事情是很有用的。一个经典的例子是,在编辑文档的同时对文档中的单词个数进行实时统计。这个明显的多任务工作如果用多进程的方式来完成将很难做到高效,因为各个不同的进程必须紧密合作才能满足加锁和数据一致性方面的要求,而用多线程来完成就比用多进程要容易得多。

1.2 线程特点

一个进程里的所有信息对该进程的所有线程都是共享的,包括程序的全局内存、堆、栈以及文件描述符。

2.线程常用API

这里介绍线程常用的一些API。

2.1 线程创建

传统的UNIX进程中,每个进程只有一个控制线程,可以使用pthread_create来创建线程。

#include <pthread.h>

       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

       Compile and link with -pthread.

param1: 当创建成功后,新创建线程的线程ID会被设置成thread所指向的内存单元
param2: 线程属性配置,后面再详细讲
param3: 创建完线程之后,新线程从 start_routine这个函数地址开始运行
param4: 把参数传递给start_routine这个函数,作为入参

下面看一个实际的例子,创建线程后,分别在主线程和新线程中打印进程号和线程号,预期结果是:两个线程的进程ID相同,而线程ID不同

pthread_t ntid;

//这个是用来打印线程号和进程号的函数
void printids(const char *s)
{
    pid_t       pid;
    pthread_t   tid;

    pid = getpid();
    tid = pthread_self(); //获取自身线程ID
    printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long)pid, (unsigned long)tid, (unsigned long)tid);
}

void *thr_fn(void *arg)
{
    printids("new thread: ");
}

int main(void)
{
    int err;
    err = pthread_create(&ntid, NULL, thr_fn, NULL);
    if (err != 0)
    {
        printf("pthread create error!\n");
    }
    printids("main thread:");
    sleep(1);
    exit(0);
}

jason@ubuntu:~/WorkSpace/0.Unix_AP/7.PCB_env/pthread$ ./a.out 
main thread: pid 25438 tid 139714495969088 (0x7f11d0e6f740)
new thread:  pid 25438 tid 139714495964928 (0x7f11d0e6e700)

与预期结果一致。

2.2 线程终止

单个线程在不终止整个进程的前提下,可以通过3种方式来退出:

  1. 直接使用return,返回值是线程的退出码
  2. 线程被该进程中的其他线程取消
  3. 线程调用pthread_exit
#include <pthread.h>

       void pthread_exit(void *retval);

       Compile and link with -pthread.

retval是一个无类型指针,进程中的其他线程可以通过pthread_join来访问到这个指针

       int pthread_join(pthread_t thread, void **retval);

调用pthread_join的线程将一直阻塞,直到thread进程退出,并且retval是返回码

下面看一个具体的例子:
创建2个线程,并让他们通过不同的方式来结束线程,在主线程使用pthread_join,并且打印各个线程的返回值

void *thr_fn1(void *arg)
{
    printf("INFO: thread 1 returning\n");
    return ((void *)1);
}

void *thr_fn2(void *arg)
{
    printf("INFO: thread 2 exiting\n");
    pthread_exit((void *)2);
}

int main(void)
{
    int         err;
    pthread_t   tid1, tid2;
    void        *tid_ret;

    err = pthread_create(&tid1, NULL, thr_fn1, NULL);
    if (err != 0)
        printf("ERR: can't create thread1\n");

    err = pthread_create(&tid2, NULL, thr_fn2, NULL);
    if (err != 0)
        printf("ERR: can't create thread2\n");

    err = pthread_join(tid1, &tid_ret);
    if (err != 0)
        printf("ERR: can't join thread1\n");
    printf("INFO: thread 1 return code %ld\n", (long)tid_ret);

    err = pthread_join(tid2, &tid_ret);
    if (err != 0)
        printf("ERR: can't join thread2\n");
    printf("INFO: thread 2 exit code %ld\n", (long)tid_ret);
    exit(0);
}

jason@ubuntu:~/WorkSpace/0.Unix_AP/7.PCB_env/pthread$ ./a.out 
INFO: thread 2 exiting
INFO: thread 1 returning
INFO: thread 1 return code 1
INFO: thread 2 exit code 2

这里有一个与预期不同的现象在于,明明主线程中是先创建了线程1,再创建了线程2,但是看代码执行的结果,却是线程2先运行,线程1后运行,这是有可能的,因为pthread_create只是将线程创建好放入线程池中,真正的调度是OS来进行调度的

2.3 线程同步——互斥量

1.为什么需要互斥量?

前面提到了,同一进程的多个线程,他们所看到的数据视图是一样的,这样就会存在一个问题,A线程所用的变量可能会被B线程不小心给修改掉。
看一个实际的例子,在主线程中有一个变量,然后在线程2中去将这个变量自增,线程1中不去动,如果两个线程互不影响的话,应该是线程1中的值没变,线程2中的值增加了,我们看下实际运行的结果如何:

typedef struct _test_t {
    int8_t value;
} test_t;
test_t *fp_test = NULL;

void *thr_fn1(void *arg)
{
    printf("INFO_THR1: thread 1 returning\n");
    printf("INFO_THR1: thread 1 do nothing, value is %d\n", fp_test->value);
    return ((void *)1);
}

void *thr_fn2(void *arg)
{
    printf("INFO_THR2: thread 2 exiting\n");
    fp_test->value++;
    printf("INFO_THR2: thread 2 ++, value is %d\n", fp_test->value);
    pthread_exit((void *)2);
}

int main(void)
{
    int         err;
    pthread_t   tid1, tid2;
    void        *tid_ret;
    fp_test = malloc(sizeof(test_t));
    fp_test->value = 1;
    printf("INFO_MAIN: value is %d\n", fp_test->value);

    err = pthread_create(&tid1, NULL, thr_fn1, NULL);
    if (err != 0)
        printf("ERR: can't create thread1\n");

    err = pthread_create(&tid2, NULL, thr_fn2, NULL);
    if (err != 0)
        printf("ERR: can't create thread2\n");

    err = pthread_join(tid1, &tid_ret);
    if (err != 0)
        printf("ERR: can't join thread1\n");
    printf("INFO: thread 1 return code %ld\n", (long)tid_ret);

    err = pthread_join(tid2, &tid_ret);
    if (err != 0)
        printf("ERR: can't join thread2\n");
    printf("INFO: thread 2 exit code %ld\n", (long)tid_ret);
    exit(0);
}

jason@ubuntu:~/WorkSpace/0.Unix_AP/7.PCB_env/pthread$ ./a.out 
INFO_MAIN: value is 1
INFO_THR2: thread 2 exiting
INFO_THR2: thread 2 ++, value is 2
INFO_THR1: thread 1 returning
INFO_THR1: thread 1 do nothing, value is 2
INFO: thread 1 return code 1
INFO: thread 2 exit code 2

可见线程2的运行结果影响了线程1,所以我们需要一个东西能够对数据进行保护

2.pthread互斥量

可以使用pthread的互斥接口来保护数据,确保同一时间只有一个线程访问数据。
互斥量(mutex)从用处来说很像一把锁,当去访问共享的资源时,先把这个资源锁上,然后访问完了之后再把锁打开,这样就能保证在某一时刻最多只有一个线程去访问该共享资源。

1.初始化

互斥量用pthread_mutex_t类型来表示,使用互斥量之前需要先对他进行初始化:

  1. 如果是静态分配的互斥量,进行初始化时,可以把它设置为常量 PTHREAD_MUTEX_INITIALIZER,也可以使用 pthread_mutex_init 函数来进行初始化
  2. 如果是动态分配的互斥量(例如使用malloc进行分配的),最后在释放内存前需要使用 pthread_mutex_destory

mutex相关API:

SYNOPSIS
       #include <pthread.h>

       pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;

       pthread_mutex_t recmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

       pthread_mutex_t errchkmutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;

       int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr);



       int pthread_mutex_destroy(pthread_mutex_t *mutex);
2.加锁

对互斥量进行加锁,需要使用pthread_mutex_lock,如果互斥量已经上锁,则调用pthread_mutex_lock的线程将一直阻塞直到互斥量被解锁,对互斥量进行解锁,使用pthread_mutex_unlock

       int pthread_mutex_lock(pthread_mutex_t *mutex);

       int pthread_mutex_trylock(pthread_mutex_t *mutex);

       int pthread_mutex_unlock(pthread_mutex_t *mutex);
3.实例

在主线程中上锁,在线程1中解锁,在线程2中上锁,(由于线程2总是快于线程1运行,在线程2中上锁,能够让线程2阻塞)看其中变量的变化

typedef struct _test_t {
    int8_t value;
    pthread_mutex_t lock;
} test_t;
test_t *fp_test = NULL;

void *thr_fn1(void *arg)
{
    printf("INFO_THR1: thread 1 returning\n");
    printf("INFO_THR1: thread 1 do nothing, value is %d\n", fp_test->value);
    pthread_mutex_unlock(&fp_test->lock);
    printf("INFO_THR1: thread 1 MUTEX UNLOCK, value is %d\n", fp_test->value);
    return ((void *)1);
}

void *thr_fn2(void *arg)
{
    printf("INFO_THR2: thread 2 exiting\n");
    pthread_mutex_lock(&fp_test->lock);
    fp_test->value++;
    printf("INFO_THR2: thread 2 ++, value is %d\n", fp_test->value);
    pthread_exit((void *)2);
}

int main(void)
{
    int         err;
    pthread_t   tid1, tid2;
    void        *tid_ret;
    fp_test = malloc(sizeof(test_t));
    fp_test->value = 1;
    printf("INFO_MAIN: value is %d\n", fp_test->value);
    pthread_mutex_init(&fp_test->lock, NULL);
    pthread_mutex_lock(&fp_test->lock);
    printf("INFO_MAIN: MUTEX LOCK! \n");

    err = pthread_create(&tid1, NULL, thr_fn1, NULL);
    if (err != 0)
        printf("ERR: can't create thread1\n");

    err = pthread_create(&tid2, NULL, thr_fn2, NULL);
    if (err != 0)
        printf("ERR: can't create thread2\n");

    err = pthread_join(tid1, &tid_ret);
    if (err != 0)
        printf("ERR: can't join thread1\n");
    printf("INFO: thread 1 return code %ld\n", (long)tid_ret);

    err = pthread_join(tid2, &tid_ret);
    if (err != 0)
        printf("ERR: can't join thread2\n");
    printf("INFO: thread 2 exit code %ld\n", (long)tid_ret);
    exit(0);
}

// INFO_MAIN: value is 1
// INFO_MAIN: MUTEX LOCK! 
// INFO_THR2: thread 2 exiting
// INFO_THR1: thread 1 returning
// INFO_THR1: thread 1 do nothing, value is 1
// INFO_THR1: thread 1 MUTEX UNLOCK, value is 1
// INFO: thread 1 return code 1
// INFO_THR2: thread 2 ++, value is 2
// INFO: thread 2 exit code 2

2.4 线程同步——条件变量

条件变量是线程可用的另外一种同步机制,条件变量和互斥量一起使用
条件本身由互斥量来保护,线程在改变条件之前需要先锁住互斥量。

与互斥量类似,条件变量也需要先进行初始化

如果是静态分配的条件变量,可以使用 PTHREAD_COND_INITIALIZER 赋给静态分配的条件变量
如果是动态分配的条件变量,则使用 pthread_cond_init函数来进行初始化

同时动态分配的也需要释放,使用 pthread_cond_destory :

       #include <pthread.h>

       pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

       int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);

       int pthread_cond_signal(pthread_cond_t *cond);

       int pthread_cond_broadcast(pthread_cond_t *cond);

       int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);

       int pthread_cond_destroy(pthread_cond_t *cond);

pthread_cond_wait等待条件变量为真,这是一个阻塞式的等待

       int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);

有两个函数可以用于通知线程条件已经满足

       int pthread_cond_signal(pthread_cond_t *cond);

       int pthread_cond_broadcast(pthread_cond_t *cond);

看下面一个具体例子,定义锁和条件变量,线程2需要等待线程1的条件变量释放才能去进行后面的操作

typedef struct _test_t {
    int8_t value;
    pthread_mutex_t lock;
    pthread_cond_t  cond;
} test_t;
test_t *fp_test = NULL;

void *thr_fn1(void *arg)
{
    printf("INFO_THR1: thread 1 returning\n");
    printf("INFO_THR1: thread 1 LOCK\n");
    pthread_mutex_lock(&fp_test->lock);
    printf("INFO_THR1: thread 1 do nothing, value is %d\n", fp_test->value);
    if (fp_test->value == 0)
    {
        pthread_cond_signal(&fp_test->cond);
        printf("INFO_THR1: thread 1 COND SIGNAL\n");
    }
    pthread_mutex_unlock(&fp_test->lock);
    printf("INFO_THR1: thread 1 UNLOCK\n");
    return ((void *)1);
}

void *thr_fn2(void *arg)
{
    printf("INFO_THR2: thread 2 exiting\n");
    printf("INFO_THR2: thread 2 LOCK\n");
    pthread_mutex_lock(&fp_test->lock);

    printf("INFO_THR2: thread 2 begin wait\n");
    pthread_cond_wait(&fp_test->cond, &fp_test->lock); //cond_wait的时候,释放了互斥锁,不然thread1不能进行lock
    printf("INFO_THR2: thread 2 end wait\n");
    printf("INFO_THR2: thread 2 value is %d\n", fp_test->value);
    fp_test->value++;
    pthread_mutex_unlock(&fp_test->lock);
    printf("INFO_THR2: thread 2 UNLOCK\n");
    pthread_exit((void *)2);
}

int main(void)
{
    int         err;
    pthread_t   tid1, tid2;
    void        *tid_ret;
    fp_test = malloc(sizeof(test_t));
    fp_test->value = 0;
    printf("INFO_MAIN: value is %d\n", fp_test->value);
    pthread_mutex_init(&fp_test->lock, NULL);
    pthread_cond_init(&fp_test->cond, NULL);
    printf("INFO_MAIN: Init lock & cond!\n");

    err = pthread_create(&tid1, NULL, thr_fn1, NULL);
    if (err != 0)
        printf("ERR: can't create thread1\n");

    err = pthread_create(&tid2, NULL, thr_fn2, NULL);
    if (err != 0)
        printf("ERR: can't create thread2\n");

    err = pthread_join(tid1, &tid_ret);
    if (err != 0)
        printf("ERR: can't join thread1\n");
    printf("INFO: thread 1 return code %ld\n", (long)tid_ret);

    err = pthread_join(tid2, &tid_ret);
    if (err != 0)
        printf("ERR: can't join thread2\n");
    printf("INFO: thread 2 exit code %ld\n", (long)tid_ret);
    exit(0);
}

//运行结果:
jason@ubuntu:~/WorkSpace/0.Unix_AP/7.PCB_env/pthread$ ./a.out 
INFO_MAIN: value is 0
INFO_MAIN: Init lock & cond!
INFO_THR2: thread 2 exiting
INFO_THR2: thread 2 LOCK
INFO_THR2: thread 2 begin wait
INFO_THR1: thread 1 returning
INFO_THR1: thread 1 LOCK
INFO_THR1: thread 1 do nothing, value is 0
INFO_THR1: thread 1 COND SIGNAL
INFO_THR1: thread 1 UNLOCK
INFO: thread 1 return code 1
INFO_THR2: thread 2 end wait
INFO_THR2: thread 2 value is 0
INFO_THR2: thread 2 UNLOCK
INFO: thread 2 exit code 2

2.5 线程同步——POSIX信号量

有两组接口函数用于信号量:

  • 一组取自POSIX的实时扩展,用于线程
  • 另一组被称为系统V信号量,常用于进程的同步
    我们这里主要介绍用于线程同步的POSIX信号量

常用的API如下,包括:

  • 信号量的创建和释放,sem_init 和 sem_destory
  • 信号量的数值调节,sem_trywait、sem_wait 和 sem_post
NAME
       sem_init - initialize an unnamed semaphore

SYNOPSIS
       #include <semaphore.h>

       int sem_init(sem_t *sem, int pshared, unsigned int value);

       Link with -pthread.
//声明一个sem_t类型的变量,并把它的地址传递给sem_init来实现初始化
//pshared参数表明是否在多个进程中使用信号量
//value指定了信号量的初始值

NAME
       sem_post - unlock a semaphore,解锁信号量,对信号量+1

SYNOPSIS
       #include <semaphore.h>

       int sem_post(sem_t *sem);

NAME
       sem_wait, sem_timedwait, sem_trywait - lock a semaphore,对信号量加锁,信号量值减1

SYNOPSIS
       #include <semaphore.h>

       int sem_wait(sem_t *sem);

       int sem_trywait(sem_t *sem);

       int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);

代码实例:
有一个main_thread,负责接收标准输入的字符,同时对信号量进行+1;
有一个a_thread,负责对main_thread接收到的字符进行统计,同时对信号量进行-1
使用信号量保证两个线程之间的同步

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>

void *thread_function(void *arg);
sem_t bin_sem;

#define WORK_SIZE 1024
char work_area[WORK_SIZE];

int main()
{
	int res;
	pthread_t a_thread;
	void *thread_result;
	
	res = sem_init(&bin_sem, 0, 0);
	if (res != 0)
	{
		perror("semaphore init failed!");
		exit(EXIT_FAILURE);
	}
	
	res = pthread_create(&a_thread, NULL, thread_function, NULL);
	if (res != 0)
	{
		perror("pthread create failed!");
		exit(EXIT_FAILURE);
	}
	
	printf("main_thread: input some text, enter 'end' to finish\n");
	while(strncmp("end", work_area, 3) != 0)
	{
		fgets(work_area, WORK_SIZE, stdin);
		sem_post(&bin_sem);
	}

	printf("main_thread: waiting for thread finish...\n");
	res = pthread_join(a_thread, &thread_result);
	if (res != 0)
	{
		perror("thread join failed!");
		exit(EXIT_FAILURE);
	}
	printf("thread joined\n");
	sem_destroy(&bin_sem);
	exit(EXIT_SUCCESS);
}

void *thread_function(void *arg)
{
	sem_wait(&bin_sem); //初始化阻塞在这等一次
	while(strncmp("end", work_area, 3) != 0)
	{
		printf("a_thread: you input %ld char\n", strlen(work_area) - 1);
		sem_wait(&bin_sem); //循环时在这里阻塞等
	}
	pthread_exit(NULL);
}


jason@ubuntu:~/WorkSpace/0.Unix_AP/7.PCB_env/pthread$ ./a.out 
main_thread: input some text, enter 'end' to finish
f
a_thread: you input 1 char
a
a_thread: you input 1 char
hhh
a_thread: you input 3 char
end
main_thread: waiting for thread finish...
thread joined

也可以用互斥量来实现上述功能,主要是对 work_area 这个变量在使用时加锁,使用完之后解锁。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值