Boost logo

Boost Users :

Subject: Re: [Boost-users] passing variables into thread
From: Alexander Carôt (alexander_carot_at_[hidden])
Date: 2016-09-03 06:07:57


Hello Juan,
 
thanks a lot - this looks very promising ! I will figure it out as soon as possible.
 
Best
 
Alex
 
--
http://www.carot.de
Email : Alexander@Carot.de
Tel.: +49 (0)177 5719797
 
 
Gesendet: Samstag, 03. September 2016 um 07:10 Uhr
Von: "Juan Ramírez" <jramirez.uy@gmail.com>
An: boost-users@lists.boost.org
Betreff: Re: [Boost-users] passing variables into thread
I think what you want may be called an 'Event us'.
 
You have a queue (Bus) of elements of type Event (Event is the type you define to contain the data).
The thread is locked in the call to 'pop()'
Anytime you want to send more data you just push a new element into the queue
The thread gets unlocked and gets the new data
 
Of course you may want to try another if more than one thread are allowed to handle the same event, but that won't be difficult if the first steps are understood.
 
I invite you to take a look to this for an implementation of a thread-safe queue
 
Pseudo-code:
 
 
void thread_handler() {
    while (true) {
        Event e = queue_.pop();
        // do somehting with e
    }
}
 
void trigger() {
    Event e (data1, data2, data3)
   queue_.push(e);
}
 
 
On Fri, Sep 2, 2016 at 5:30 PM, Alexander Carôt <Alexander_Carot@gmx.net> wrote:
Hello all,

I have a thrad function like:

void sj::eventTrigger(){
    boost::mutex::scoped_lock lock(eventTriggerMutex);

    while (eventTriggerActive){
        dFC->eventTriggerLock.wait(lock);

        /// do something with pointer-variable

    }
}

This function is executed by a eventTriggerLock.notify_all() – call from other objects and when I need to pass certain variables into the thread I have been unsing a global pointer variable.

Now my question:

I wonder if there is a way to get rid of the pointer variable and directly pass variables into the thread such as:

eventTriggerLock.notify_all(data1)

void sj::eventTrigger(int data1){
    boost::mutex::scoped_lock lock(eventTriggerMutex);

    while (eventTriggerActive){
        dFC->eventTriggerLock.wait(lock);

        /// do something with data1

    }
}

Any hint welcome – thanks in advance

Alex



_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
 
 
--
Juan
:wq
 
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net