Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. a parellel program problem

a parellel program problem

Scheduled Pinned Locked Moved Algorithms
helpregexquestion
3 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JackPuppy
    wrote on last edited by
    #1

    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!

    J 2 Replies Last reply
    0
    • J JackPuppy

      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!

      J Offline
      J Offline
      JackPuppy
      wrote on last edited by
      #2

      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" <

      1 Reply Last reply
      0
      • J JackPuppy

        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!

        J Offline
        J Offline
        JackPuppy
        wrote on last edited by
        #3

        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" <

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups