分页: 1 / 1

谁能告诉我,这段C语言代码到底哪里的语法出错啦?

发表于 : 2013-03-17 22:07
thlgood
代码在这里:

代码: 全选

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

void route(void *arg)
{
    printf("Yes, Yes, Yes!\n");
}

void *func(void *arg)
{
    pthread_detach(pthread_self());

    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    pthread_cleanup_push(route, NULL);
    while(1)
    {
        sleep(1);
    }
}

int main()
{
    pthread_t pt_one;
    int return_value;
    return_value = pthread_create(&pt_one, NULL, func, NULL);
    sleep(10);
    pthread_cancel(pt_one);

    return 0;
}

编译的时候老是通不过,错误提示是

Re: 谁能告诉我,这段C语言代码到底哪里的语法出错啦?

发表于 : 2013-03-17 22:12
thlgood
已经查出问题了,注释pthread_cleanup_push(route, NULL);语句就可以了。。。但是我现在必须要用到这个函数啊。。

Re: 谁能告诉我,这段C语言代码到底哪里的语法出错啦?

发表于 : 2013-03-17 22:16
cuihao

代码: 全选

/* Install a cleanup handler: ROUTINE will be called with arguments ARG
   when the thread is canceled or calls pthread_exit.  ROUTINE will also
   be called with arguments ARG when the matching pthread_cleanup_pop
   is executed with non-zero EXECUTE argument.

   pthread_cleanup_push and pthread_cleanup_pop are macros and must always
   be used in matching pairs at the same nesting level of braces.  */
#  define pthread_cleanup_push(routine, arg) \
  do {                                                                        \
    __pthread_cleanup_class __clframe (routine, arg)
嗯,具体用法我不懂,但根据头文件里面的注释,pthread_cleanup_push和pthread_cleanup_pop必须成对出现。

Re: 谁能告诉我,这段C语言代码到底哪里的语法出错啦?

发表于 : 2013-03-17 22:17
thlgood
找到原因了,pthread_cleanup_push()和pthread_cleanup_pop()必须成对地使用,因为两个宏里面分别有“{”和“}”