如何在终止线程之前将线程数据复制到数组?

问题描述 投票:0回答:1

我试图从一个方法中复制C中的线程数据,该方法使用指向结构类型数组的指针引用线程的结构。

我试图使用“&”符号来获取结构数据,但是这样做时收到了make错误。我想在结束类型的线程终止之前复制整个struct的数据。

Person queue[300];
Person statsArray[300];
// the queue contains Person structs that have been given data already
//      within another method, prior to calling Leave().

typedef struct
{
struct timeval startChange;
struct timeval endChange;
struct timeval arrive;

int id;
int changingTime;
int storeTime;
int returning;
int numVisits;
int type;
int queuePos;
} Person;

void Leave(int queuePosition)
{
Person *aPerson = &queue[queuePosition];

statsArray[statsArrayIndex] = &aPerson;
statsArrayIndex++;
}

在编译时,我从'Person ** {aka struct **}'类型中分配到类型'Person {aka struct}'时出现“不兼容类型”的错误

c linux multithreading struct copying
1个回答
1
投票

根据错误消息,有问题的行是:

statsArray[statsArrayIndex] = &aPerson;

你在哪里指定Person**Person。如果要复制每个struct元素,那么您可能需要:

statsArray[statsArrayIndex] = *aPerson;

请注意,对于大型结构数组,struct复制可能很昂贵。根据您的程序,可能更好/可能重新设计您的程序,以便不复制并只使用指针(例如,不要让线程销毁queue)。

© www.soinside.com 2019 - 2024. All rights reserved.