Lambda and thread

I recently started using lambdas very much in streams, and want to make sure that I don't tune into security / crash issues later. My usual way to use them:

class SomeClass {  
    int someid;  
    void NextCommand();  
    std::function<void(int, int)> StoreNumbers;  
    SomeClass(id, fn); // constructor sets id and storenumbers fn  
}

// Called from multiple threads  
static void read_callback(int fd, void* ptr)  
{  
    SomeClass* sc = static_cast<SomeClass*>ptr;  
    ..  
    sc->StoreNumbers(someint,someotherint); // voila, thread specific storage.  
}  

static DWORD WINAPI ThreadFn(LPVOID param)  
{  
    std::list<int> ints1;  
    std::list<int> ints2;  
    auto storenumbers = [&] (int i, int i2) {  
        // thread specific lambda.  
        ints1.push_back(i);  
        ints2.push_back(i2);  
        };  
    SomeClass s(id, storenumbers);  
    ...  
    // set up something that eventually calls read_callback with s set as the ptr. 
}  

ThreadFn is used as a thread function for 30-40 threads.

It is acceptable? Usually I have a few of these stream specific lambdas that work with a bunch of specific stream data.

Thank!

+3
source share
1 answer

. , , , bind . , .

+2

All Articles