apr线程使用

apr线程使用



前言

主要对apr 线程作一下简单介绍,涉及apr_thread_t、apr_thread_mutex_t、apr_thread_cond_t等的使用。


一、核心函数

apr_thread_t相关接口

APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new_thread, 
                                            apr_threadattr_t *attr, 
                                            apr_thread_start_t func, 
                                            void *data, apr_pool_t *cont);
APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, 
                                          apr_thread_t *thd); 
APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd, 
                                          apr_status_t retval);
APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
                                               apr_pool_t *p);  
APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
                                          void (*func)(void));                                                                                                                                                                             

apr_thread_mutex_t相关接口

APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
                                                  unsigned int flags,
                                                  apr_pool_t *pool);
 APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_threadAPR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex);
_mutex_t *mutex);
APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex);                                            

apr_thread_cond_t相关接口

APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
                                                 apr_pool_t *pool);
APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
                                               apr_thread_mutex_t *mutex);
APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond);
APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond);                                                                                                

二、代码示例

代码如下(示例):
MyThread类中封装了apr线程、锁、信号量的使用,可以据此学习一下apr线程的使用。

#include "stdafx.h"
#include "apr_test.h"

extern "C" {
#include "apr_pools.h"
#include"apr_thread_mutex.h"
#include "apr_thread_cond.h"
#include "apr_thread_proc.h"
}

class MyThread
{
public:
	MyThread(apr_pool_t* pool)
		: pool_(pool)
	{
		if (apr_thread_mutex_create(&data_mutex_, APR_THREAD_MUTEX_DEFAULT, pool) != APR_SUCCESS) {
			printf("create data mutex failed...\n");
			return;
		}
		if (apr_thread_mutex_create(&mutex_, APR_THREAD_MUTEX_DEFAULT, pool) != APR_SUCCESS) {
			printf("create mutex failed...\n");
			return;
		}
		if (apr_thread_cond_create(&cond_, pool) != APR_SUCCESS) {
			printf("create cond failed...\n");
			return;
		}
	}
	~MyThread()
	{
		stop();
		apr_thread_mutex_destroy(mutex_);
		apr_thread_cond_destroy(cond_);
		mutex_ = nullptr;
	}

	bool is_start()
	{
		return is_started_;
	}

	void start()
	{
		stop();
		apr_thread_create(&thread_, NULL, &MyThread::run, this, pool_);
		is_started_ = true;
	}

	void stop()
	{
		if (is_started_)
		{
			is_started_ = false;
			apr_status_t retval;
			signal();
			apr_thread_join(&retval, thread_);
		}
	}

	void set_index(int n)
	{
		lock();
		index_ = n;
		is_data_ready_ = true;
		unlock();
		signal();
	}

	static void* APR_THREAD_FUNC run(apr_thread_t *thd, void *data)
	{
		MyThread* pThis = (MyThread*)data;
		if (!pThis)
		{
			printf("%s, params error...\n", __FUNCTION__);
			return NULL;
		}
		pThis->is_started_ = true;

		printf("<----- thread start----->\n");

		while (pThis->is_started_)
		{
			if (!pThis->is_data_ready_)
			{
				pThis->wait();
			}
			pThis->is_data_ready_ = false;
			pThis->lock();
			printf("<----- index[%d]----->\n", pThis->index_);
			pThis->unlock();
		}
		apr_thread_exit(thd, 0);

		printf("<----- thread exit----->\n");

		return NULL;
	}

	static void msleep(int msec)
	{
		apr_sleep(apr_time_from_msec(msec));
	}

protected:
	void lock()
	{
		apr_thread_mutex_lock(data_mutex_);
	}

	void unlock()
	{
		apr_thread_mutex_unlock(data_mutex_);
	}

	void wait()
	{
		apr_thread_mutex_lock(mutex_);
		apr_thread_cond_wait(cond_, mutex_);
		apr_thread_mutex_unlock(mutex_);
	}

	void signal()
	{
		apr_thread_mutex_lock(mutex_);
		apr_thread_cond_signal(cond_);
		apr_thread_mutex_unlock(mutex_);
	}
	

private:
	apr_pool_t* pool_ = nullptr;
	apr_thread_mutex_t* data_mutex_ = nullptr;
	apr_thread_mutex_t* mutex_ = nullptr;
	apr_thread_cond_t* cond_ = nullptr;
	apr_thread_t* thread_ = nullptr;
	bool is_started_ = false;
	bool is_data_ready_ = false;
	int index_ = 0;
};

void thread_once_func()
{
	printf("<----- thread run once----->\n");
}

void thread_test(apr_pool_t* pool)
{
	MyThread* mythread = new MyThread(pool);

	apr_thread_once_t *control = nullptr;
	apr_thread_once_init(&control, pool);
	apr_thread_once(control, &thread_once_func);

	mythread->start();

	int arr[] = { 3, 1, 4, 1, 5, 9, 2, 6 };
	int i = 0, len = sizeof(arr) / sizeof(int);
	for (int i = 0; i < len+1 && mythread->is_start(); i++)
	{
		if (i == len)
		{
			mythread->stop();
			break;
		}
		mythread->set_index(arr[i]);
		MyThread::msleep(3);
	}
	delete mythread;
	mythread = nullptr;
}
static void aprerr(const char *fn, apr_status_t rv)
{
	char buf[120];

	fprintf(stderr, "%s->%d/%s\n",
		fn, rv, apr_strerror(rv, buf, sizeof buf));
	exit(1);
}


int main()
{
	apr_status_t rv = apr_initialize();
	if (rv != APR_SUCCESS) {
		aprerr("apr_initialize()", rv);
	}

	apr_pool_t* pool = NULL;
	apr_pool_create(&pool, NULL);
	thread_test(pool);
	apr_pool_destroy(pool);
	apr_terminate();
	getchar();
	return 0;
}

/**
 * output
 <----- thread run once----->
 <----- thread start----->
 <----- index[3]----->
 <----- index[1]----->
 <----- index[4]----->
 <----- index[1]----->
 <----- index[5]----->
 <----- index[9]----->
 <----- index[2]----->
 <----- index[6]----->
 <----- index[6]----->
 */

总结

本文主要介绍了apr 线程的使用,包含了线程、锁、信号量的使用。

代码下载:https://download.csdn.net/download/xxm524/85477298

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: apr-util-1.6.1.tar.gz 是Apache Portable Runtime(APR)的一个附加库,用于支持Apache软件中的各种功能。APR是一套为网络应用程序开发提供基本功能的库,它为开发者提供了一个跨平台的编程接口,用于处理底层操作系统的网络功能。 APR-util是APR项目的一个子库,为APR提供了额外的功能和特性。它包含一些常用的实用工具和扩展模块,如数据库连接池、内存缓存、LDAP和SSL等。 版本号apr-util-1.6.1.tar.gz表示这是APR-util的1.6.1版本的源代码压缩包。该压缩包是为了方便用户下载和安装APR-util库。压缩包中的源代码可以编译成可执行文件,以便于在用户的系统上部署和使用APR-util库。 用户可以在下载apr-util-1.6.1.tar.gz压缩包后,解压并编译源代码,生成相应的库文件。然后,将生成的库文件与自己的应用程序链接,以使用APR-util提供的功能和特性。这个过程可能需要一些编译工具和依赖库的支持,用户可以根据具体的操作系统和编译环境进行配置和安装。 总之,apr-util-1.6.1.tar.gz是Apache Portable Runtime(APR)附加库APR-util的源代码压缩包,用户可以通过编译该源代码生成可执行文件,以获取APR-util库的功能和特性。这样的工具库对于开发网络应用程序非常有用。 ### 回答2: apr-util-1.6.1.tar.gz 是一个压缩文件,用于安装 Apache Portable Runtime (APR) 工具的工具包。APR 是一套为软件开发者提供跨平台的 API 接口的库,可以提供底层操作系统功能的封装。 apr-util-1.6.1.tar.gz 包含了 APR 的附加工具和库,可以用于扩展和增强 APR 的功能。这个版本号表示的是 1.6.1 版本的 apr-util 软件包。 要使用 apr-util-1.6.1.tar.gz,首先需要将其解压缩。可以使用压缩软件或者命令行工具进行解压。解压后,可以得到源代码文件和其他相关文件。 接下来,可以使用编译工具,如 gcc,对源代码进行编译。编译时需要参考软件包中的安装说明文档,按照说明进行编译和安装。 具体而言,可以运行 configure 命令进行配置,然后运行 make 命令进行编译,最后运行 make install 命令进行安装。这样就可以将 apr-util-1.6.1 软件包编译并安装到系统中。 安装完成后,可以在开发中使用 APR 提供的 API 接口,来实现底层操作系统功能的封装。APR 提供了许多有用的功能,如网络编程、多线程处理、内存管理等等,可以简化软件开发过程,提高开发效率。 总之,apr-util-1.6.1.tar.gz 是一个 APR 工具的软件包,通过解压缩、编译和安装,可以在开发中使用 APR 提供的 API 接口,实现跨平台的软件开发。 ### 回答3: apr-util-1.6.1.tar.gz是一个软件包的压缩文件,被广泛用于Unix或类Unix操作系统中。它是Apache Portable Runtime (APR)中的一个组件,用于提供一些辅助函数和工具给开发者使用。 在开发Web服务器或其他网络应用程序时,APR-Util可以为开发人员提供一系列的功能,如数据库连接、加密解密、XML处理、URI解析等。它构建在APR之上,与APR一起,为开发人员提供了更高层次的抽象和易用性。 APR-Util的安装非常简单。首先,我们需要将tar.gz文件通过解压工具解压缩。然后,进入解压后的目录,并执行一些配置和编译的命令。最后,运行make和make install命令,将APR-Util安装到指定的目录中。 安装完成后,开发人员可以使用所提供的函数和工具来简化编写网络应用程序的过程。无论是连接和管理数据库,还是进行文件操作或加密解密,APR-Util都可以提供一系列易用的API。 总结来说,APR-Util是Apache Portable Runtime的一个组件,用于提供一些辅助函数和工具给开发者使用。它可以简化网络应用程序的开发过程,提供了常用的功能和工具。通过配置、编译和安装APR-Util,开发人员可以开始使用它来构建高效、稳定的网络应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一马途追

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

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

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

打赏作者

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

抵扣说明:

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

余额充值