繁体   English   中英

ALooper 回调函数同步

[英]ALooper callback functions syncronization

我在 C 中有一个带有 2 个管道和 2 个回调函数的 ALooper,

mainThreadLooper = ALooper_forThread(); // get looper for this thread
ALooper_acquire(mainThreadLooper); // add reference to keep object alive
pipe(messagePipe); 
pipe(commandPipe);

ALooper_addFd(mainThreadLooper, messagePipe[0],
        0, ALOOPER_EVENT_INPUT, looperCallback, NULL);
ALooper_addFd(mainThreadLooper, commandPipe[0],
        0, ALOOPER_EVENT_INPUT, commandLooperCallback, NULL);

这是 2 个正常工作的回调函数

static int commandLooperCallback(int fd, int events, void* data){
    LOGD("command pipe callback");
    wchar_t c;
    read(fd, &c,sizeof(c));
    if(running){
        if(c == 0){
            return 1;
        }
        if(c == CLEAR_SCREEN){
            LOGD("clear screen");
            clearScreen();
            return 1;
        }
    }

    return 1;
}


static int looperCallback(int fd, int events, void* data) {
    //char msg[100];
    wchar_t msg[100];

    int length = read(fd, &msg,sizeof(msg)); //99); // read message from pipe
    LOGD("string recieved: %ls", msg);
    char str[length];
    wcstombs(str,msg,length);
    LOGD("char string %s", str);
  //msg[length] = 0;
    if(running){
        printToTextView(str);
    }

    //LOGD("returning from looper callback");
    return 1; // continue listening for events
}

问题是它们在我的代码中被无序调用,导致在与我在代码中写入管道时不同的时间出现clearScreen()

如果有人知道如何同步这些 alooper 回调,请填写我。我使用 ALooper 因为我在 C 中使用 pthread 并且对管道的写入是从 pthread 发生的,而 Alooper 是我回调到 android 中的 UI 线程的方式

感谢您的时间

我就是这样结束的。

在我的带有回调的文件中

_Atomic bool command = false;
_Atomic bool writing = false;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

然后在我的回调中我把这个

static int commandLooperCallback(int fd, int events, void* data){
    if(!command){
        return 1;
    }
    LOGD("command pipe callback");
    wchar_t c;
    read(fd, &c,sizeof(c));
    if(running){
        if(c == 0){
            return 1;
        }
        if(c == CLEAR_SCREEN){
            LOGD("clear screen");


            clearScreen();
            command = false;
            pthread_mutex_lock(&lock);
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&lock);
            //wait = false;
            return 1;
        }
    }

    return 1;
}

static int looperCallback(int fd, int events, void* data) {
    //char msg[100];
    if(!writing){
        return 1;
    }
    wchar_t msg[100];

    int length = read(fd, &msg,sizeof(msg)); //99); // read message from pipe
    LOGD("string recieved: %ls", msg);
    char str[length];
    wcstombs(str,msg,length);
    LOGD("char string %s", str);
  //msg[length] = 0;
    if(running){
        printToTextView(str);
    }
    writing = false;
    pthread_mutex_lock(&lock);
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&lock);
    //LOGD("returning from looper callback");
    return 1; // continue listening for events
}

然后取决于我写的是哪个管道

//and opposite for commandPipe and making command bool true
write(messagePipe[1],NameBuffer, sizeof(NameBuffer));//strlen(s));
    writing = true;
    pthread_mutex_lock(&lock);
    pthread_cond_wait(&cond, &lock);
    pthread_mutex_unlock(&lock);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM