Is it good to return an iterator in a list in an object that is used and used in a multi-threaded environment?
class RequestList { public: RequestList::RequestList(); RequestList::~RequestList(); std::list<boost::shared_ptr<Request> >::iterator GetIterator(); int ListSize(); void AddItem(boost::shared_ptr<Request> request); void RemoveItem(boost::shared_ptr<Request> request); std::list<boost::shared_ptr<Request> > GetRequestsList(); boost::shared_ptr<Request> GetRequest(); private: std::list<boost::shared_ptr<Request> > requests; std::list<boost::shared_ptr<Request> >::iterator iter; //Iterator boost::mutex listmtx; }; std::list<boost::shared_ptr<Request> >::iterator RequestList::GetIterator() { return this->iter; }
Using:
RequestList* requests;
In some thread (can be used again in other threads)
std::list<boost::shared_ptr<Request> >::iterator iter = requests->GetIterator();
Or would it be wiser to just create an iterator for this list every time and use it locally inside each thread?
No, it is usually not recommended to share an iterator over threads. There are several ways to make sure you have no problem.
-, - , . - . , .
, , . , boost::mutex. , , .
boost::mutex
- . , , , , . , , , - .
, , , , . , , , : shared_ptr .
shared_ptr
, , - . , , , "" .
, "" , , API , . , . , , :
, , , , .
GetRequestsList . , , .
GetRequestsList
, , .
, , (, , , , .?)
, , , . , ( ). , (, ?)
, GetIterator, . std::list<>::iterator - , . , .
GetIterator
std::list<>::iterator
, . , , , . / .
, .
, , , ro- rw- . , , ( ) ? .
, , , , .. , "", list, , , , vector: -)
list
vector
, . , - . , Intel TBB, . , / . ( std::vector ), parallel_for() , () !. !, .
Source: https://habr.com/ru/post/1768971/More articles:Setting the timer interval to infinite - .netiPhone, an object on a picket sheet, how can I determine when an action sheet is closed / fired? - iphoneMigrating from ASP.NET MVC to Java - javaiPhone - How to recognize iTunes user of my application - iphoneDjango South migration error "contains null values" with field null = True - djangoHow to create a random value between two numbers - javahql join - the path expected to connect - nhibernateразделите строку, если она не заключена между другими строками - sqlGet a local copy of all changes to the SVN project - svnПолучение уведомлений MediaWiki для всех изменений - mediawikiAll Articles