C/C++ 用 pthread 进行多线程开发

时间:2022-06-18
本文章向大家介绍C/C++ 用 pthread 进行多线程开发,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

作为一个程序员,不管你用的开发语言是 C、C++、Java、Python 或者其它,你总会需要处理多任务。

多线程是实现多任务处理的一种最常用的手段,线程相比进程而言显得轻量级。

本文分享如何用 C 进行多线程开发。

核心在于 pthread 这个库。

调用 pthread_create()函数就可以创建一个线程。

它的函数原型如下:

#include <pthread.h>
extern int pthread_create (pthread_t *__restrict __newthread,
			   const pthread_attr_t *__restrict __attr,
			   void *(*__start_routine) (void *),
			   void *__restrict __arg) 

pthread.h 是它的库。

参数说明: 第一个参数是 pthread_t* 也就是代表线程实体的指针 第二个参数为了设置线程的属性,一般为 NULL 第三个参数是线程运行时的函数,这是个函数指针。 第四个参数也是一个指针,它是用来将数据传递进线程的运行函数

下面用一个代码来示例说明。

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

//线程函数
void *test(void *ptr)
{
	int i;
	for(i=0;i<8;i++)
	{
		printf("the pthread running ,count: %dn",i);
		sleep(1); 
	}

}


int main(void)
{
	pthread_t pId;
	int i,ret;
	//创建子线程,线程id为pId
	ret = pthread_create(&pId,NULL,test,NULL);
	
	if(ret != 0)
	{
		printf("create pthread error!n");
		exit(1);
	}

	for(i=0;i < 5;i++)
	{
		printf("main thread running ,count : %dn",i);
		sleep(1);
	}
	
	printf("main thread will exit when pthread is overn");
	//等待线程pId的完成
	pthread_join(pId,NULL);
	printf("main thread  exitn");

	return 0;

}

创建了名为 test.c 的文件。

这里还有一个重要的函数 pthread_join(),它的作用是挂起当前的线程,等待指定的线程运行完毕。在示例代码中主线程等待子线程执行完毕后才继续执行后面的代码。

我们现在可以编译然后执行它。

gcc -o test test.c -lpthread
./test

pthread 是一个动态库,编译的时候需要动态链接,不然程序会报错。

运行结果如下:

main thread running ,count : 0
the pthread running ,count: 0
main thread running ,count : 1
the pthread running ,count: 1
main thread running ,count : 2
the pthread running ,count: 2
main thread running ,count : 3
the pthread running ,count: 3
main thread running ,count : 4
the pthread running ,count: 4
main thread will exit when pthread is over
the pthread running ,count: 5
the pthread running ,count: 6
the pthread running ,count: 7
main thread  exit

线程与线程之间经常进行数据通讯,前面也提到过,pthread_create()最后一个参数就是用来传递数据的。

但需要特别注意的是它是 void * 的,也是就是无类型指针,这样做的目的是为了保证线程能够接受任意类型的参数,到时候再强制转换就好了。

我们简单做一些改动。

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

//线程函数
void *test(void *arg)
{
	int i;
	char* name = (char *)arg;
	for(i=0;i<8;i++)
	{
		printf("the pthread %s running ,count: %dn",name,i);
		sleep(1); 
	}

}

int main (int argc,char* argv)
{
	pthread_t pId;
	int i,ret;
	//创建子线程,线程id为pId
	ret = pthread_create(&pId,NULL,test,"sub pthread");
	
	if(ret != 0)
	{
		printf("create pthread error!n");
		exit(1);
	}

	for(i=0;i < 5;i++)
	{
		printf("main thread running ,count : %dn",i);
		sleep(1);
	}
	
	printf("main thread will exit when pthread is overn");
	//等待线程pId的完成
	pthread_join(pId,NULL);
	printf("main thread  exitn");

	return 0;

}

在线程创建的时候给子线程传递了它的名字,然后在线程的运行函数中进行了强制转换。

我们再看它编译后的运行结果。

main thread running ,count : 0
the pthread sub pthread running ,count: 0
main thread running ,count : 1
the pthread sub pthread running ,count: 1
main thread running ,count : 2
the pthread sub pthread running ,count: 2
main thread running ,count : 3
the pthread sub pthread running ,count: 3
main thread running ,count : 4
the pthread sub pthread running ,count: 4
main thread will exit when pthread is over
the pthread sub pthread running ,count: 5
the pthread sub pthread running ,count: 6
the pthread sub pthread running ,count: 7
main thread  exit

运行正常,也达到了目的。

上面是 C 的代码,其实 C++ 也同样适用,只不过需要用 g++ 编译器来编译代码。

本文介绍了简单的 C/C++ 多线程编程方法,初学者认真学习后基本可以用了。

但是,多线程最难的地方其实在于线程之间的数据共享和同步,有空的时候我会专门写一系列的文章来一一说明。