這工作:構建一個std ::線程與功能
std::thread t = std::thread(printf, "%d", 1);
這不起作用:
t2 = std::thread(my_thread_func , std::ref(context));
OR
std::thread t1 = std::thread(my_thread_func , context_add);
my_thread_func定義:
int my_thread_func(long long *context_add)
背景是一些結構..我試圖 '通過引用傳遞'
錯誤的事:
function call missing argument list;
error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments
>>>編輯< < <
對不起混亂。 ..實際上我在公共MainPage中定義了my_thread_func,所以我不能使用native類型,因此我認爲值得嘗試很長時間並給出它的地址。
/* test data types, context for thread function, in .cpp (not in namespace) */
typedef struct _test_context
{
HANDLE hHandle;
unsigned int flag;
}test_context_t;
test_context_t *context;
//Do something with its member
context_add = (long long *)context;
std::thread t2 = std::thread(sem_waiting_thread, context_add);
錯誤:
error C3867: 'App1::MainPage::my_thread_func': function call missing argument list; use '&App1::MainPage::my_thread_func' to create a pointer to member
error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments
我的命名空間的樣子:
namespace App1
{
public ref class MainPage sealed
{
public:
MainPage();
public:
MainPage();
int my_thread_func(long long cotext);
..
};
}
< < < < EDIT 2 >>> 我現在很好奇..這個簡單的一個也不起作用!
void f1(int n)
{
for(int i=0; i<5; ++i) {
// Print n
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
.
.
.
int n=0;
std::thread t2(f1, n+1); (//This didn't work,same error!)
.
.
.
but this worked!
std::thread t2;
.
.
.
t2= std::thread (f1, n+1);
從這裏嘗試:http://en.cppreference.com/w/cpp/thread/thread/thread
的問題是有點混亂.. 。* context是一些struct *,但是在聲明中它是一個指向'long long'的指針......你可以發佈函數的實際聲明和你想傳入的類型嗎? – 2012-07-19 17:20:43
你應該展示一個完整的例子,在非工作的std :: thread構造函數行註釋掉的情況下編譯。然後有人可以告訴你如何正確地編寫該行。否則,我們必須猜測。儘管如此,我認爲betabandido確實在這種情況下能夠成功回答 - 但我們大多數人並不像他那樣通靈。 – abarnert 2012-07-19 17:22:00
你是什麼意思「我不能使用原生類型」?你爲什麼要將'context'投射到'long long'? – betabandido 2012-07-19 17:33:11