Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
2401 Discussions

spin_rw_mutex upgrade/downgrade not compiling

Nav
New Contributor I
122 Views
I took an example from the TBB tutorial pdf.
[bash]std::vector MyVector;
typedef spin_rw_mutex MyVectorMutexType;
MyVectorMutexType MyVectorMutex;
void AddKeyIfMissing( const string& key ) {
// Obtain a reader lock on MyVectorMutex
MyVectorMutexType::scoped_lock
lock(MyVectorMutex,/*is_writer=*/false);
size_t n = MyVector.size();
for( size_t i=0; i if( MyVector==key ) return;
if( !MyVectorMutex.upgrade_to_writer() )
// Check if key was added while lock was temporarily released
for( int i=n; i if(MyVector==key ) return;
vector.push_back(key);
}
[/bash]


And created my own program.
[bash]#include
#include

#include

using namespace std;
using namespace tbb;

typedef spin_rw_mutex myMutex;
myMutex sm;
int i=0;

void writerFunction()
{
{
myMutex::scoped_lock lock(sm,/*is writer=*/true);
++i;
sm.downgrade_to_reader();
//sm.upgrade_to_writer();
cout<<"wrote value "<< }
}


int main()
{
tbb_thread my_thread1(writerFunction);
tbb_thread my_thread2(writerFunction);

my_thread1.join();
my_thread2.join();

cout<<"Value of i = " << i <}[/bash]


It shows the following compilation error:
[bash]spin_rw_mutex.cpp: In function void writerFunction():
spin_rw_mutex.cpp:25: error: class myMutex has no member named downgrade_to_reader
bash: ./spin_rw_mutex: No such file or directory
[/bash]
I had a look at the spin_rw_mutex_v3.hpp file. It shows that downgrade_to_reader is within scoped_lock. If that's so, I'm confused as to how the function is accessed as a mutex instance member in the tutorial example. What am I doing/understanding wrong?
0 Kudos
1 Solution
Alexey_K_Intel3
Employee
122 Views
It's an error in the Tutorial; thank you for pointing it out!
Upgrading/downgrading should be done through the scoped_lock object, not through the mutex object. We will fix the Tutorial.

View solution in original post

2 Replies
Alexey_K_Intel3
Employee
123 Views
It's an error in the Tutorial; thank you for pointing it out!
Upgrading/downgrading should be done through the scoped_lock object, not through the mutex object. We will fix the Tutorial.

View solution in original post

Nav
New Contributor I
122 Views
Ah! So I just had to use
lock.downgrade_to_reader();
T'was as simple as that! Thanks! :)
Reply