简体   繁体   English

用函数构造一个std :: thread

[英]Constructing a std::thread with a function

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 : my_thread_func定义:

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. 很抱歉混淆......实际上我是在公共MainPage中定义my_thread_func,所以我不能使用原生类型因此我认为值得长期尝试并给它地址。

/* 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! <<<< 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);

Trying from here: http://en.cppreference.com/w/cpp/thread/thread/thread 试试这里: http//en.cppreference.com/w/cpp/thread/thread/thread

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 . 然后,您就可以将引用传递给context If the function is not supposed to modify the context then it is better to use: 如果该函数不应该修改context那么最好使用:

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 . 从你的代码看,你似乎有一个指向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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM