misfit382 Posted July 27, 2021 Share Posted July 27, 2021 Hi, I have encountered a problem using threads with the help of classes. I have been using sample codes so far as well as browsing the forum, but unfortunately I cannot find an answer to this problem. Following the guide about threads I got something like this Unfortunately I don't understand why a function of void type not belonging to a class passes in the compilation without any problem, but in the other direction it doesn't. I apologize for bothering you with probably simple problems and for my poor English Thanks Quote Link to comment Share on other sites More sharing options...
Josh Posted July 27, 2021 Share Posted July 27, 2021 Is port_scanner::test2 a static function? You might need to add & in front of test2, i.e. &test2 or maybe &port_scanner::test2. Maybe try to create a simple example and post the code so we can test it. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
misfit382 Posted July 27, 2021 Author Share Posted July 27, 2021 11 minutes ago, Josh said: Is port_scanner::test2 a static function? Maybe try to create a simple example and post the code so we can test it. Sure, here u go with main.cpp file - should be enough to show the problem. I can't afford to use static functions in this case. #include "UltraEngine.h" using namespace UltraEngine; class testclass { private: void test2(const int start, const int size); void test(); }; void testclass::test2(const int start, const int size) { int i; for (int num = start; num <= start + size; num++) { //something here } } void testclass::test() { int thread_count = 40; int range = 65535; vector<shared_ptr<Thread> > threads(thread_count); for (int n = 0; n < thread_count; ++n) { threads[n] = CreateThread(bind((test2), range / thread_count * n, range / thread_count), true); } for (int n = 0; n < 40; ++n) { threads[n]->Wait(); } } int main(int argc, const char* argv[]) { return 0; } Quote Link to comment Share on other sites More sharing options...
Josh Posted July 27, 2021 Share Posted July 27, 2021 Just make declare the function like this in the header and it will work: static void test2(const int start, const int size); If it is a method for a specific object then you will need to use a lambda function, which would be something like this: auto o = make_shared<testclass>(); int start = range / thread_count * n; int size = range / thread_count; threads[n] = CreateThread([o, start, size]() {o->test2(start, size); }, true); 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
misfit382 Posted July 27, 2021 Author Share Posted July 27, 2021 Thanks, lambda solves everything Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.