gpt4 book ai didi

c - 从主结构中提取子结构以创建线程

转载 作者:行者123 更新时间:2023-11-30 16:57:19 25 4
gpt4 key购买 nike

我在尝试向 pthread_create 发送多个参数时遇到问题,该问题基本上是因为其中一个参数是另一个结构。

这是节点。

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#define NUM_THREADS 4


struct arr {
char line[10];
};
struct args {
struct arr record; int count; int init; int end;
};

void* processarr(void *arguments)
{
int count; int init; int end;
struct args *argstmp=arguments;
init=argstmp->init;
count=argstmp->count;
end=argstmp->end;
struct arr record[count];
record=(struct arr)argstmp->record;

printf("Some of the vals are init %d count %d end %d\n",init, count, end);
printf("vals like record 0\n", record[0].line);
pthread_exit(NULL);
}/*end of processarr*/


int main (int argc, char *argv[])
{

int line_count;
FILE *ptr_file;
char buf[10];


ptr_file =fopen(argv[ 1 ],"r");
if (!ptr_file)
return 1;

while (fgets(buf,10, ptr_file)!=NULL)
{
line_count++ ;
}
rewind(ptr_file);
struct arr record[line_count];

line_count=0;
while (fgets(buf,10, ptr_file)!=NULL)
{
line_count++ ;
buf[strcspn(buf, "\r\n")] = 0; /* Removing end null chars*/
strcpy(record[line_count].line,buf);
}


float grptmp,group, lgroup;

grptmp=line_count/NUM_THREADS;

int counter1,counter2,init,end;
counter2=1;

struct args myargs;

//processarr(record, line_count, init, end);
pthread_t threads[NUM_THREADS];


for (counter1=0;counter1<=line_count;counter1++)
{
if(counter2==NUM_THREADS)
{
end=line_count;
}else{
end=counter1+grptmp;
}
init=counter1;
myargs.record=*record;
myargs.count=line_count;
myargs.init=init;
myargs.end=end;
printf ("Run job #%d with paramts Init=%d and End=%d\n",counter2, init, end);
//call here
//struct arr *record; int count; int init; int end;


int rc;
long t;
for(t=0;t<NUM_THREADS;t++){

rc = pthread_create(&threads[t], NULL,processarr,&myargs);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}

counter1=counter1+grptmp;
counter2++;

}

return 0;
}

因此,当我发送存储在 myargs.record=*record 中的参数时,由于某种原因,我无法在函数中“解压”它一次。

该函数被定义为 void 以便能够捕获整个大参数,我正在尝试重新映射那里的所有内容,计数工作正常,但称为记录的那个实际上是另一个结构不是工作正常,看起来像是 Actor 问题。

void* processarr(void *arguments)
{
int count; int init; int end;
struct args *argstmp=arguments;
init=argstmp->init;
count=argstmp->count;
end=argstmp->end;
struct arr record[count];
record=(struct arr)argstmp->record;

printf("Some of the vals are init %d count %d end %d\n",init, count, end);
printf("vals like record 0\n", record[0].line);
pthread_exit(NULL);
}

编译时出现以下错误。

test4.c: In function processarr:
test4.c:31:7: error: assignment to expression with array type
record=(struct arr)argstmp->record;

知道为什么这不起作用吗?最后一个是我最后一次使用 argstmp 前面的转换(struct arr)进行的更改(应该包含所有内容)。

最佳答案

详细说明我的回答,这就是我使用另一个结构来传递参数所做的事情。

typedef struct {char line[10];}mystruct;
typedef struct {mystruct struct1;char line[10];}wrapper;
struct wrapper2 {mystruct struct1;char line[10];};

void unwrap(struct wrapper2 args){
printf("val is %s\n",args.line);
mystruct tmp=args.struct1;
printf("val inside structure is %s\n\n", tmp.line);
}

int main ()
{
mystruct names;
strcpy(names.line,"TEST");

struct wrapper2 wrapper1;
wrapper1.struct1=names;
strcpy(wrapper1.line,"Value1");
unwrap (wrapper1);
}

我希望这个示例可以帮助您解决问题,您只需要使用 pthread_create 传递相同的内容即可。

更新:

最终的代码可能如下所示:

#include <pthread.h>
#include <stdlib.h>


struct mystruct {
int val;
};

void * func (void *args){
struct mystruct *st1=args;
printf("Thread created..%d\n", st1->val);
pthread_exit(NULL);
}
int main ()
{
/* Thread creation logic as void * (*)(void *)

int pthread_create (pthread_t *, const pthread_attr_t *,
void *(*)(void *), void *);
* */
struct mystruct mystruct1;
mystruct1.val=230;
pthread_t threads;
pthread_create(&threads,NULL,&func,&mystruct1);
pthread_exit(NULL);

return 0;
}

我建议你阅读pthread_create的实际手册。 http://man7.org/linux/man-pages/man3/pthread_create.3.html

关于c - 从主结构中提取子结构以创建线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39582968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com