pthread_create的返回值

ona*_*000 5 c pthreads

我正在尝试拨打以下电话,

PID = pthread_create(&t, NULL, schedule_sync(sch,t1), NULL);
Run Code Online (Sandbox Code Playgroud)

schedule_sync返回一个值,我希望能够获取该值,但是根据我对pthread_create的了解,您应该传递一个“ void”函数。是否有可能获得schedule_sync的返回值,还是我将不得不修改传入的某种参数?

谢谢您的帮助!

Pot*_*ter 5

pthread_create返回一个<errno.h>代码。它不会创建新进程,因此没有新的PID。

要将指针传递给函数,请使用来获取其地址&

pthread_create具有形式功能void *func( void * )

所以假设schedule_sync是线程函数,

struct schedule_sync_params {
    foo sch;
    bar t1;
    int result;
    pthread_t thread;
} args = { sch, t1 };

int err = pthread_create( &args.thread, NULL, &schedule_sync, &args );
 .....

schedule_sync_params *params_ptr; // not necessary if you still have the struct
err = pthread_join( args.thread, &params_ptr ); // just pass NULL instead
 .....

void *schedule_sync( void *args_v ) {
   shedule_sync_params *args = args_v;
   ....
   args->result = return_value;
   return args;
}
Run Code Online (Sandbox Code Playgroud)