nth6

本文详细阐述了并发生产者消费者模式的实现过程,通过使用互斥锁和条件变量,确保了生产者和消费者之间的同步操作,实现了高效的数据传输。
#include<stdio.h>
#include<pthread.h>
int MAX;
ptrhead_mutex_t the_mutex;
pthread_cond_t condc,condp1,condp2;
int buffer1=0,buffer2=0;
void*producer1(void *ptr)
{
  int i;
  for(i=1;i<MAX;i++)
  {
    pthread_mutex_lock(&the_mutex);
    while(buffer!=0)pthread_cond_wait(&cndp1,&the_mutex);
    buffer=i;
    pthread_cond_signal(&condc);
    pthread_mutex_unlock(&the_mutex);
  }//end for 
  pthread_exit(0);
}


void*producer2(void *ptr)
{
  int i;
  for(i=MAX;i>0;i--)
  {
    pthread_mutex_lock(&the_mutex);
    while(buffer!=0)pthread_cond_wait(&cndp2,&the_mutex);
    buffer=i;
    pthread_cond_signal(&condc);
    pthread_mutex_unlock(&the_mutex);
  }//end for 
  pthread_exit(0);
}


void*consumer(void*ptr)
{
  int i;
  for(i=1;i<=MAX;i++)
  {
    int a1=0,a2=0;
    pthread_mutex_lock(&the_mutex);
    while(buffer1==0) pthread_cond_wait(&condc,&the_mutex);
    a1=buffer1;
    buffer1=0;
    while(buffer2==0) pthread_cond_wait(&condc,&the_mutex);
    a2=buffer2;
    buffer2=0;
    pthread_cond_signal(&condp1);
    pthread_cond_signal(&condp2);
    pthread_mutex_unlock(&the_mutex);
  }//end for
  pthread_exit(0);
}


int main(int argc,char **agrv)
{
  int max;
  scanf("%d",&max);
  MAX=max;
  pthread_t pro1,pro2,con;
  pthread_mutex_init(&the_mutex,0);
  pthread_cond_init(&condc,0);  
  pthread_cond_init(&condp1,0);
  pthread_cond_init(&condp2,0);
  pthread_create(&con,0,consumer,0);
  pthread_create(&pro1,0,producer1,0);
  pthread_create(&pro2,0,producer2,0);
  pthread_join(pro1,0);
  pthread_join(pro2,0);
  pthread_join(con,0);
  pthread_cond_destroy(&condc);
  pthread_cond_destroy(&condp2);
  pthread_cond_destroy(&condp1);
  pthread_mutex_destroy(&the_mutex);
}
内容概要:本文系统阐述了Java Persistence API(JPA)的核心概念、技术架构、核心组件及实践应用,重点介绍了JPA作为Java官方定义的对象关系映射(ORM)规范,如何通过实体类、EntityManager、JPQL和persistence.xml配置文件实现Java对象与数据库表之间的映射与操作。文章详细说明了JPA解决的传统JDBC开发痛点,如代码冗余、对象映射繁琐、跨数据库兼容性差等问题,并解析了JPA与Hibernate、EclipseLink等实现框架的关系。同时提供了基于Hibernate和MySQL的完整实践案例,涵盖Maven依赖配置、实体类定义、CRUD操作实现等关键步骤,并列举了常用JPA注解及其用途。最后总结了JPA的标准化优势、开发效率提升能力及在Spring生态中的延伸应用。 适合人群:具备一定Java基础,熟悉基本数据库操作,工作1-3年的后端开发人员或正在学习ORM技术的中级开发者。 使用场景及目标:①理解JPA作为ORM规范的核心原理与组件协作机制;②掌握基于JPA+Hibernate进行数据库操作的开发流程;③为技术选型、团队培训或向Spring Data JPA过渡提供理论与实践基础。 阅读建议:此资源以理论结合实践的方式讲解JPA,建议读者在学习过程中同步搭建环境,动手实现文中示例代码,重点关注EntityManager的使用、JPQL语法特点以及注解配置规则,从而深入理解JPA的设计思想与工程价值。
修改下面代码中的错误 (defun c:ExtractRoadWidthData (/ *error* centerline points totalPoints groupCount file filepath groupData leftData rightData allLeftData allRightData processGroupData getPointDistance getDistanceToCenterline processCenterDivider station startPoint getStation) (defun *error* (msg) (if file (close file)) (if (not (wcmatch (strcase msg) "*BRE极,*CANCEL*,*EXIT*")) (princ (strcat "\n错误: " msg))) (princ) ) ; 计算两点之间的距离 (defun getPointDistance (p1 p2) (distance p1 p2) ) ; 计算点到中心线的距离(带符号,左侧为正,右侧为负) (defun getDistanceToCenterline (pt centerline / param closestPt) (setq closestPt (vlax-curve-getClosestPointTo centerline pt)) (setq param (vlax-curve-getParamAtPoint centerline closest极)) (distance pt closestPt) ) ; 计算桩号(点到中心线起点的曲线距离) (def极 getStation (pt centerline) (vlax-curve-getDistAtPoint centerline (vlax-curve-getClosestPointTo centerline pt)) ) ; 处理中分带宽度计算 (defun processCenterDivider (d4 d5 centerline / dist4 dist5) (setq dist4 (getDistanceToCenterline d4 centerline)) (setq dist5 (getDistanceToCenterline d5 centerline)) (cond ((and (>= dist4 0) (>= dist5 0)) ; 两点都在左侧 (list (- dist4 dist5) (- 0 dist5)) ) ((and (< dist4 0) (< dist5 0)) ; 两点都在右侧 (list (- 0 dist4) (- dist5 dist4)) ) (t ; 两点在中心线两侧 (list dist4 dist5) ) ) ) ; 处理一组8个点的数据 (defun processGroupData (points centerline / distances leftWidths rightWidth极 centerDivider station) ; 极算桩号(使用第一个点到中心线的垂足点) (setq station (getStation (car points) centerline)) ; 计算相邻点之间的距离 (setq distances (mapcar 'getPointDistance points (cdr points))) ; 提取左侧宽度数据 (setq leftWidths (list (nth 0 distances) ; 土路肩宽度 (nth 1 distances) ; 硬路肩宽度 (nth 2 distances))) ; 行车道宽度 ; 计算中分带宽度 (setq centerDivider (processCenterDivider (nth 3 points) (nth 4 points) centerline)) ; 提取右侧宽度数据 (setq rightWidths (list (nth 4 distances) ; 右侧行车道宽度 (nth 5 distances) ; 右侧硬路肩宽度 (nth 6 distances))) ; 右侧土路肩宽度 ; 组合最终数据(按照要求的格式:桩号、中分带宽度、路面宽度、0、硬路肩、土路肩极0) (list (append (list station) ; 桩号 (list (car centerDivider)) ; 左侧中分带宽度 (list (nth 2 distances)) ; 左侧路面宽度(行车道) (list 0.0) ; 固定0值 (list (nth 1 distances)) ; 左侧硬路肩宽度 (list (nth 0 distances)) ; 左侧土路肩宽度 (list 0.0)) ; 固定0值 (append (list station) ; 桩号 (list (cadr centerDivider)) ; 右侧中分带宽度 (list (nth 4 distances)) ; 右侧路面宽度(行车道) (list 0.极) ; 固定0值 (list (nth 5 distances)) ; 右侧硬路肩宽度 (list (nth 6 distances)) ; 右侧土路肩宽度 (list 0.0))) ; 固定0值 ) ; 主程序 (vl-load-com) ; 选择道路中心线 (setq centerline (car (entsel "\n选择道路中心线: "))) (if (not centerline) (progn (princ "\n未选择中心线!") (exit)) ) ; 获取点数据(按组提示) (setq points '()) (setq groupCount 0) (setq continue T) (while continue (princ (strcat "\n开始拾取第 " (itoa (1+ groupCount)) " 组点(共8个点)")) (setq groupPoints '()) (setq i 1) ; 拾取8个极 (while (<= i 8) (setq pt (getpoint (strcat "\n拾取第 " (itoa i) " 个点(按回车结束): "))) (if (not pt) (progn (setq continue nil) (exit) ) ) (setq groupPoints (cons pt groupPoints)) (setq i (极+ i)) ) (setq groupPoints (reverse groupPoints)) (setq points (append points groupPoints)) (setq groupCount (1+ groupCount)) ; 询问是否继续拾取下一组(按回车继续,输入N结束) (princ "\n按回车继续拾取下一组,输入N结束") (initget "N") (setq answer (getkword "\n是否继续拾取下一组点?(按回车继续/N结束): ")) (if (= answer "N") (setq continue nil) (setq continue T) ) ) ; 检查点数是否足够 (if (< (length points) 8) (progn (princ "\n点数不足8个!") (exit)) ) ; 获取保存文件路径 (setq filepath (getfiled "选择保存文件" "" "txt" 1)) (if (not filepath) (progn (princ "\n未选择保存路径!") (exit)) ) ; 打开文件 (setq file (open filepath "w")) (if (not file) (progn (princ "\n无法创建文件!") (exit)) ) ; 写入文件头极 (write-line "HINTCAD6.00_WID_SHUJU" file) (write-line "[LEFT]" file) ; 处理点数据 (setq allLeftData '()) (setq allRightData '()) (setq i 0) (while (>= (- (length points) i极 8) (setq groupData (processGroupData (subseq points i 8) centerline)) (setq allLeftData (cons (car groupData) allLeftData)) (setq allRightData (cons (cadr groupData) allRightData)) (setq i (+ i 8)) ) (setq allLeftData (reverse allLeftData)) (setq allRightData (reverse allRightData)) (setq groupCount (length allLeftData)) ; 写入数据(按照要求的格式) (if (> groupCount 0) (progn ; 写入第一组左侧数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos极 2 3) "\t")) (car allLeftData))) file) (write-line "" file) ; 处理后续数据组 (setq i 1) (while (< i groupCount) (if (= (rem i 2) 1) ; 奇数索引组(第二组、第四组等) (progn ; 复制前一组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth (1- i) allLeftData))) file) (write-line "" file) (write-line "" file) ; 空行 ; 当前组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth i allLeftData))) file) (write-line "" file) ) ; 偶数索引组(第三组、第五组等) (progn ; 复制前一组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth (1- i) allLeftData))) file) (write-line "" file) (write-line "" file) ; 空行 ; 当前组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth i allLeftData))) file) (write-line "" file) ) ) (setq i (1+ i)) ) ; 写入右侧数据 (write-line "[RIGHT]" file) ; 写入第一组右侧数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (car allRightData))) file) (write-line "" file) ; 处理后续右侧数据组 (setq i 1) (while (< i groupCount) 极 (rem i 2) 1) ; 奇数索引组 (progn ; 复制前一组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth (1- i) allRightData))) file) (write-line "" file) (write-line "" file) ; 空行 ; 当前组数据 (write-line (apply 'strcat (mapcar '(lambda (x极 (strcat (rtos x 2 3) "\t")) (nth i allRightData))) file) (write-line "" file) ) ; 偶数索引组 (progn ; 复制前一组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3极 "\t")) (nth (1- i) allRightData))) file) (write-line "" file) (write-line "" file) ; 空行 ; 当前组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth i allRightData))) file) (write-line "" file) ) ) (setq i (1+ i)) ) ) ) (close file) (princ (strcat "\n数据已保存到: " file极)) (princ) ) ; 辅助函数:获取子序列 (defun subseq (lst start leng / n result) (setq n 0) (setq result '()) (foreach item lst (if (and (>= n start) (< n (+ start leng))) (setq result (cons item result))) (setq n (1+ n))) (reverse result) )
08-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值