c++ - Error : An exception (first chance) at 0x76f6f9d2 in Boost_Mutex.exe: 0xC0000008: An invalid handle was specified -
i write program test multithreading. in main
function thread t
created. in function d
, in thread t
, 2 threads tt
, ttt
created. function process
runs in thread ttt
. in process
member function doanotherthing
of class dat
called. in thread tt
member function doonething
called.
when debug program, error occured: an exception (first chance) @ 0x76f6f9d2 in boost_mutex.exe: 0xc0000008: invalid handle specified.
sometimes this error occured instead of error above :
run-time check failure #2 - stack around variable 'odat' corrupted.
can me solve problem , modify codes?
these codes:
"dat.h"
#pragma once #ifndef dat_h #define dat_h #include <boost\thread\thread.hpp> using namespace std; class dat { public: dat(); ~dat(); void doonething(); void doanotherthing (); private: boost::mutex omutex; int x; }; #endif // dat_h
"dat.cpp"
#include "dat.h" dat::dat() { } dat::~dat() { } void dat::doonething() { x = 1; } void dat::doanotherthing() { omutex.lock(); x = 2; omutex.unlock(); }
"main.cpp"
#include "dat.h" #include <boost\function.hpp> struct parameter // parameters of function process , d { dat* pdat; }; void process(void*pparam) { // init parameter parameter* puserparams = (parameter*)pparam; puserparams->pdat->doanotherthing(); } void d(void* pparam) { // init parameter parameter* puserparams = (parameter*)pparam; boost::function<void()> f; boost::thread ttt(process, (void*)&pparam); f = boost::bind(&dat::doonething, puserparams->pdat); // capture thread started boost::thread tt(f); ttt.join(); tt.join(); } void main() { dat odat; parameter ppara ; ppara.pdat = &odat; boost::thread t(d,(void*)&ppara); t.join(); }
if have suggestions statement of quesion, pleas tell me , modify it. thank you
the main problem here line:
boost::thread ttt(process, (void*)&pparam);
you take address of pparam
(which pointer), yielding void**
, , cast void*
. c++ threading interface type-safe. fact have cast strong indicator you're doing wrong. (also, usage of void*
on place. needed c threading interfaces, not c++.)
anyway, in process
take parameter, points pparam
, , pretend points ppara
object. (why prefixed p
anyway? it's not pointer!) reach pdat
pointer, of course nonsense, because there's no parameter
struct there in first place. pointer isn't valid, , doesn't point valid dat
struct, means mutex
in there isn't valid either, means internal thread handle isn't valid, crash need when try lock mutex.
here's how fix code: rid of void pointer , rid of casts. also, not strictly necessary, should rid of variable prefixes. redundant naming convention bad enough, incorrectly applied redundant naming convention disaster.
you have 1 more error: don't protect all accesses x
mutex, one. useless. you've still got race condition. have protect accesses shared variable. also, should use lock_guard
raii object instead of manually calling lock
, unlock
, can never forget unlock.
Comments
Post a Comment