2015-04-12 78 views
1

而且我必須在一個函數中創建一個線程,也就是(線程)在一個無限循環中等待連接並接受來自另一個進程的消息(因此,該線程將作爲一個tcp服務器工作),並且每當消息到來時,它都必須使用?或那種來電或不管它作爲一個參數傳遞的功能,線程創建的功能。如何在一個用作tcp服務器的線程中使用帶函數參數的函數

所以這是函數頭

int init(void (*notif)(const char *, const char *), 
      void (*parameter1)(const char *), 
      void (*parameter2)(const char *)); 

所以,我已經看到了這個帖子How do you pass a function as a parameter in C?它有助於獲得這個想法,但我完全不確定如何在線程內部完成。

及其產生的原因是因爲我沒有什麼可以與內螺紋服務器沒有經驗,我發現這個代碼,這是非常有幫助的https://gist.github.com/silv3rm00n/5821760

但它的工作方式,所有的代碼是在函數(所以在線程之外),並且線程作爲用於連接的套接字的唯一參數。我已經看到過幾次類似這樣的用法。

然後我懷疑:將服務器的所有代碼放入線程中是否合理?或者這樣做沒什麼意義,線程會太「沉重」,最好在這種情況下進行。

如果問題措辭不當或我沒有正確解釋自己,我很抱歉,對於這類問題,我幾乎沒有線程經驗和0經驗。

編輯: 這一切都是那麼遠,但我想的東西我有,這是行不通的:

void (*funcionParametroThread)(const char *, const char *); 
funcionParametroThread=(*notif_evento)(const char *, const char *); 
pthread_create(&thid, &atrib_th, tcp_server, funcionParametroThread); 

我試圖使它類似下面的代碼是一個答案從鏈接上述

問題
int dosomethingwithchar(char a) { return 1; } 
functiontype2 func2 = &dosomethingwithchar 
int result = func2('a'); 

回答

2

線程(Linux的並行線程)(記住,我寫的飛行保留),因爲通常這個簽名:

void * threadName(void *parms) 

它由函數pthread_create啓動,它啓動它傳遞參數。

typedef void * THFN_t(void *parms) 

struct thStruct { 
volatile int a; //volatile because the value may change during inter-process! 
volatile int b; 
volatile int c; 
}; 

void * thread(struct thStruct *k) 
{ 
    for(;;) { 
     /* do your job */ 
    } 

    pthread_exit(NULL); 
} 

int main(void) 
{ 
    struct thStruct s; 

    s.a=1; //These structure will be passed to the thread 
    s.b=2; 
    s.c=3; 

    pthread_attr_t attr; 
    pthread_t thr; 
    pthread_attr_init(attr); 
    if (pthread_create(
       (pthread_t*)&thr, 
       (pthread_attr_t*)&attr, 
       (THFN_t *)thread, //The function above 
       (void *)&s)) { 
        perror("Error"); 
        return errno; 
     } 

     while(s.a!=0) { //Used to lock the main, but also to pass something to the thread! 
     printf("Insert a number: "); 
     scanf("%d",s.a); 
     } 
} 

這是包含提示以解釋如何將信息傳遞給正在運行的線程的框架。請記住volatile,並記住一些更改可以作爲原子來執行(您可以使用互斥鎖以正確方式更改它們)。

我知道你需要通知你的線程它必須調用一個函數,並且你需要這個函數是一個參數。

我希望這個函數將有一個固定數量的參數...:)

我們說,我們將調用函數都會有這樣的簽名:

int fnname(int a, char *b); 

然後我們宣佈(我喜歡這樣):

typedef int FNX(int a, char *b); 

然後我們插入結構thStruct的元素,它指定了我們想要調用的函數的指針。

struct thStruct { 
volatile int a; //volatile because the value may change during inter-process! 
volatile int b; 
volatile int c; 
volatile FNX * fn; //Function pointer 
}; 

現在我們需要的功能:

int functionX(int a, char *b) 
{ 
    printf("%d %s\n",a,b); 
} 

下一步:在主,在這裏我們初始化thStruct,我們要發出信號的功能信息在特定事件發生時,線程將被調用(例如Sa成爲5)

確定:

我們添加:

s.fn=functionX; 

現在我們修改線程:

void * thread(struct thStruct *k) 
{ 
    for(;;) { 
     if (k->a==5) { 
      k->fn(a,"Yeah!\n"); 
      k->a=4; //To avoid continue calling k->fn 
     } 
     sched_yield(); //To avoid useless use of CPU time 
         //(this thread does nothing and waits for nothing!) 
    } 

    pthread_exit(NULL); 
} 

現在,如果程序不包含錯誤,你可以編譯它(我想,如果發生錯誤,你可以解決),並運行。

當它運行時,當main詢問你一個數字和函數functionX時,你可以插入5!

我怎麼說你呢,我已經編寫了這段代碼來幫助你。它可能包含錯誤,但它應該能夠解釋你需要的一些基本的東西。還有更多,但我認爲這給你一個好方法!

這是結束! (大門)

+0

我閱讀和重新閱讀這一點,因爲有些東西是有點複雜,但我想我慢慢變了,所以即使在完成之前,我想說謝謝!我已經在上面了。 – keont

+0

如果您有任何問題可以免費給我寫信([email protected]),但不要以我的耐心解答! :p –

+0

一個例子:製作一個明喻,而不是數字5,它可以是一個數字,就像一個接受者在連接到達時給出的數字,並且在我要接收和發送的線程內(根據需要),然後調用funcion用你寫的語法, k-> fn(a,「是啊!\ n「); – keont

0

我已經根據所給的鏈接和Sergio Formiggini的令人難以置信的詳細崗位做:

功能:

int init(void (*notif)(const char *, const char *), 
     void (*parameter1)(const char *), 
     void (*parameter2)(const char *)); 

裏面是:

void (*funcionParametroThread)(const char *, const char *); 
funcionParametroThread=&(*notif); 

並創建線程:

pthread_create(&thid, &atrib_th, tcp_server, funcionParametroThread); 

一旦我使用的線程功能(tcp_server)內:

void (*funcionParametroThread)(const char *, const char *); 
    funcionParametroThread=arg; 
    (*funcionParametroThread)(temaRecibido,valorRecibido); 
相關問題