c++ - "Project.exe has triggered a breakpoint" after implementing multithreading -
i have visual studio project worked fine until tried implement multithreading
. project acquires images gige
camera, , after acquiring 10 images, video made acquired images.
the program flow such program didn't acquire images when making video. wanted change this, created thread makes videos images. wanted program acquire images continuously, after 10 images acquired, thread runs in parallel make video. continue until stop program, 10 images acquired, video these 10 images made in parallel while next 10 images acquired , on.
i haven't created threads
before followed tutorial on this website. similar website, created thread
function
saves video. function
creates video takes 10 images vector
argument. execute join
on thread before line main
function terminates.
for clarity, here's pseudo-code i've implemented:
#include ... #include <thread> using namespace std; thread threads[1]; vector<image> images; void thread_method(vector<image> & images){ // save vector of images video // clear vector of images } int main(int argc, char* argv[]){ // stuff while(true) { (int = 0; < 10; i++) { //acquire image //put image pointer in images vector named images } threads[0] = thread(thread_method, images) } // stuff threads[0].join(); cout << endl << "done! press enter exit..." << endl; getchar(); return 0; }
when run project now, message pops saying project.exe has triggered breakpoint. project breaks in report_runtime_error.cpp
in static bool __cdecl issue_debug_notification(wchar_t const* const message) throw()
.
i'm printing cout
messages on console me understand what's going on. happens program acquires 10 images, thread
saving video starts running. there 10 images, 10 images have appended video. message says project.exe has triggered breakpoint pops after second time 10 images acquired, @ point parallel thread has appended 6 images first acquired set of images video.
the output contains multiple lines of thread xxxx has exited code 0
, after output says
debug error!
program: ...path\program.exe
abort() has been called
(press retry debug application)
program.exe has triggered breakpoint.
i can't explain in comment. i'm dropping here because looks op heading in bad directions , i'd head him off before cliff. caleth has caught big bang , provided solution avoiding it, bang symptom of op's , solution detach
questionable.
using namespace std;
why "using namespace std" considered bad practice?
thread threads[1];
an array 1 pretty pointless. if don't know how many threads there be, use vector
. plus there no reason global variable.
vector<image> images;
again, no reason global , many many reasons not be.
void thread_method(vector<image> & images){
pass reference can save copying, a) can't copy reference , threads copy parameters. ok, use pointer or std::ref
. can copy those. don't want to. problem 1: multiple threads using same data? better read or protected concurrent modification. includes thread generating vector
. 2. reference still valid?
// save vector of images video // clear vector of images } int main(int argc, char* argv[]){ // stuff while(true) { (int = 0; < 10; i++) { //acquire image //put image pointer in images vector named images } threads[0] = thread(thread_method, images)
bad reasons caleth covered. plus images
keeps growing. first thread, if copied, has ten elements. second has first ten plus ten. weird, , not op wants. references or pointers vector
fatal. vector
resized while other threads using it, invalidating old datastore , making impossible safely iterate.
} // stuff threads[0].join();
wrong reasons covered caleth
cout << endl << "done! press enter exit..." << endl; getchar(); return 0; }
the solution joining on threads same every stack overflow question doesn't resolve "use std::string
": use std::vector
.
#include <iostream> #include <vector> #include <thread> void thread_method(std::vector<int> images){ std::cout << images[0] << '\n'; // output can see work being done. // may or may not see of numbers in order depending on how // threads scheduled. } int main() // not using arguments, leave them out. { int count = 0; // have show // no need threads global. std::vector<std::thread> threads; // using vector can store multiple threads // stuff while(count < 10) // better-defined terminating condition testing purposes { // every thread gets own vector. no chance of collisions or duplicated // effort std::vector<int> images; // don't have image, stubbing out int (int = 0; < 10; i++) { images.push_back(count); } // create , store thread. threads.emplace_back(thread_method,std::move(images)); count ++; } // stuff (std::thread &temp: threads) // wait threads exit { temp.join(); } // std::endl expensive. it's linefeed , s stream flush, save // when need message out std::cout << "\ndone! press enter exit..." << std::endl; char temp; std::cin >>temp; // sticking standard librarly way through return 0; }
to better explain
threads.emplace_back(thread_method,std::move(images));
this created thread
inside threads
(emplace_back
) call thread_method
copy of images
. odds compiler have recognized end of road particular instance of images
, eliminated copying, if not, std::move
should give hint.
Comments
Post a Comment