libtbb segfault on second attempt in boost::thread on ARM/aarch64
I am having problems with OpenCV 3.3.1 on ARM. I have arrived at the following minimal example:
(snip includes)
void cvtTest() {
cv::Mat imgTmp = cv::Mat(1200,1920, CV_16UC1, cv::Scalar(1));
cv::Mat imgTmp2 = cv::Mat(1200,1920, CV_8UC3, cv::Scalar(0));
cvtColor(imgTmp, imgTmp2, cv::COLOR_BayerBG2BGR);
}
int main(int argc, char* argv[])
{
cout << "Start function call test" << endl;
cvtTest();
cout << "First done" << endl;
cvtTest();
cout << "Second done" << endl;
cout << "Start boost::thread test" << endl;
boost::thread* test1 = new boost::thread(cvtTest);
test1->join();
cout << "First done" << endl;
boost::thread* test2 = new boost::thread(cvtTest);
test2->join();
cout << "Second done" << endl;
[...]
this prints:
Start function call test
First done
Second done
Start boost::thread test
First done
[27069.712978] grabber[3369]: unhandled level 0 translation fault (11) at 0xfffffffffffffff7, esr 0x92000004
[27069.722563] pgd = ffff80096cb0c000
[27069.725958] [fffffffffffffff7] *pgd=00000009ed434003[27069.730751] , *pud=00000009ec966003
, *pmd=0000000000000000[27069.736243]
[27069.737726]
[27069.739219] CPU: 1 PID: 3369 Comm: grabber Not tainted 4.9.35+gb226445 #1
[27069.746009] Hardware name: LS1046A RDB Board (DT)
[27069.750714] task: ffff80096dd14f40 task.stack: ffff80096cad4000
[27069.756638] PC is at 0xffffa7454dd4
[27069.760127] LR is at 0xffffa7454ef0
[27069.763613] pc : [<0000ffffa7454dd4>] lr : [<0000ffffa7454ef0>] pstate: 80000000
[27069.771011] sp : 0000ffffa707f840
[27069.774321] x29: 0000ffffa707f840 x28: 0000ffffa707fde8
[27069.779639] x27: 000000000000077e x26: 0000ffffa707fd28
[27069.784956] x25: 0000ffffa707f980 x24: 0000000000000023
[27069.790275] x23: 0000000000000000 x22: 0000ffffa707f988
[27069.795592] x21: 0000ffffa7477568 x20: 0000000000000000
[27069.800909] x19: 0000ffffa5e0be00 x18: 0000000000000030
[27069.806229] x17: 0000ffffa7f5d380 x16: 0000ffffa5eca0d8
[27069.811548] x15: 0000000000000000 x14: 0000000000000027
[27069.816897] x13: 006b636174735f68 x12: 637461775f737472
[27069.822216] x11: ffffffffffff7fff x10: 0000000000003fff
[27069.827536] x9 : 0000000000000001 x8 : 0000ffffa5e08000
[27069.832852] x7 : 0000000000000000 x6 : 0000000000000000
[27069.838169] x5 : 00000000ba5703f5 x4 : 0000000000000001
[27069.843489] x3 : 0000000000000000 x2 : ffffffffffffffff
[27069.848804] x1 : 0000ffffa5e0beb0 x0 : ffffffffffffffff
[27069.854120]
Segmentation fault
or with valgrind:
(snip lots of Conditional jump or move depends on uninitialised value(s) )
First done
Second done
Start boost::thread test
First done
==3348== Thread 5:
==3348== Invalid read of size 8
==3348== at 0x5BECDD4: ??? (in /usr/lib/libtbb.so.2)
==3348== by 0x5BECEEF: ??? (in /usr/lib/libtbb.so.2)
==3348== by 0x5BEE953: ??? (in /usr/lib/libtbb.so.2)
==3348== by 0x5BED067: ??? (in /usr/lib/libtbb.so.2)
==3348== by 0x5BE7FCB: ??? (in /usr/lib/libtbb.so.2)
==3348== by 0x5BE610B: tbb::internal::allocate_root_with_context_proxy::allocate(unsigned long) const (in /usr/lib/libtbb.so.2)
==3348== by 0x4994FEF: cv::parallel_for_(cv::Range const&, cv::ParallelLoopBody const&, double) (in /usr/lib/libopencv_core.so.3.3.0)
==3348== by 0x4B69B07: cv::demosaicing(cv::_InputArray const&, cv::_OutputArray const&, int, int) (in /usr/lib/libopencv_imgproc.so.3.3.0)
==3348== by 0x4B3BBFB: cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int) (in /usr/lib/libopencv_imgproc.so.3.3.0)
==3348== by 0x5E0A13: cvtTest() (in /usr/bin/grabber)
==3348== by 0x508E15F: ??? (in /usr/lib/libboost_thread.so.1.64.0)
==3348== by 0x50CEF47: ??? (in /lib/libpthread-2.26.so)
==3348== Address 0xfffffffffffffff7 is not ...
did you see a note anywhere, that cvtColor would be thread-safe ?
(it is not. as long as your opencv libs were built with tbb support)
I see, I did read the function documentation but saw no mention of thread unsafety. This is "inherited" code, and works fine for several thousand invocations in the first thread. Also the threads are never concurrent nor are the matrices in question accessed from outside the thread.
So nothing in OpenCV with TBB is thread-safe unless explicitly mentioned? Where can I find what is thread-safe without TBB? I can try recompiling tomorrow when I get back to work.