LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-04-2011, 05:01 PM   #1
fsshl
Member
 
Registered: Jan 2002
Distribution: Ubuntu10.04
Posts: 49

Rep: Reputation: 1
fill gap of boost/thread/read_write_mutex.hpp at1.46.1 and/or later


Dear boost(linux/g++/c++) programers:
http://examples.oreilly.com/9780596007614/

12-3.cpp

is the program I am talking about.

although I will try to contact authors(4
D.Ryan Stephens
Christopher Diggins
Jonathan Turkanis
Jeff Cogswell
, and I can only find email of second one, and I emailed him but never get him reply)
hope boost experts/programers take care on this problem a little bit more deeper
and like to see your post again to make this problem be fixed someway.

Eric


> Date: Thu, 4 Aug 2011 12:11:02 -0700
> From: brian.budge@gmail.com
> To: boost-users@lists.boost.org
> Subject: Re: [Boost-users] is boost 1.46.1 contain thread/read_write_mutex.hpp
>
> On Thu, Aug 4, 2011 at 11:52 AM, Eric Lin <kingdavid1@w.cn> wrote:
> >
> > Dear boost experts:
> >
so I follow your suggestion, add #include <
#include <boost/thread/shared_mutex.hpp>
---
but I still get compile error about

root@eric-laptop:/home/eric/cppcookbook/ch12# g++ -lboost_thread Example12-3.cpp
Example12-3.cpp:41:4: error: ‘read_wirte_mutex’ in namespace ‘boost’ does not name a type
------------
about the program segment on
-----
private:
std::list<T> list_;
boost::read_wirte_mutex rwMutex_;
};
-----------
do you (or any boost expert) know/suggest what kind function(subroutine) I can use to substitute it in share_mutex.hpp, so it still not deviate book author's main
intent of the program?
--------
another way is to copy in needed files
read_write_mutex.hpp
lock.hpp
read_write_lock.hpp
from web search and my compile error about
insufficient include file
, but then it come with many in compatiable
typedef, (and/or repeat) errors.


need your help and thanks a lot in advance, Eric
> > ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> > ----------------------------------------
> >> Date: Thu, 4 Aug 2011 08:30:50 -0700
> >> From: brian.budge@gmail.com
> >> To: boost-users@lists.boost.org
> >> Subject: Re: [Boost-users] is boost 1.46.1 contain thread/read_write_mutex.hpp
> >>
> >> On Wed, Aug 3, 2011 at 6:15 PM, Eric Lin <kingdavid1@w.cn> wrote:
> >> >
> >> > dear boost progamers:
> >> >
> >> > when I tried to compile from my g++4.5.2 a simple file which include
> >> > #include <boost/thread/read_write_mutex.hpp>
> >> > -------
> >> > root@eric-laptop:/home/eric/cppcookbook/ch12# g++ -lboost_thread Example12-3.cpp
> >> > Example12-3.cpp:3:45: fatal error: boost/thread/read_write_mutex.hpp: No such file or directory
> >> > compilation terminated.
> >> > ---------
> >> > and I check from / of my ubuntuLinux with boost 1.46.1
> >> > I can not find any file name as read_wirte_mutex.hpp
> >> > ---------
> >> > root@eric-laptop:/# find . | grep read_write_mutex.hpp
> >> > root@eric-laptop:/#
> >>
> >> It won't compile because that file does not exist. It sounds like
> >> you should get a new cppcookbook, given the number of examples you've
> >> posted about that are broken. You should probably first try emailing
> >> the author of that book regarding these problems. I believe you're
> >> looking for a shared_mutex.
> >>
> >> Brian
>
> The code is wrong, and we cannot divine the original author's intent.
> I suggest you contact the author regarding their broken code.
>
> Brian
> _______________________________________________
> Boost-users mailing list
> Boost-users@lists.boost.org
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
 
Old 08-04-2011, 05:31 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Is read_wirte_mutex the way it's written in the example?
Kevin Barry
 
Old 08-04-2011, 09:05 PM   #3
fsshl
Member
 
Registered: Jan 2002
Distribution: Ubuntu10.04
Posts: 49

Original Poster
Rep: Reputation: 1
code of read_write_mutex

#include <iostream>

#include <boost/thread/thread.hpp>

#include <boost/thread/read_write_mutex.hpp>

#include <string>



template<typename T>

class Queue {

public:

Queue( ) : // Use a read/write mutex and give writers priority

<userinput>rwMutex_(boost::read_write_scheduling_policy::writer_priority)</userinput> {}

~Queue( ) {}



void enqueue(const T& x) {

// Use a r/w lock since enqueue updates the state

boost::read_write_mutex::scoped_write_lock writeLock(rwMutex_);

list_.push_back(x);

}



T dequeue( ) {

// Again, use a write lock

boost::read_write_mutex::scoped_write_lock writeLock(rwMutex_);



if (list_.empty( ))

throw "empty!";

T tmp = list_.front( );

list_.pop_front( );

return(tmp);

}



T getFront( ) {

// This is a read-only operation, so you only need a read lock

boost::read_write_mutex::scoped_read_lock readLock(rwMutex_);

if (list_.empty( ))

throw "empty!";

return(list_.front( ));

}



private:

std::list<T> list_;

boost::read_write_mutex rwMutex_;

};



Queue<std::string> queueOfStrings;



void sendSomething( ) {

std::string s;



for (int i = 0; i < 10; ++i) {

queueOfStrings.enqueue("Cyrus");

}

}



void checkTheFront( ) {

std::string s;



for (int i = 0; i < 10; ++i) {

try {s = queueOfStrings.getFront( );}

catch(...) {}

}

}



int main( ) {



boost::thread thr1(sendSomething);

boost::thread_group grp;



grp.create_thread(checkTheFront);

grp.create_thread(checkTheFront);

grp.create_thread(checkTheFront);

grp.create_thread(checkTheFront);



thr1.join( );

grp.join_all( );

}
---------------------------------------------------------------------
this is copy from download
it actually a little bit different from book's(page 454), and this book is published at 05, 06
I hope linux experts can modify a little bit from it(not deviate from original code/intent of book author too much) but still work(at least, compile)
on my 1.46.1 of boost
hope it is nothing to do with gcc or linux
----
the book author claim(on page 453)
--
Using a mutex will get the job done, but it leaves a little to be desired. It makes no
distinction between reading and writing, which is significant, because it is inefficient
to force threads to wait in line to use a resource when many or all of them are doing a
read-only operation, which should be require exclusive access. For this, the Boost
Threads library provides read_write_mutex. Example 12-3 shows how you might
implement Example 12-2 using a read_write_mutex with a front member function
that allows the caller to retrieve a copy of the first item on the queue without pop
ping it.


--
and after the code, at page 455
--
There are a few things I should point out here. Notice that now I am using a read_
write_mutex, like this:

boost::read_write_mutex rwMutex_;

The locks are also different when you're using read/write mutexes. In Example 12-3,
when I want to lock the Queue for writing, I create a scoped_write_lock:

boost::read_write_mutex::scoped_write_lock writeLock(rwMutex_);

And when I just need to read the Queue, I use a scoped_read_lock:

boost::read_write_mutex::scoped_read_lock readLock(rwMutex_);

Read/write locks are handy, but they don't prevent you from shooting yourself in the
foot. There is no compile-time check on the resource represented by rwMutex_ to
make sure you're not changing it when you only have a read lock. Take extra care to
ensure a thread only modifies the state of an object when it has a write lock because
the compiler won't.
 
Old 08-05-2011, 04:08 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
@ fsshl

Please post your code within [CODE] <your code here> [ /CODE] tags!! It makes reading the code much easier. Also, when possible, try to reduce the size of the code such that it encompasses on the issue that you are trying to resolve. Something like the following would have sufficed (and note the CODE tags):
Code:
#include <boost/thread/read_write_mutex.hpp>

int main()
{
   boost::read_write_mutex myMutex;
}
Now, as for the issue you have having, it is no doubt due to the deprecation of the read_write_mutex as of Boost 1.35 (read here at the very bottom of the page); you should instead rely on the shared_mutex.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Increase home to fill free space gap after resizing windows tradet Linux - General 3 08-10-2010 03:48 PM
LXer: GNU PDF to fill missing gap in functionality LXer Syndicated Linux News 0 11-30-2007 01:00 AM
boost::thread angustia Programming 0 01-28-2007 04:37 PM
Where to find C++ examples using boost:thread shreks Programming 4 09-06-2006 05:24 AM
IdGlobal.hpp (C++ Builder) counterparts lucky6969b Programming 4 12-15-2005 08:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration