Hello
I need some help using CSemaphore class in MFC C++ application. I have an edit1 box with multil ine and each line has a string. I'm trying to loop through edit1 box and for each string to start a thread that is using the string for specific function. I'm trying to limit the run of only one thread at the same time with semaphores but all treads start at the same time.
So when i click button1 i loop through edit1 box and start threads:
While looping through edit1 box multi line and starting the threads:
Instead of running only one thread at a time all threads start. What am i doing wrong ?
Thanks for your help!!!
I need some help using CSemaphore class in MFC C++ application. I have an edit1 box with multil ine and each line has a string. I'm trying to loop through edit1 box and for each string to start a thread that is using the string for specific function. I'm trying to limit the run of only one thread at the same time with semaphores but all treads start at the same time.
So when i click button1 i loop through edit1 box and start threads:
Code:
void CMFCApplication1Dlg::onButton1Click()
{
int i, nLineCount = edit1.GetLineCount();
CString strText, strLine, mesaj;
for (i = 0; i < nLineCount; i++)
{
int len = edit1.LineLength(edit1.LineIndex(i));
edit1.GetLine(i, strText.GetBuffer(len), len);
strText.ReleaseBuffer(len);
strLine.Format(_T("%s"), strText);
THREADSTRUCT *_param = new THREADSTRUCT;
_param->dlsg = this;
_param->link = strLine;
CSemaphore semafor(1, 1); // here i limit only one instance of thread at a time ???
CWinThread* tread[5];
tread[i] = AfxBeginThread(StartThread, _param);
}
}
Code:
UINT CMFCApplication1Dlg::StartThread(LPVOID param)
{
WaitForSingleObject(semafor, INFINITE); // wait for semafor to signal
THREADSTRUCT* ts = (THREADSTRUCT*)param;
// here i'm doing some operations with the string from edit1 box
ReleaseSemaphore(semafor, 1, NULL); //release the semaphore for next thread to begin
}
Thanks for your help!!!