ofThread in vector

#1

Hi,

Is there an objection to have a vector with objects extended from ofThread ?
or more accurately, a vector with objects from a class containing an object extended from ofThread ?

If there’s not, why do I have those errors…

  
'Poco::FastMutex::FastMutex(const Poco::FastMutex&)' is private|  

This happens from that line

  
m_rays.push_back(m_rayToAdd);  

m_rayToAdd is an instance from ofdRay (class containing an object threaded)

  
  
vector<ofdRay> m_rays;  
ofdRay m_rayToAdd;  
  

I hope I’m understandable, and somebody can help.
details of all errors below
Cheers

Sébastien

  
  
..\..\..\libs\openFrameworks\utils\ofThread.h|12|instantiated from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = ofdRay]'|  
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_vector.h|737|instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = ofdRay, _Alloc = std::allocator<ofdRay>]'|  
D:\PROJETS\_OF\007\addons\_myAddons\ofdRays\ofdRays.cpp|79|instantiated from here|  
..\..\..\libs\poco\include\Poco\Mutex.h|168|error: 'Poco::FastMutex::FastMutex(const Poco::FastMutex&)' is private|  
..\..\..\libs\openFrameworks\utils\ofThread.h|12|error: within this context|  
..\..\..\addons\_myAddons\ofdAnimation\ofdAnimation.h|22|note: synthesized method 'ofThread::ofThread(const ofThread&)' first required here |  
D:\PROJETS\_OF\007\addons\_myAddons\ofdRays\ofdRay.h|7|note: synthesized method 'ofdAnimation::ofdAnimation(const ofdAnimation&)' first required here |  
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ext\new_allocator.h||In member function 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = ofdRay]':|  
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_vector.h|737|instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = ofdRay, _Alloc = std::allocator<ofdRay>]'|  
D:\PROJETS\_OF\007\addons\_myAddons\ofdRays\ofdRays.cpp|79|instantiated from here|  
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ext\new_allocator.h|105|note: synthesized method 'ofdRay::ofdRay(const ofdRay&)' first required here |  
..\..\..\libs\poco\include\Poco\Mutex.h||In member function 'ofThread& ofThread::operator=(const ofThread&)':|  
..\..\..\libs\openFrameworks\utils\ofThread.h|12|instantiated from 'void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = ofdRay, _Alloc = std::allocator<ofdRay>]'|  
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_vector.h|741|instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = ofdRay, _Alloc = std::allocator<ofdRay>]'|  
D:\PROJETS\_OF\007\addons\_myAddons\ofdRays\ofdRays.cpp|79|instantiated from here|  
..\..\..\libs\poco\include\Poco\Mutex.h|169|error: 'Poco::FastMutex& Poco::FastMutex::operator=(const Poco::FastMutex&)' is private|  
..\..\..\libs\openFrameworks\utils\ofThread.h|12|error: within this context|  
..\..\..\addons\_myAddons\ofdAnimation\ofdAnimation.h||In member function 'ofdAnimation& ofdAnimation::operator=(const ofdAnimation&)':|  
..\..\..\addons\_myAddons\ofdAnimation\ofdAnimation.h|22|note: synthesized method 'ofThread& ofThread::operator=(const ofThread&)' first required here |  
D:\PROJETS\_OF\007\addons\_myAddons\ofdRays\ofdRay.h||In member function 'ofdRay& ofdRay::operator=(const ofdRay&)':|  
D:\PROJETS\_OF\007\addons\_myAddons\ofdRays\ofdRay.h|7|note: synthesized method 'ofdAnimation& ofdAnimation::operator=(const ofdAnimation&)' first required here |  
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\vector.tcc||In member function 'void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = ofdRay, _Alloc = std::allocator<ofdRay>]':|  
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_vector.h|741|instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = ofdRay, _Alloc = std::allocator<ofdRay>]'|  
D:\PROJETS\_OF\007\addons\_myAddons\ofdRays\ofdRays.cpp|79|instantiated from here|  
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\vector.tcc|312|note: synthesized method 'ofdRay& ofdRay::operator=(const ofdRay&)' first required here |  
||=== Build finished: 4 errors, 1 warnings ===|  
  

ofThread waiting
#2

When you put an object in a vector, it copies it, which isn’t going to play nicely with the thread. I’m not super savvy with how the Poco threads are initialized but it might work if you have a vector of pointers to those objects, but you probably shouldn’t have a threaded function in an object if you’re making a lot of them. Actually, you probably shouldn’t have a threaded function in an object unless it’s something that’s going to be created very infrequently and be around for a substantial amount of time.

#3

everything josh said + the error you are getting is because the Poco::Mutex inside ofThread is not copiable: it’s copy constructor it’s private. This is done like this because you can have a mutex that it’s locked so when you copy it the copy should be also locked? unlocked?

If you want to put them in a vector use pointers, but as josh said you probably don’t want to do that, your computer has as much 5 cores? if you create more than that threads having to swap among them is going to eat more resources than actually doing that process sequentially

#4

Well,

There’s a french proverb
“when 2 topics from OF champions say don’t do that, don’t do that”

I will stop following that track :smiley:

1 Like
#5

Very old thread but deserves an update. There is a way, using a vector of unique pointers to the ofThread extended object. It’s documented in the ofBook threads chapter but there is a little mistake there: instead of using the dot operator in:

imgLoaders.push_back(move(unique_ptr(new ImageLoader)));
imgLoaders.back().load(“someimage.png”);

the arrow operator should appear instead:

imgLoaders.back()->load(“someimage.png”);