[英]Error when passing arguments to pthread_create()
I am trying to create a Thread-Pool-like structure for pthread
s to do identical jobs for network programming, which is very similar to this question .我正在尝试为pthread
创建一个类似线程池的结构,以便为网络编程执行相同的工作,这与这个问题非常相似。 However, a problem occurred as I tried to pass the arguments of the init()
method to pthread_create()
.但是,当我尝试将init()
方法的 arguments 传递给pthread_create()
时出现问题。
class ThreadPool{
public:
BlockingQueue socket_bq;
pthread_mutex_t* threads[THREAD_NUM]; // store the pointer of threads
ThreadPool();
void init(pthread_attr_t* attr, void*start_routine(void*), void* arg);
};
void ThreadPool::init(pthread_attr_t* attr, void*start_routine(void*), void* arg){
// create threads
for(int i = 0; i < THREAD_NUM; i++){
if(pthread_create(threads[i], attr, start_routine, arg)!=0){
fprintf(stderr, "Thread Pool Init: falied when creatng threads");
exit(1);
}
}
}
error: no matching function for call to 'pthread_create' if(pthread_create(threads[i], attr, start_routine, arg).=0){ ^~~~~~~~~~~~~~ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread:h:329:5: note: candidate function not viable, no known conversion from 'pthread_mutex_t *' (aka '_opaque_pthread_mutex_t *') to 'pthread_t _Nullable * _Nonnull' (aka '_opaque_pthread_t **') for 1st argument int pthread_create(pthread_t _Nullable * _Nonnull __restrict. ^ 1 error generated.错误:没有匹配的 function 调用 'pthread_create' if(pthread_create(threads[i], attr, start_routine, arg).=0){ ^~~~~~~~~~~~~~ /Library/Developer/ CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread:h:329:5:注意:候选 function 不可行,没有从 'pthread_mutex_t *'(又名 '_opaque_pthread_mutex_t *')到 'pthread_t _Nullable * _Nonnull' 的已知转换(又名 '_opaque_pthread_t **') 用于第一个参数 int pthread_create(pthread_t _Nullable * _Nonnull __restrict。^ 1 个错误生成。
The definition of threads
is likely incorrect. threads
的定义可能不正确。 It should be:它应该是:
pthread_t* threads[THREAD_NUM];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.