Boost logo

Boost Users :

Subject: Re: [Boost-users] passing variables into thread
From: Juan Ramírez (jramirez.uy_at_[hidden])
Date: 2016-09-03 01:10:42


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
<https://github.com/ichramm/cppmisc/blob/master/concurrent_queue.hpp> 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_at_[hidden]>
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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users

-- 
Juan
:wq


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