【问题标题】:passing a pointer to a structure as an argument to a thread cancellation cleanup handler将指向结构的指针作为参数传递给线程取消清理处理程序
【发布时间】:2009-11-13 21:03:06
【问题描述】:

我无法将指向结构的指针作为参数传递给线程取消清理处理程序。这是一些示例代码,当它遇到编译器时会爆炸。知道我做错了什么吗?

#include <pthread.h>

typedef struct struct_def {
     /* data */
} struct_def;

struct_def *ptr_to_struct_def;

void *
thread_function(void *arg)
{
     pthread_cleanup_push(cleanup, (void *)ptr_to_struct_def); /* correct? */

     /* function continues */
}

int
main()
{
     int err;
     pthread_t tid;

     err = pthread_create(&tid1, NULL, thread_function, (void *)1);

     /* main continues */
}

【问题讨论】:

    标签: c posix pthreads


    【解决方案1】:

    主要问题是您错过了对

    的调用
    pthread_cleanup_pop(0)
    

    在“功能继续”注释之后。这将使编译器失败。在opengroup.org 使用 push/pop 查看示例代码。

    正如其他答案中指出的那样,您还有一些其他问题。

    在修复所有编译器错误后,至少可以编译

    #include <pthread.h>
    
    typedef struct struct_def {
             /* data */
    } struct_def;
    
    struct_def *ptr_to_struct_def;
    
    void cleanup (void *arg)
    {
          /* Do your cleanup for the thread here */
    }
    
    void *
    thread_function(void *arg)
    {
        pthread_cleanup_push(cleanup, (void *)ptr_to_struct_def); /* correct? */
        /* Function continues */
        pthread_cleanup_pop (0);
    }
    
    int
    main()
    {
        int err;
        pthread_t tid;
    
        err = pthread_create(&tid, NULL, thread_function, (void *)1);
        /* main continues */
    }
    

    【讨论】:

      【解决方案2】:

      我在您发布的代码中没有看到分配 ptr_to_struct_def = &amp;struct_def;

      等等……你是说编译器吗?如果这不能编译 - 发布编译器错误。虽然我不认为你是那个意思。

      好的,你的意思是:)感谢@Gonzalo,我查看了/usr/include/pthread.h,果然推送/弹出是宏(至少在Linux上):

      
      #  define pthread_cleanup_push(routine, arg) \
        do {                                        \
          __pthread_cleanup_class __clframe (routine, arg)
      ...
      #  define pthread_cleanup_pop(execute) \
          __clframe.__setdoit (execute);                        \
        } while (0)
      

      多么令人不快的惊喜……

      【讨论】:

      【解决方案3】:
      typedef struct
      {
      
      } struct_def;
      
      struct_def *ptr_to_struct_def;
      
      void * thread_function(void *arg)
      
      int main()
      {
           int err,iResult;
           pthread_attr_t sThreadAttr;
           iResult = pthread_attr_init(&sThreadAttr);
           assert(iResult==0);
      
           pthread_t tid;
      
           err = pthread_create(&tid1, &sThreadAttr, thread_function, (void *)&ptr_to_struct_def);
      
       iResult = pthread_join(sThread, NULL);
        assert(iResult==0);
      
           /* main continues */
      #else
      
      thread_function();
      #endif
      
        // the end
        return 0;
      }
      
      void * thread_function(void *arg)
      {
      }
      

      【讨论】:

        【解决方案4】:

        使用该代码,ptr_to_struct_def 未定义/未初始化,无法工作。它应该指向struct_def 的某个实例。

        另外,我认为将(void*)1 传递给未使用的参数是很不常见的,如果你不使用该值,只需使用0

        【讨论】:

          【解决方案5】:
          pthread_cleanup_push(cleanup, (void *)ptr_to_struct_def);
          //-------------------^^^^^^^  where is this function defined/declared?
          

          【讨论】:

            【解决方案6】:

            你不需要指针(注意我删除了 typedef):

            typedef struct struct_def {
             /* data */
            } struct_def;
            
            struct_def *ptr_to_struct_def;
            
            void *
            thread_function(void *arg)
            {
                 pthread_cleanup_push(cleanup, (void *)ptr_to_struct_def); /* correct? */
            
                 /* function continues */
            }
            
            int
            main()
            {
                 int err;
                 pthread_t tid;
            
                 err = pthread_create(&tid1, NULL, thread_function, (void *)1);
            
                 /* main continues */
            }
            

            【讨论】:

              猜你喜欢
              • 2013-09-02
              • 2012-01-28
              • 2013-11-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-07-17
              • 1970-01-01
              相关资源
              最近更新 更多