nth6

本文通过一个具体的多线程程序示例介绍了如何使用pthread库实现线程间的同步,包括生产者-消费者模型中互斥锁和条件变量的使用。
#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);
}
修改下面代码中的错误 (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
由于未提供具体的提取道路宽度数据并保存到文件的AutoLISP代码,下面给出一个示例代码及其解释,该代码假设图形中道路对象有一个自定义的宽度属性,然后将这些宽度数据提取出来保存到一个文本文件中。 ```lisp (defun c:ExtractRoadWidths ( / ss ent width file) ; 选择所有图形对象 (setq ss (ssget)) ; 打开一个文件用于写入数据 (setq file (open "C:\\road_widths.txt" "w")) (if file (progn ; 遍历选择集中的每个对象 (repeat (sslength ss) (setq ent (ssname ss 0)) ; 假设道路对象有一个自定义的宽度属性,这里简单模拟获取宽度 ; 实际中需要根据对象类型和属性来获取正确的宽度值 (setq width (getproperty ent "Width")) (if width ; 将宽度数据写入文件 (write-line (rtos width) file) ) ; 从选择集中移除已处理的对象 (ssdel ent ss) ) ; 关闭文件 (close file) (princ "\n道路宽度数据已保存到 C:\\road_widths.txt") ) (princ "\n无法打开文件进行写入。") ) (princ) ) ``` ### 代码解释 1. `(defun c:ExtractRoadWidths ( / ss ent width file)`:定义一个名为 `c:ExtractRoadWidths` 的AutoLISP函数,其中 `ss` 用于存储选择集,`ent` 用于存储当前处理的对象,`width` 用于存储道路宽度,`file` 用于存储文件句柄。 2. `(setq ss (ssget))`:选择图形中的所有对象。 3. `(setq file (open "C:\\road_widths.txt" "w"))`:打开一个名为 `road_widths.txt` 的文件用于写入数据。 4. `(repeat (sslength ss)`:遍历选择集中的每个对象。 5. `(setq ent (ssname ss 0))`:获取选择集中的第一个对象。 6. `(setq width (getproperty ent "Width"))`:假设道路对象有一个自定义的宽度属性,这里尝试获取该属性的值。实际使用时需要根据对象类型和属性来获取正确的宽度值。 7. `(write-line (rtos width) file)`:将宽度数据转换为字符串并写入文件。 8. `(ssdel ent ss)`:从选择集中移除已处理的对象。 9. `(close file)`:关闭文件。 ### 注意事项 - 上述代码中的 `getproperty` 函数是一个示例,实际中需要根据道路对象的具体类型和属性来获取正确的宽度值。可能需要使用 `entget` 函数来获取对象的详细信息。 - 确保文件路径 `C:\\road_widths.txt` 是有效的,并且有写入权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值