Reputation: 1682
This works:
std::thread t = std::thread(printf, "%d", 1);
This doesn't work:
t2 = std::thread(my_thread_func , std::ref(context));
OR
std::thread t1 = std::thread(my_thread_func , context_add);
my_thread_func definition :
int my_thread_func(long long *context_add)
context is some struct .. and I'm trying to do 'pass by reference'
Error:
function call missing argument list;
error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments
>>> EDIT <<<
Sorry for confusion ... actually I'm defining my_thread_func in public of MainPage, so I cannot use native type hence I thought worth trying for long and give it address.
/* 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:
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
my namespace looks like:
namespace App1
{
public ref class MainPage sealed
{
public:
MainPage();
public:
MainPage();
int my_thread_func(long long cotext);
..
};
}
<<<< EDIT 2 >>> I'm curious now .. this simple one also doesn't work!
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);
Trying from here: http://en.cppreference.com/w/cpp/thread/thread/thread
Upvotes: 1
Views: 4498
Reputation: 19704
It would be better to define your function as:
int my_thread_func(context& ctx);
Then you would be able to pass a reference to a context
. If the function is not supposed to modify the context
then it is better to use:
int my_thread_func(const context& ctx);
Then you can create the thread like:
test_context_t context;
/* ... */
std::thread t = std::thread(my_thread_func , std::ref(context));
From your code it seems you have a pointer to a context
. You might want to reconsider that and just use an object instance as I do above. If the context is passed to a function as a pointer you might want to change that pointer to a reference too. But if that is not possible (or desirable) then you can still create the thread by doing:
test_context_t* context;
/* ... */
std::thread t = std::thread(my_thread_func , std::ref(*context));
Alternatively, you can just use plain pointers:
int my_thread_func(context* ctx); // notice the different function's signature
test_context_t* context;
/* ... */
std::thread t = std::thread(my_thread_func , context);
Upvotes: 5