Click to See Complete Forum and Search --> : Mutex Locking only after the 3erd entrance?


Alphadan
March 19th, 2011, 04:24 PM
Hi

i have a mutex declared and initialized on a CPP

HANDLE RqMutex;

int main(){
RqMutex=CreateMutex(NULL,FALSE,"LegRefMTI");
}


Then from another cpp on another thread

I refer to my mutex as:

extern HANDLE RqMutex;

then on the procedure i want to lock

I call:

for (RqIt = RqLst.begin(); RqIt != RqLst.end(); ++RqIt){ //Loop iterating the args to be passed
HANDLE hThread;
hThread=(HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))StartRequest,(void*)RqIt->c_str(),0,NULL); //Starting thread
if (WaitForSingleObject(RqMutex, 60000) == WAIT_TIMEOUT){
//Times out after 1 minute of waiting
}
//here should only enter once (if i dont release the mutex)
}


inside of the thread started after i make the copy of my args passed to the thread by reference...

only after i have the copy of the args( (void*)RqIt->c_str() ) on the other thread (on stack) then I release the mutex.

but there is a problem i commented the relese mutex and the beginthread starts 3 threads after locking.

i should only start one since i never released the mutex since i commented the release mutex the mutex isnt being released on another thread or cpp either.

does anyone knows whats wrong??

Thx in advance.

Arjay
March 19th, 2011, 04:32 PM
After wfso times out, it creates another thread before checking the mutex.

The whole design of your code looks a little in question (for one thing global variables are problemmatic in multithread programming).

Can you explain what you are trying to do? We might be able to help you restructure your code to get you what you want.

Alphadan
March 19th, 2011, 05:34 PM
hello arjay 1st of all thx for ur time.

yes i knew global variables are a bad practice in terms of multi threading but i just have this bug after arround one week this is from a filetransfer, the list conains the paths of the files to be transfered the goal is to alow multi transfer simultaneously, it also supports resume.

the list contains the files to be transfered with its path + staring point for the resume i plreviously pased the mutex using an accesor methood then seting it to a private declared HANDLE on that class but it was having exactly the same issue so i tested declaring it globally. but nothing changed.

the main goal of this personal project is to explore multithreading and sincronization.

UINT WINAPI StartRequest(void *arg){ is the function that the thread is strating it is declared also globally this may be a issue also but really didnt know how to do it properly.

but i dont really understand why does the mutex behave like this it enters 3 times fast simultaneously or almost then the mutex locks.

if set a Sleep(60000); just where the Lock is and it only lets one thread start, i could just add 100 ms sleep there and it will work just fine but my main goal is to learn how to properly sincronize the threads.

thx in advance!!.

but seens like im failing to understand it atm =/.

Alphadan
March 19th, 2011, 08:56 PM
mmmm i think i understood whats wront is a mis understanding by me, the loop is a sigle threa

i think i should add the lock inside the thread started instead of puting the lock on the loop.

Edit:
problem solved, it was all that i tought the mutex would lock the samethread untill i release it but i was wrong what i did was to obtain and lock the mutex on the thread created instead and also addded a lock b4 making the thread and is working 100% now, i also removed the global declaration and used the accesor methood back to pass the mutex.