C++, create a pthread for a function with a return type?

后端 未结 4 1770
忘掉有多难
忘掉有多难 2021-02-06 19:26

Say I have the following function:

bool foo (int a); // This method declaration can not be changed.

How do I create a pthread for this? And how

4条回答
  •  无人共我
    2021-02-06 20:12

    A bool is functionally equivalent to an int. false is (usually) 0, and true is anything else. Thus

    void *foo(void *a){
      int *a_int = (int *)a;
      //do things
      bool *x = new bool;
      *x = answer; 
      pthread_exit(x);
    }
    

    Then in your main you would get the returned result by casting it back to a bool.

    bool *x; 
    pthread_join(thread,(void *)x);
    //Answer is *x
    

提交回复
热议问题