2013-02-22 66 views
0
typedef struct client 
{ 
    pthread thread; 
    Window_t *win 
}client; 

client * client_create(int ID) 
{ 
    client *new_Client = (client *) malloc(sizeof(client)); 
    char title[16]; 

    if (!new_Client) return NULL; 

    sprintf(title, "Client %d", ID); 

    /* Creates a window and set up a communication channel with it */ 
    if ((new_Client->win = window_create(title))) 
     return new_Client; 
    else { 
     free(new_Client); 
     return NULL; 
    } 
} 

當用戶輸入'e'時,我嘗試用新客戶端和窗口創建一個新線程,這樣做是我的int_main。 標誌只是告訴我,用戶輸入êC pthread_create

if(eflag == 1) 
{  
    client *c = NULL; 
    c=client_create(started); 
    pthread_create(&c.thread, NULL, client_create, (void *) &started); 
    started++; 
    eflag =0; 
} 

這應該是上一個新的窗口,一個新的線程創建一個新的客戶端,但它沒有做到這一點。

我不知道該怎麼把我的pthread_create,以及我怎麼得到一個新的客戶端實例,因爲client_create函數創建一個新的窗口。當我嘗試通過執行pthread_create創建一個新線程時,它也會創建一個新窗口...如果這是java每當用戶按下'e'時,我只會創建一個類客戶端的新實例...但是我可以'這裏真的這樣做。有什麼建議麼?

+0

你的代碼是做什麼的?這是什麼* window_create *函數? – hyde 2013-02-22 05:06:23

+0

最初這是一個單線程程序。 window_createe創建一個用戶可以與數據庫交互的新窗口,。我試圖做到這一點,所以你可以擁有儘可能多的用戶 – user1665569 2013-02-22 05:14:08

回答

1

的在pthread_create函數的原型是

int pthread_create(pthread_t *restrict thread, 
       const pthread_attr_t *restrict attr, 
       void *(*start_routine)(void*), void *restrict arg); 

您的start_routine定義需要匹配無效*(*的start_routine)(無效*)......,它是在一個新的線程上下文中執行。

void *my_client_routine(void *arg) { 
    while(1) { 
     // do what each client does. 
    } 
} 

當前在您的main()中,您正在執行兩次create_client函數。一次在調用pthread_create()之前,一次作爲從pthread_create產生的新線程的一部分。你可能想要做的是

c = create_client() 
    pthread_create(&c.thread, NULL, my_client_routine, &args_that_client_routine_needs);