類中的p線程函數。假設我有一個類似的類class c {
// ...
void *print(void *){ cout << "Hello"; }}然后我有一個c的向量vector<c> classes; pthread_t t1;classes.push_back(c());classes.push_back(c());現在,我想在c.print();以下是我的問題:pthread_create(&t1, NULL, &c[0].print, NULL);錯誤輸出:無法轉換‘void*(tree_tem:)(無效)“to”無效*()(無效)‘for參數’3‘to’int pline_create(p線程_t*,const p線程_attr_t*,void*()(無效),無效*‘
3 回答
臨摹微笑
TA貢獻1982條經驗 獲得超2個贊
thispthread_create()thisthis
class C{public:
void *hello(void)
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
static void *hello_helper(void *context)
{
return ((C *)context)->hello();
}};...C c;pthread_t t;pthread_create(&t, NULL, &C::hello_helper, &c);
至尊寶的傳說
TA貢獻1789條經驗 獲得超10個贊
class MyThreadClass{public:
MyThreadClass() {/* empty */}
virtual ~MyThreadClass() {/* empty */}
/** Returns true if the thread was successfully started, false if there was an error starting the thread */
bool StartInternalThread()
{
return (pthread_create(&_thread, NULL, InternalThreadEntryFunc, this) == 0);
}
/** Will not return until the internal thread has exited. */
void WaitForInternalThreadToExit()
{
(void) pthread_join(_thread, NULL);
}protected:
/** Implement this method in your subclass with the code you want your thread to run. */
virtual void InternalThreadEntry() = 0;private:
static void * InternalThreadEntryFunc(void * This) {((MyThreadClass *)This)->InternalThreadEntry(); return NULL;}
pthread_t _thread;};
12345678_0001
TA貢獻1802條經驗 獲得超5個贊
pthread_create
cpthread_createc.
static void* execute_print(void* ctx) {
c* cptr = (c*)ctx;
cptr->print();
return NULL;}void func() {
...
pthread_create(&t1, NULL, execute_print, &c[0]);
...}- 3 回答
- 0 關注
- 584 瀏覽
添加回答
舉報
0/150
提交
取消
