nth4

本文展示了一个使用C语言实现的多线程计算示例,通过创建多个线程来并行处理一个整数数组的数据。该示例展示了如何初始化线程、分配任务以及收集线程的计算结果。
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>




int thread_num = 1;
long vector_length = 100000;
int total = 0;
int *a = NULL;


void *vel_mul(void *arg);


int main(int argc, char *argv[]){
int res;
void *thread_result;
//scan income
if(argc != 3){
printf("You have not enter enough parameters. So using default parameter\n");
}
else{
thread_num = atoi(argv[1]);
vector_length = atol(argv[2]);
}
printf("Thread number is %d. Vector length is %ld\n",thread_num,vector_length);
//scan finished


//start create threads
a = (int *)calloc(vector_length,sizeof(int));
for(int i = 0; i < vector_length; i++){
switch((i+1)%3){
case 0 :
a[i]=1;
break;
case 1 :
a[i]=-1;
break;
case 2 :
a[i]=0;
break;
}
}
pthread_t *a_thread;
a_thread = (pthread_t *)calloc(thread_num,sizeof(pthread_t));


for(int i = 0; i < thread_num; i++){
res = pthread_create(&a_thread[i],NULL,vel_mul,(void *)&i);
if(res != 0)perror("thread creation failed");
}


printf("Waiting ...\n");


for(int i = thread_num-1; i >=0; i--){
res = pthread_join(a_thread[i],&thread_result);
}
printf("finish \n");
printf("total is %d\n",total);
修改下面代码中的错误 (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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值