向pthread_create()传递参数时出错



我正试图为pthread创建一个类似线程池的结构,以便为网络编程做相同的工作,这与这个问题非常相似。但是,当我试图将init()方法的参数传递给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.

threads的定义不正确。应该是:

pthread_t* threads[THREAD_NUM];

相关内容

  • 没有找到相关文章

最新更新