Thread: multithread: how call a function name from a class, inside of the class?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    multithread: how call a function name from a class, inside of the class?

    i did a class for multithread:
    Code:
    class Multithread
    {
    private:
        pthread_t threads;
        std::function<void(void)> setf;
        void *multithreadproc(void*)
        {
            //call the function
            setf();
        }
    public:
    
        Multithread(std::function<void(void)> SetFunction)
        {
            setf=SetFunction;
        }
    
        void call()
        {
            pthread_create(&threads, NULL,std::bind(multithreadproc, this, std::placeholders::_1));//error
        }
    
        Multithread operator()()
        {
            pthread_create(&threads, NULL,std::bind(multithreadproc, this, std::placeholders::_1), (void*)1);
            return this;
        }
    
        ~Multithread()
        {
            pthread_exit(NULL);
            setf=nullptr;
        }
    
    };
    see(for exemple) the method call:
    Code:
    pthread_create(&threads, NULL,std::bind(multithreadproc, this, std::placeholders::_1));
    i'm trying use the function name on 3rd parameter. but the function is from these class, so i think that i must use bind(). but i get these error:
    "no matching function for call to 'bind(<unresolved overloaded function type>, Multithread* const, const std::_Placeholder<1>&)'"
    what i'm doing wrong?

  2. #2
    Guest
    Guest
    I've never used pthreads, but try if std::bind(&Multithread::multithreadproc, this, ...) works.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by Guest View Post
    I've never used pthreads, but try if std::bind(&Multithread::multithreadproc, this, ...) works.
    don't works.
    i did a static functions for recive the pointer:
    Code:
    class Multithread
    {
    private:
        pthread_t threads;
        function<void()> multithreadfunction;
    
    public:
    
        static void *multithreadproc(void *pThisArg)
        {
            Multithread *pThis = static_cast<Multithread*>(pThisArg);
            pThis->multithreadfunction();
            pthread_cancel(pThis->threads);
            pthread_exit(NULL);
            pThis=nullptr;
        }
    
        Multithread(std::function<void()> SetFunction)
        {
            multithreadfunction=SetFunction;
        }
    
        void operator ()()
        {
            pthread_create(&threads, 0, &Multithread::multithreadproc, static_cast<void*>(this));
    
        }
        void cancel()
        {
            pthread_exit(NULL);
            pthread_cancel(threads);
            multithreadfunction=nullptr;
        }
    
        ~Multithread()
        {
            pthread_cancel(threads);
            pthread_exit(NULL);
            multithreadfunction=nullptr;
        }
    
    };
    how i use it:
    Code:
    void text()
    {
        static int i=0;
        do
        {
            i=i+1;
            cout << "hello";
            Sleep(1000);
        }while(i<5);
    
    }
    //..............
    Multithread mtltest(text);
        mtltest();
    the function is called. but, when i close the application, seems that the thread continues activated
    the process isn't terminated

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    pthread_create doesn't have any way to know what to do with an std::bind object. It expects a pointer to a function, and std::bind does not return such a pointer. If you're going to use std::bind, you can use std::thread (include the <thread> header), or you can use boost::bind with boost::thread, if you have boost installed.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    finally it's fixed:
    Code:
    class Multithread
    {
    private:
        pthread_t threads;
        function<void()> multithreadfunction;
    
    public:
    
        static void *multithreadproc(void *pThisArg)
        {
            Multithread *pThis = static_cast<Multithread*>(pThisArg);
            pThis->multithreadfunction();
            return nullptr;//terminates the thread
        }
    
        Multithread(std::function<void()> SetFunction)
        {
            multithreadfunction=SetFunction;
        }
    
        void operator ()()
        {
            pthread_create(&threads, nullptr, &Multithread::multithreadproc, static_cast<void*>(this));
    
        }
    };
    heres a sample:
    Code:
    void text()
    {
        static int i=0;
        do
        {
            i=i+1;
            cout << "hello";
            Sleep(1000);
        }while(i<5);
    
    }
    
    //..........
    Multithread mtltest(text);
        mtltest();
    like you see i don't need use the pthread_exit(). i just need return the nullptr on my static function.
    thanks for all

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or, you know, you can use C++ instead of C:

    Code:
    class Multithread
    {
    private:
        function<void()> m_multithreadfunction;
     
    public:
        Multithread(std::function<void()> SetFunction)
        {
            m_multithreadfunction=SetFunction;
        }
     
        void operator ()()
        {
            std::thread([this] { m_multithreadfunction(); }).detach();
        }
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    Quote Originally Posted by Elysia View Post
    Or, you know, you can use C++ instead of C:
    and use a thread pool instead of launching threads irresponsibely.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Dave11 View Post
    and use a thread pool instead of launching threads irresponsibely.
    If there was a standard thread pool, I would agree, but since there is none, raw threads are the way to go. If the OP wants to implement a thread pool, it would certainly be a good exercise, but not necessary to complete the task immediately at hand.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    thanks to all

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Dave11 View Post
    and use a thread pool instead of launching threads irresponsibely.
    Depends on the task at hand, but regardless, thread pools aren't part of the standard, so they add more complexity instead of removing complexity as going from C to C++ does.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Dave11 View Post
    and use a thread pool instead of launching threads irresponsibely.
    I also object to your broad assertion that lauching threads is implicitly irresponsible. On what do you base that claim?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  12. #12
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    1. if we are to limit ourself to what the standard provides us,
    the most clever program we could write is a multi-threaed console
    application which reads and writes to files.

    C++ is the ritchest in terms of language features but one of the poorest
    in terms of utilities. the browser we are using at this very moment
    is written in C++ and (probably) use a thread pool.

    so I don't get this claim. C++ was never "battaries included" language
    like Java or C#. we always had to write our own extensions or integrate
    other developers libraries - otherwise the best thing we could have write
    is basic console applications and nothing more.

    "Depends on the task at hand, but regardless, thread pools aren't part of the standard, so they add more complexity instead of removing complexity as going from C to C++ does."
    "I also object to your broad assertion that lauching threads is implicitly irresponsible. On what do you base that claim"

    it's clear that the OP is just learning C++ and doesn't write production code,
    so I don't want to dig further on the subject.
    Elisya suggested (or followed the idea) that the OP should overload the ()
    operator and this operator will launch anonymus thread that will be detached
    on the spot.

    well,kill me but this seems pretty irresponsible for me.
    just like returning a raw pointer to inner a variable, just like
    using raw "new" and "delete" and many more things we *can* do, but we
    actually shouldn't in most of the cases.

    Design wise , following the design that was proposed is obsurd.
    a good code should:
    1) work
    2) be safe as possible
    3) be intuitive as possible
    4) be explenatory as possible.

    so here:

    1) we overload the () operator on an object named "Multithread".
    well, this isn't pretty obvious to me what exactly does () mean.
    I actually would suspect that the constructor itself construct a
    new thread , the () notation is pretty much ambiguous. does it launch
    a new thread? wake a sleeping one? launches a new thread and enqueue
    it somewhere ? we don't have any intuition on what () means. nor
    it's very explenatory.

    2) are you guys realize a thread is a *heavy* object with many many
    implication on the system itself? thread is not a string. it's implemented
    with kernel object and makes heavy system calls. one should not
    just create threads as he wishes!
    for example, what prevents me from doing something (stupid) like
    Code:
     
    for (int i=0;i<100;i++){
        launcher();
    }
    this is not about performance , it's about wasting 100 megabytes
    for building stacks, creating and destroying 100 kernel objects AND
    making the whole system slower by putting heavy weights on the system scheduler.
    this is not a good design and not a smart resource management.

    3) in the suggested design , we have no way of determining how many
    threads are actually running, how many did we launched and how many are finished.
    I wouldn't suggest a design like this in general, and specificly when
    dealing with threads.

    luckily, a Thread pool solves all of these issues by providing Task-based
    API with smart resource management which promotes heavy rsources re-use

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Proper design with threads is just a nightmare. I wouldn't want to explain all that to a newbie.

    A thread pool is sometimes good when you want to limit the number of threads (e.g. for performance threads). But if you're just running a background task, chances are you can just launch it and detach it right away. Depends on your task at hand. That is not at all irresponsible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    well,kill me but this seems pretty irresponsible for me.
    O_o

    Good.

    But if you're just running a background task, chances are you can just launch it and detach it right away.
    You are intentionally tossing the most obvious and succinct means of ensuring the "background task" is completed.

    That is not at all irresponsible.
    The "background task" you've started isn't necessarily going to be completed before the host terminates the process which means, among other things, destructors will not be invoked.

    You have written code which depends on undefined behavior to do something as common as invoking a destructor.

    I'd call relying on undefined behavior irresponsible.

    So have you...

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by phantomotap View Post
    I'd call relying on undefined behavior irresponsible.

    So have you...

    Soma
    So it is. But if you can guarantee the host process doesn't terminate before the thread, you're safe.
    Besides, again, I know it's not a good idea to just throw up threads and detach them, but I don't want to get into the whole good practice when programming with threads.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help deducing the derived class inside a base class
    By DrPiratePhD in forum C++ Programming
    Replies: 6
    Last Post: 02-08-2011, 07:03 PM
  2. Passing a pointer to a function inside a class
    By shiroaisu in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2010, 11:22 PM
  3. Pointer to a class inside the WindowsProcedure function
    By like_no_other in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2009, 12:52 PM
  4. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 05:23 AM
  5. Having a class function call a function outside the class
    By Aeroren in forum C++ Programming
    Replies: 2
    Last Post: 08-09-2005, 06:33 AM