i have vector of threads collect during execution of application. avoid nasty behavior, i'm trying ensure every thread completed when main thread exits. tried call std::thread::join
on each thread in vector upon termination event, seems stuck if recent thread hasn't finished work , won't stop blocking after should. it's important note thingmaker::creatething
reads frames series of video files , writes them 1 video, know thread should finish work in less time length of video clip being created.
std::vector<std::thread> threadlist; while (!done) { switch (triggerevent) { case 'h': // spawn new thread make thing "in background" { thingmaker next_thing = new thingmaker(); threadlist.push_back(std::thread(&thingmaker::creatething, next_thing)); next_thing = null; break; } case 't': // terminate application { std::vector<std::thread>::iterator threads; (threads = threadlist.begin(); threads != threadlist.end(); ++threads) threads->join(); done = true; break; } default: break; } }
if send 't' before recent thread has finished making video clip , finished altogether, threads->join()
blocks forever. however, if wait video clips created, application terminates. understanding, should wait thread finish work , let main thread carry on - misunderstanding?
there 2 possibilities:
the thread hasn't finished yet. can add logging code or use debugger see whether case.
the thread called
join
holds lock preventing other thread finishing. callingjoin
waits thread finish , cannot safely called unless you're absolutely sure thread callsjoin
holds no locks other thread might need acquire in order finish. (look closely @ call stack leading calljoin
see if posssibility.)
Comments
Post a Comment