# C++ probe block concurrency, locking necessary?

**URL:** <https://www.ruby-forum.com/t/c-probe-block-concurrency-locking-necessary/209222>\
**Category:** GNU Radio\
**Created:** [July 8, 2011, 2:47pm UTC](https://www.ruby-forum.com/t/c-probe-block-concurrency-locking-necessary/209222 "2011-07-08T14:47:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 8, 2011, 2:47pm UTC](https://www.ruby-forum.com/t/c-probe-block-concurrency-locking-necessary/209222/1 "2011-07-08T14:47:10Z")

</div>

Hey guys,  
I am implementing a probe block that stores several values to a member  
struct so they can be read from pyhton via accessor method  
(get\_probe\_values() const {return probe\_struct;}).  
Now my question is: Is it necessary to use some kind of thread locking  
mechanism to prevent gnuradio from changing one of the values until  
the accessor method can return the whole struct to python?  
I am affraid of getting probe values from different “cycles” that  
don’t belong together.  
In other words is it possible that the work() method and  
get\_probe\_values() will be called at the same time and I get some  
concurrency problem?

Johannes

---

<div class="post-metadata">

**Author:** ![jst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jst](https://www.ruby-forum.com/u/jst)\
**Post date:** [July 8, 2011, 5:53pm UTC](https://www.ruby-forum.com/t/c-probe-block-concurrency-locking-necessary/209222/2 "2011-07-08T17:53:13Z")

</div>

On Fri, Jul 8, 2011 at 05:45, Johannes S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> concurrency problem?

Yes, this is not only possible but likely to happen. You will need to  
acquire a mutex in your work function just prior to updating the probe  
data,  
and release it afterward. The accessor function also needs to do the  
same,  
copy the probe data locally, then release the mutex. The gruel library  
in  
GNU Radio is has a gruel::mutex for this purpose. It’s really a typedef  
for  
boost::mutex, so you can also use that directly.

Johnathan  
Johnathan

---

<div class="post-metadata">

**Author:** ![jst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jst](https://www.ruby-forum.com/u/jst)\
**Post date:** [July 8, 2011, 10:22pm UTC](https://www.ruby-forum.com/t/c-probe-block-concurrency-locking-necessary/209222/3 "2011-07-08T22:22:25Z")

</div>

Thanks for your answer.

> Yes, this is not only possible but likely to happen. You will need to  
> acquire a mutex in your work function just prior to updating the probe data,  
> and release it afterward. The accessor function also needs to do the same,  
> copy the probe data locally, then release the mutex. The gruel library in  
> GNU Radio is has a gruel::mutex for this purpose. It’s really a typedef for  
> boost::mutex, so you can also use that directly.

I am really kind of a beginner when it comes to this mutex stuff.  
Since we don’t have the newest gnuradio version from git running here  
it seems I need to use something called mld\_mutex instead of gruel.  
I found this via google in usrp/host/lib/fusb\_darwin but it is not  
working. I couldn’t find out which header to include.

My idea was to do something like this:  
d\_metric is a struct

d\_metric\_mutex-\>lock();  
d\_metric.x = something  
d\_metric.y = something  
d\_metric\_mutex-\>unlock();

metric get\_metric () const  
{  
d\_metric\_mutex-\>lock();  
return d\_metric;  
d\_metric\_mutex-\>unlock();  
}

So when I return a struct to python via SWIG, what will happen. Will  
there be some copy of it generated?

---

<div class="post-metadata">

**Author:** ![jst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jst](https://www.ruby-forum.com/u/jst)\
**Post date:** [July 11, 2011, 1:00pm UTC](https://www.ruby-forum.com/t/c-probe-block-concurrency-locking-necessary/209222/4 "2011-07-11T13:00:33Z")

</div>

Hi Johnathan,  
can you give me some hint how to use the mutex? Why is it not possible  
just to use lock and unlock?

Regards,  
Johannes

2011/7/8 Johannes S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![jst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jst](https://www.ruby-forum.com/u/jst)\
**Post date:** [July 8, 2011, 10:46pm UTC](https://www.ruby-forum.com/t/c-probe-block-concurrency-locking-necessary/209222/5 "2011-07-08T22:46:30Z")

</div>

I changed my code to use boost::mutex now. Therefore I included  
#include \<boost/thread.hpp\>

Then I did something like this, which made my program go into a deadlock  
☹

in header:  
boost::mutex d\_metric\_mutex;

in .cc  
d\_metric\_mutex.lock();  
d\_metric.x = 1  
d\_metric.y = 2  
d\_metric\_mutex.unlock();

and accessor:

metric get\_metric ()  
{  
d\_metric\_mutex.lock();  
return d\_metric;  
d\_metric\_mutex.unlock();  
}

2011/7/8 Johannes S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![jst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jst](https://www.ruby-forum.com/u/jst)\
**Post date:** [July 11, 2011, 1:40pm UTC](https://www.ruby-forum.com/t/c-probe-block-concurrency-locking-necessary/209222/6 "2011-07-11T13:40:42Z")

</div>

lock() and unlock() affect the running of the flow graph; you don’t want  
to interrupt the sample stream.

Find an example of a mutex:  
% grep “gruel::mutex” \* # Grep is your friend!  
gr\_histo\_sink\_f.h: gruel::mutex d\_mutex;

See that file (and the .cc) as a reference.  
Mutexing for this case is very simple.

Have fun,  
MB

On Mon, Jul 11, 2011 at 12:57:31PM +0200, Johannes S. wrote:

> > I changed my code to use boost::mutex now. Therefore I included  
> > d\_metric.y = 2
> > 
> > > I am really kind of a beginner when it comes to this mutex stuff.  
> > > d\_metric.y = something  
> > > there be some copy of it generated?
> 
> * * *
> 
> Discuss-gnuradio mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

–  
Karlsruhe Institute of Technology (KIT)  
Communications Engineering Lab (CEL)

Dipl.-Ing. Martin B.  
Research Associate

Kaiserstraße 12  
Building 05.01  
76131 Karlsruhe

Phone: +49 721 608-43790  
Fax: +49 721 608-46071  
www.cel.kit.edu

KIT – University of the State of Baden-Württemberg and  
National Laboratory of the Helmholtz Association
