我有這樣的C++代碼,在那裏我嘗試創建一個並行線程,和我有4個錯誤:在pthread_create錯誤在C++(內部類並行線程)
任何人都可以請幫助?
在此先感謝。
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
static void* func(void*);
class Test
{
public:
pthread_t *threadId;
pthread_create(threadId, NULL, func, NULL);
};
static void* func(void *arg)
{
printf("Thread function called\n");
}
int main()
{
Test();
}
編譯:
# g++ simplepThread.cc -lpthread
simplepThread.cc:11: error: ‘threadId’ is not a type
simplepThread.cc:11: error: expected identifier before ‘__null’
simplepThread.cc:11: error: expected ‘,’ or ‘...’ before ‘__null’
simplepThread.cc:11: error: ISO C++ forbids declaration of ‘pthread_create’ with no type
如果我使用的線程功能爲 「C」 鏈接:
extern "C" void* func(void *arg)
{
printf("Thread function called\n");
}
錯誤面向的是:
simplepThread.cc:7: error: previous declaration of ‘void* func(void*)’ with ‘C++’ linkage
simplepThread.cc:15: error: conflicts with new declaration with ‘C’ linkage
爲什麼要在類聲明中調用函數? – kennytm 2011-04-28 08:30:41
我的設計要求是什麼解決方案。我會糾正這個? – kingsmasher1 2011-04-28 08:31:24
@ kingsmasher1:這是無效的C++。單詞中的設計要求是什麼? – kennytm 2011-04-28 08:32:29