Jump to content

Threads usage problem with classes


misfit382
 Share

Recommended Posts

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

image.thumb.png.965a37a6be3bf26891d113b5a02c4a5d.png

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

Link to comment
Share on other sites

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.

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

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;
}

 

Link to comment
Share on other sites

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);
  • Like 1

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

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...