2012-09-18 33 views
0

我發現了一些回調簽名,如data_completion_tstring_completion_t。但我想知道zookeeper如何在異步API中調用這些函數。zookeeper如何在異步API中調用完整函數

它是否使用單線程來接收來自zookeeper的響應?所以我必須添加互斥體來保護回調中的user_data。還是每次調用另一個asyn API時都檢查回調?

回答

1

我測試了C mt庫,發現zookeeper爲IO和完成創建了另外兩個線程,在完成線程中調用完成函數,以便這些函數應該被順序調用。

2012-10-09 15:35:36,904:60028(0x7fff7d0f7180):[email protected][email protected]: Initiating client connection, host=127.0.0.1:3000 sessionTimeout=5000 watcher=0x0 sessionId=0 sessionPasswd=<null> context=0x0 flags=0 
2012-10-09 15:35:36,904:60028(0x7fff7d0f7180):[email protected][email protected]: starting threads... 
2012-10-09 15:35:36,905:60028(0x10af45000):[email protected][email protected]: started completion thread 
2012-10-09 15:35:36,905:60028(0x10aec2000):[email protected][email protected]: started IO thread 
2012-10-09 15:35:36,905:60028(0x7fff7d0f7180):[email protected][email protected]: Sending request xid=0x5073d3c9 for path [/mm/mmidc] to 127.0.0.1:3000 
2012-10-09 15:35:36,905:60028(0x10aec2000):[email protected][email protected]: initiated connection to server [127.0.0.1:3000] 
2012-10-09 15:35:36,909:60028(0x10aec2000):[email protected][email protected]: session establishment complete on server [127.0.0.1:3000], sessionId=0x13a43632d85000f, negotiated timeout=5000 
2012-10-09 15:35:36,909:60028(0x10aec2000):[email protected][email protected]: Calling a watcher for a ZOO_SESSION_EVENT and the state=ZOO_CONNECTED_STATE 
2012-10-09 15:35:36,909:60028(0x10af45000):[email protected][email protected]: Calling a watcher for node [], type = -1 event=ZOO_SESSION_EVENT 
2012-10-09 15:35:36,910:60028(0x10aec2000):[email protected][email protected]: Processing sync_completion with type=1 xid=0x5073d3c9 rc=-101 
zoo_set2: no node 

用於測試的代碼非常簡單,動物園管理員服務器都是一個獨立監聽本地主機:3000

int main() 
    { 

    zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); 

    const char* host = "127.0.0.1:3000"; 

    zhandle_t *zh; 
    clientid_t myid; 
    zh = zookeeper_init(host, NULL, 5000, &myid, NULL, 0); 

    struct Stat stat; 
    const char* line = "/test"; 
    const char* ptr = "hello, world"; 
    int ret = zoo_set2(zh, line, ptr, strlen(ptr), -1, &stat); 
    printf("zoo_set2: %s\n", zerror(ret)); 

    } 

其實,這些都是在文件中解釋。 http://zookeeper.apache.org/doc/r3.1.2/zookeeperProgrammers.html

C語言綁定

The C binding has a single-threaded and multi-threaded library. The multi-threaded library is easiest to use and is most similar to the Java API. This library will create an IO thread and an event dispatch thread for handling connection maintenance and callbacks. The single-threaded library allows ZooKeeper to be used in event driven applications by exposing the event loop used in the multi-threaded library.