Errore durante il passaggio di argomenti per pthread_create()

0

Domanda

Sto cercando di creare un Pool di Thread-come la struttura per pthreads a che fare identici posti di lavoro per la programmazione di rete, che è molto simile a questa domanda. Tuttavia, si è verificato un problema, come ho cercato di passare gli argomenti del init() metodo per pthread_create().

Codice

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);
        }
    }
}

Messaggio di errore

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.
arguments c++ macos pthreads
2021-11-23 11:10:08
1

Migliore risposta

2

La definizione di threads non è corretto. Dovrebbe essere:

pthread_t* threads[THREAD_NUM];
2021-11-23 20:16:38

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................