在z节点中存储2d数组

时间:2014-08-14 11:54:56

标签: c arrays apache-zookeeper

我试图在zookeeper znode中存储一个2d整数数组,但在检索它时,不会返回预期的数据。

以下是代码:

#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <zookeeper.h>
#include <pthread.h>

static zhandle_t *zh;
static int batchMode=0;
static int shutdownThisThing=0;

pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;

struct timeval startTime;
struct Stat stat;

char* buffer;
int* buff_len;

int rc = 0;

int size = 12;

char *foo_get_cert_once(char* id) { return 0; }

void my_data_completion(int rc, const char *value, int value_len,
    const struct Stat *stat, const void *data) {

    int *int_value = (int *) value;     //data received from the z-node
    int *int_data = (int *) data;       //data received from the caller
    int i=0,j=0;
    for(i=0;i<size*size;i++)
        fprintf(stderr, "%d ",*(int_value+i) );
    fprintf(stderr,"\n");
    for(i=0;i<size*size;i++)
        fprintf(stderr, "%d ",*(int_data+i) );
}

void watcher(zhandle_t *zzh, int type, int state, const char *path, void *watcherCtx) {

    fprintf(stderr, "Inside watcher function. Here type is %d and state is %d\n", type, state);
    zoo_exists(zh, "/pravesh", 1, &stat);
}

/**
Function called in a separate thread.
Ignite wait here.
**/
void *waiting(void *arg){

    fprintf(stderr, "Inside waiting function\n");
    pthread_mutex_lock(&count_mutex);
    pthread_cond_wait(&count_threshold_cv, &count_mutex);
    pthread_mutex_unlock(&count_mutex);
    pthread_exit(NULL);
}

int main() {

    char buffer[512];
    char p[2048];
    char *cert=0;
    char appId[64];

    int string[size][size];
    int i=0,j=0,k=0;

    for(i=0;i<size;i++)
        for(j=0;j<size;j++)
            string[i][j]=k++;

    for(i=0;i<size;i++)
        for(j=0;j<size;j++)
            fprintf(stderr, "%d ", string[i][j]);
    fprintf(stderr, "\n");

    int end = 0;
    int rc = 0;

    zoo_set_debug_level(ZOO_LOG_LEVEL_INFO);

    zh = zookeeper_init("localhost:2181", watcher, 10000, 0, 0, 0);
    if (!zh) {
        return errno;
    }

    int buflen= sizeof(buffer);

    zoo_set(zh, "/pravesh", (const char*)string, size*size, -1);                //sets the data in the z-node
    zoo_aget(zh, "/pravesh", 1, my_data_completion, (const char*)string );      //gets the data from the z-node and passes it to my_data_completion

/**
 * Trying to implement waiting functionality using threads
 **/

    fprintf(stderr, "Starting threads\n");
    pthread_t thread;
    pthread_mutex_init(&count_mutex, NULL);
    pthread_cond_init (&count_threshold_cv, NULL);
    pthread_create(&thread, NULL, &waiting, NULL);

    fprintf(stderr, "Waiting function has been called\n");

    pthread_mutex_destroy(&count_mutex);
    pthread_cond_destroy(&count_threshold_cv);
    pthread_exit(NULL);
}

打印数据后的输出正确(数字0-143)但打印后的输出是意外的(如下所示):

  

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26   27 28 29 30 31 32 33 34 35 0 0 1946159936 32538 176 0 37 0 1946159744   32538 21 25 0 0 132017 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

实施有问题吗?

0 个答案:

没有答案