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. C / C++ / MFC
  4. parellel mutithread problem

parellel mutithread problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpregexquestion
4 Posts 2 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!

    --Jack

    C 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!

      --Jack

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      Do you ever release the mutexes you lock with WaitForSingleObject? Once you are done with it you should call ReleaseMutex[^]. Anyways, the high CPU usage can mean 2 things i think: you either make too many threads, or one or more of your threads are stuck in an infinite loop. Once your program is eating all the CPU power use your debugger to break execution and check what your threads are doing. If there are a lot of items you need to process you are probably better off using a few threads and queuing the items to them rather then creating a hundred-and-twenty-five threads for each and every item you want to process.

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

      J 1 Reply Last reply
      0
      • C Code o mat

        Do you ever release the mutexes you lock with WaitForSingleObject? Once you are done with it you should call ReleaseMutex[^]. Anyways, the high CPU usage can mean 2 things i think: you either make too many threads, or one or more of your threads are stuck in an infinite loop. Once your program is eating all the CPU power use your debugger to break execution and check what your threads are doing. If there are a lot of items you need to process you are probably better off using a few threads and queuing the items to them rather then creating a hundred-and-twenty-five threads for each and every item you want to process.

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

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

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

        C 1 Reply Last reply
        0
        • J JackPuppy

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

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          Please put your code posts between <pre> and </pre> tags for better readability next time. Did you try breaking the execution to see what your threads are doing when they gabble up the CPU?

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

          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