a parellel program problem
-
hi, there It's my first time to post a question here to ask for help because the problem is drving me crazy. Everyone who give a direct answer or redirect-url resources is appreciable. here is the problem: I decide to grab web-pages from a website, what my policy is that I use a main thread to find many items in a page and i send each item to a thread to deal with. So this is a parallel muti-thread problem. and I find out that the whole process stuck somewhere and the usage of the CPU climed up to 100%. here is the main code that i think is asscociated:
HANDLE hSemaThr,hSema,hContent;
main(){
....Handle hCont=CreateFile(....);
hSema=CREATESemaphore(0,1,1,0);
hSemaThr=CREATEMUTEX(0,10,10,0);//this Semaphore is to control thread-buildings so that there are 10 threads in the process at most
WHILE(regex_search(begin,end,stringmatch,r)){
string s=stringmatch[0];
begin=stringmatch[0].second;WaitForSingleObject(hSemaThr,INFINITE);
CreateThread(0,0,Grab,(void*)dc,0,0);}
CloseHandle(hCont);
}void Grab(void* doc){
......
......WaitForSingleObject(hSema,INFINITE);
if(!WriteFile(::hCont,lpBuf,(int)strlen(lpBuf),&lpN,0)){
cout < <"can't write file in thread" < Can somebody help me!
Thanks very much! -
hi, there It's my first time to post a question here to ask for help because the problem is drving me crazy. Everyone who give a direct answer or redirect-url resources is appreciable. here is the problem: I decide to grab web-pages from a website, what my policy is that I use a main thread to find many items in a page and i send each item to a thread to deal with. So this is a parallel muti-thread problem. and I find out that the whole process stuck somewhere and the usage of the CPU climed up to 100%. here is the main code that i think is asscociated:
HANDLE hSemaThr,hSema,hContent;
main(){
....Handle hCont=CreateFile(....);
hSema=CREATESemaphore(0,1,1,0);
hSemaThr=CREATEMUTEX(0,10,10,0);//this Semaphore is to control thread-buildings so that there are 10 threads in the process at most
WHILE(regex_search(begin,end,stringmatch,r)){
string s=stringmatch[0];
begin=stringmatch[0].second;WaitForSingleObject(hSemaThr,INFINITE);
CreateThread(0,0,Grab,(void*)dc,0,0);}
CloseHandle(hCont);
}void Grab(void* doc){
......
......WaitForSingleObject(hSema,INFINITE);
if(!WriteFile(::hCont,lpBuf,(int)strlen(lpBuf),&lpN,0)){
cout < <"can't write file in thread" < Can somebody help me!
Thanks very much!sorry, when i extrat my code i forget to change some words: here is the right one and I have release the semaphore in each thread, right in front of "return", and I know the queuing policy is better but I just want to find out what's wrong with my program,by the way at the same time there can only be 10 threads because of my semaphore controling policy.see"//"
main(){
....Handle hCont=CreateFile(....);
hSema=CREATESemaphore(0,1,1,0);
hSemaThr=CREATEMUTEX(0,10,10,0);//so there are only 10 threads at most in the same time
WHILE(regex_search(begin,end,stringmatch,r)){
string s=stringmatch[0];
begin=stringmatch[0].second;WaitForSingleObject(hSemaThr,INFINITE);
CreateThread(0,0,Grab,(void*)dc,0,0);}
CloseHandle(hCont);
}void Grab(void* doc){
......
......WaitForSingleObject(hSema,INFINITE);
if(!WriteFile(::hCont,lpBuf,(int)strlen(lpBuf),&lpN,0)){
cout < <"can't write file in thread" < -
hi, there It's my first time to post a question here to ask for help because the problem is drving me crazy. Everyone who give a direct answer or redirect-url resources is appreciable. here is the problem: I decide to grab web-pages from a website, what my policy is that I use a main thread to find many items in a page and i send each item to a thread to deal with. So this is a parallel muti-thread problem. and I find out that the whole process stuck somewhere and the usage of the CPU climed up to 100%. here is the main code that i think is asscociated:
HANDLE hSemaThr,hSema,hContent;
main(){
....Handle hCont=CreateFile(....);
hSema=CREATESemaphore(0,1,1,0);
hSemaThr=CREATEMUTEX(0,10,10,0);//this Semaphore is to control thread-buildings so that there are 10 threads in the process at most
WHILE(regex_search(begin,end,stringmatch,r)){
string s=stringmatch[0];
begin=stringmatch[0].second;WaitForSingleObject(hSemaThr,INFINITE);
CreateThread(0,0,Grab,(void*)dc,0,0);}
CloseHandle(hCont);
}void Grab(void* doc){
......
......WaitForSingleObject(hSema,INFINITE);
if(!WriteFile(::hCont,lpBuf,(int)strlen(lpBuf),&lpN,0)){
cout < <"can't write file in thread" < Can somebody help me!
Thanks very much!sorry, when i extrat my code i forget to change some words:
main(){
....Handle hCont=CreateFile(....);
hSema=CREATESemaphore(0,1,1,0);
hSemaThr=CREATEMUTEX(0,10,10,0);//so there are only 10 threads at most in the same time
WHILE(regex_search(begin,end,stringmatch,r)){
string s=stringmatch[0];
begin=stringmatch[0].second;WaitForSingleObject(hSemaThr,INFINITE);
CreateThread(0,0,Grab,(void*)dc,0,0);}
CloseHandle(hCont);
}void Grab(void* doc){
......
......WaitForSingleObject(hSema,INFINITE);
if(!WriteFile(::hCont,lpBuf,(int)strlen(lpBuf),&lpN,0)){
cout < <"can't write file in thread" <