簡體   English   中英

從主結構提取子結構以創建線程

[英]Extract sub struct from main struct for thread creation

我在嘗試將多個參數發送到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以便能夠捕獲整個大參數,並且我試圖在此重新映射所有內容,計數工作正常,但是一個稱為record的記錄(實際上是另一個結構)不起作用,看起來像演員一樣

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前面的轉換(結構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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM