0

哪种方式最好让 S 成为 GSourceFunc?或者两者都不是?

typedef struct{
//...
}S;

struct MS{
//..
S *St;
};

static gboolean AL_Calback(gpointer data){
    S *St = (S*)user_data;
    St->Something = SomethingElse;
    return TRUE;
} 

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

    //...
    MS *MainStruct = gnew0(MS, 1);
    Mainstruct->St = gnew0(S, 1);
    clutter_threads_add_timeout_full(G_PRIORITY_HIGH, 100, AL_Callback, MainStruct->St, NULL); 
    //...
    }

或者像这样,

typedef struct{
//...
}S;

struct MS{
//..
S St;
};

static gboolean AL_Calback(gpointer data){
    MS *MV = (MS*)user_data;
    MV->S.something = SomethingElse;    
    return TRUE;
} 

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

    //...
    MS *MainStruct = gnew0(MS, 1);
    clutter_threads_add_timeout_full(G_PRIORITY_HIGH, 100, AL_Callback, MainStruct, NULL); 
    //...
    }

我尝试了其他方法,但无法使它们起作用。clutter_add_timeout 需要将指针作为参数。

4

1 回答 1

1

如果您将参数传递给clutter_threads_add_timeout_Fullvia 指针,那么您可以只传递St成员的地址,MainStruct从而减少对动态分配的需求(对于内部结构)。

struct MainStruct{
  //..
  S St; // note: no pointer
};

// in main
MainStruct* ms = gnew0(MS, 1);
clutter_threads_add_timeout_Full(G_PRIORITY_HIGH, 100, AL_Callback, &(ms->St),
    NULL);

编辑:更新代码以动态分配MainStruct结构以避免 ptomato 指出的可能的段错误

于 2012-06-07T19:11:38.583 回答