【Linux多线程编程】2.线程创建与回收

引言

上篇文章【Linux多线程编程】1. 多线程与单线程 对单线程和多线程简单的做了一个引入,并给出了多线程存在相较于单线程的一个优势:效率。通过多个线程并发的执行某项任务,达到提升程序执行效率的目的,但是这种利用多线程执行同一任务的实战相对较少。更多的模式是今天要引出的一种模式:生产者-消费者模型。
在引出生产者-消费者模型之前,先简单的学习一下如何创建和回收线程。

线程创建

#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
参数解析

thread:传出参数,用于表示一个线程。
attr:传入参数,用于手动设置新建线程的属性,例如线程的调用策略、线程所能使用的栈内存的大小等。基本上传NULL。
start_routine: 函数指针,指向带有一个void*参数,返回值为void*类型的函数。
arg:传递给 start_routine 的实参,不需要时,传 NULL 即可。

返回值

成功返回0,并产生一个线程,线程执行start_routine后退出
失败返回非0

示例

void *threadFunc(void *) {
	printf("a thread\n");
}
int main()
{
	pthread_t tid;
	int ret = pthread_create(&tid, NULL, threadFunc, NULL);
	if (ret == -1) {
		printf("create thread failed\n");
		exit(1);
	}
	sleep(1);
	return 0;
}

该段代码,创建一个线程,用tid表示该线程,线程的执行函数是threadFunc,线程创建成功后,执行threadFunc函数体,也就是输出a thread,然后线程退出。
需要注意的是main函数末尾的sleep(1),如果不加sleep(1),有可能创建的tid还没有来得及执行——输出a thread,整个进程就退出了。
这里我们要引出主线程以及进程的概念来解释这里sleep(1)的作用。
不讲晦涩的概念,在本例中,我们编译上面的这段代码,产生一个可执行文件,然后运行该可执行文件,就会产生一个运行中的程序,这个程序就在一条条执行main里面的语句。这个运行中的程序就是一个独立的进程
线程我们已经知道了,通过pthread_create可以创建出一个线程。一个进程可以多次调用pthread_create产生多个线程,这多个线程都归属于该进程,共享该进程的部分资源(资源共享这部分在后续章节中会逐渐展开)。
那么主线程又是什么?
答:如程序所见,从main函数的开始运行到main函数的结束的线程就是主线程。任何一个进程都有一个主线程,用pthread_create产生的线程,通常称为子线程。
一个进程本质上是由一个或多个线程组成的。
现在再回头看为什么不加 sleep(1) 可能会导致子线程未运行?
一个进程中的各个线程是并发执行的,也就是“同时执行”,因此就会有某个线程执行的快,某个线程执行的慢的情况,上例中如果主线程快于子线程,则主线程先退出,主线程退出会导致进程退出,进程退出会回收所有的资源,包括这个进程产生的所有线程;所以子线程还未执行,就由于进程退出而退出了。

编程实践中,通常都是主线程等待所有子线程执行结束退出后,主线程最后退出。
这就是线程回收。

线程回收

int pthread_join(pthread_t thread, void ** retval);
参数说明

thread :用于指定接收哪个线程的返回值;传pthread_create传出的thread,上例中的tid;
retval:表示接收到的返回值,如果 thread 线程没有返回值,又或者我们不需要接收 thread 线程的返回值,可以将 retval 参数置为 NULL。

示例

int main()
{
	pthread_t tid;
	int ret = pthread_create(&tid, NULL, threadFunc, NULL);
	if (ret == -1) {
		printf("create thread failed\n");
		exit(1);
	}
	pthread_join(tid, NULL); // 回收 tid 线程
	return 0;
}

生产者-消费者模型

如下代码在主线程中创建了两个线程,分别执行函数 producer_thread 和 consumer_thread,producer_thread 每隔1s将全局变量 total 的值 + 5,模拟生产者;consumer_thread 每隔1s将全局变量 total 的值 - 5,模拟消费者。
多次运行这段代码,看看都会有什么可能的输出结果?
修改 producer_thread / consumer_thread 中的 sleep 秒数,再次运行代码,看看结果发生了什么变化?
示例结尾给出了一种可能的输出结果,以供参考。
结合本文的内容,细细品味这段最最基本但尚不完善的生产者-消费者模型的代码,将在下节详细展开。

/*
 * test_pthread_worker.c
*/
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>             /* for sigaction */
#include <errno.h>
#include <unistd.h>

int total = 0;

static void *producer_thread(void *UnusedArg) {
  while (1) {
    sleep(1);
    total += 5;
    printf("%s produce 5 tools, now total %d\n", __func__, total);
  }
  return NULL;
}

static void *consumer_thread(void *UnusedArg) {
  while (1) {
    sleep(1);
    total -= 5;
    printf("%s consume 5 tools, now total %d\n", __func__, total);
  }
  return NULL;
}

int main()
{
  int rc;
  pthread_t producer;
  pthread_t consumer;

  rc = pthread_create(&producer, NULL, producer_thread, NULL);
  if (rc != 0) {
    printf("Could not create producer_thread\n");
    return -1;
  }

  rc = pthread_create(&consumer, NULL, consumer_thread, NULL);
  if (rc != 0) {
    printf("Could not create consumer_thread\n");
    return -1;
  }

  pthread_join(producer, NULL);
  pthread_join(consumer, NULL);
  return 0;
}

编译为可执行文件
gcc -lpthread test_pthread_worker.c -o test_pthread_worker
一种可能的运行结果

producer_thread produce 5 tools, now total 5
consumer_thread consume 5 tools, now total 0
consumer_thread consume 5 tools, now total -5
producer_thread produce 5 tools, now total 0
consumer_thread consume 5 tools, now total -5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdnGuoYuying

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值