簡體   English   中英

如何將模板 function 傳遞給 POSIX 線程(pthread)

[英]How to pass template function to POSIX threading (pthread)

我在使用帶有 POSIX 線程的模板 function 時遇到了麻煩。 作為編程語言,我使用 C++98,作為線程基礎架構,我使用 POSIX 線程 ( <pthread.h> )。

我的環境不支持標准線程( <thread>

通常,我們可以將參數傳遞給 pthreads,如下所示:

#include <stdio.h>
#include <pthread.h>

void * hello(void *input) {
    printf("%s\n", (char *)input);
    pthread_exit(NULL);
}

int main(void) {
    pthread_t tid;
    pthread_create(&tid, NULL, hello, "hello world");
    pthread_join(tid, NULL);
    return 0;
}

問題是,如何將模板 function 和參數傳遞給 pthreads?

這是示例模板 function:

#include <pthread.h>

template<class T>
void *hello(T val){
    SampleInterface *interface = &T;
    cout << interface->Message<<endl;
}

int main(void) {
    pthread_t tid;
    SampleClass sClass;
    pthread_create(&tid, NULL, hello, sClass);
    pthread_join(tid, NULL);
    return 0;
}

如果我使用上述用法,我會收到兩個錯誤:

error: no matches converting function 'hello' to type 'void* (*)(void*)'
error: candidates are: template<class T> void* hello(T)

由於您沒有直接調用hello() ,編譯器無法為您推導出模板參數,因此您必須顯式指定模板參數。

您還需要更改hello()以匹配pthread_create()所期望的簽名(就像您的非模板示例一樣),除非您在將 function 傳遞給pthread_create() (我沒有'不推薦這樣做,因為它在技術上是未定義的行為,但它在某些編譯器中確實有效)。

嘗試這個:

#include <iostream>
#include <pthread.h>

template<class T>
void *hello(void *arg){
    T *val = static_cast<T*>(arg);
    SampleInterface *interface = val;
    cout << interface->Message << endl;
    return NULL;
}

int main(void) {
    pthread_t tid;
    SampleClass sClass;
    pthread_create(&tid, NULL, hello<SampleClass>, &sClass);
    pthread_join(tid, NULL);
    return 0;
}

或者:

#include <iostream>
#include <pthread.h>

typedef void* (*pthread_callback_t)(void*);

template<class T>
void *hello(T *val){
    SampleInterface *interface = val;
    cout << interface->Message << endl;
    return NULL;
}

int main(void) {
    pthread_t tid;
    SampleClass sClass;
    pthread_create(&tid, NULL, reinterpret_cast<pthread_callback_t>(hello<SampleClass>), &sClass);
    pthread_join(tid, NULL);
    return 0;
}

或者,您根本不需要示例中的模板,因此您可以稍微簡化代碼:

#include <iostream>
#include <pthread.h>

void *hello(void *arg){
    SampleInterface *interface = static_cast<SampleInterface*>(arg);
    cout << interface->Message << endl;
    return NULL;
}

int main(void) {
    pthread_t tid;
    SampleClass sClass;
    SampleInterface *interface = &sClass;
    pthread_create(&tid, NULL, hello, interface);
    pthread_join(tid, NULL);
    return 0;
}

或者:

#include <iostream>
#include <pthread.h>

typedef void* (*pthread_callback_t)(void*);

void *hello(SampleInterface *interface){
    cout << interface->Message << endl;
    return NULL;
}

int main(void) {
    pthread_t tid;
    SampleClass sClass;
    SampleInterface *interface = &sClass;
    pthread_create(&tid, NULL, reinterpret_cast<pthread_callback_t>(hello), interface);
    pthread_join(tid, NULL);
    return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <iostream>

struct SampleInterface {
    int Message;
};

struct SampleClass : public SampleInterface {
    SampleClass() {
        Message = 42;
    };
};

template<class T>
void *hello(void* input){
    SampleInterface *interface = (T*) input;
    std::cout << interface->Message << std::endl;
    pthread_exit(NULL);
    return NULL;
}

int main(void) {
    pthread_t tid;
    SampleClass sClass;
    SampleInterface* pInterface = &sClass; //see  Remy Lebeau's comment why we should not pass sClass directly to pthread create
    pthread_create(&tid, NULL, hello<SampleInterface>, (void*) pInterface);
    pthread_join(tid, NULL);
    return 0;
}

這將編譯並運行,嘗試在此處粘貼此代碼示例http://cpp.sh/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM