2013年12月22日 星期日 20:08
APR和thread模块提供了非常丰富的线程相关的方法,使用方法和API风格与pthread很类似,线程入口函数类型是:
typedef void*(APR_THREAD_FUNC * apr_thread_start_t)(apr_thread_t *, void *)
下面是一个简单示例:
#include <apr.h>
#include <apr_pools.h>
#include <stdio.h>
#include <apr_errno.h>
#include <apr_thread_proc.h>
#include <apr_time.h>
void *thread_func(apr_thread_t *t,void *v) {
int i;
for(i=0;i<10;i++) {
apr_sleep(500000);
printf("thread %ld => %d\n",(long)v,i);
}
return NULL;
}
int main(int argc,char **argv) {
apr_initialize();
apr_pool_t *pool;
apr_pool_create(&pool,NULL);
apr_status_t st;
apr_thread_t *t1;
apr_thread_t *t2;
apr_thread_create(&t1,NULL,thread_func,(void *)1,pool);
apr_thread_create(&t2,NULL,thread_func,(void *)2,pool);
apr_thread_join(&st,t1);
printf("%d\n",st);
apr_thread_join(&st,t2);
printf("%d\n",st);
apr_pool_destroy(pool);
apr_terminate();
return 0;
}
参考文档:
http://apr.apache.org/docs/apr/1.3/group__apr__thread__proc.html
Zeuux © 2025
京ICP备05028076号