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. Windows API
  4. Multiple Thread programming with Win API

Multiple Thread programming with Win API

Scheduled Pinned Locked Moved Windows API
helpjsonquestioncareerlearning
5 Posts 4 Posters 16 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.
  • U Offline
    U Offline
    User 10338706
    wrote on last edited by
    #1

    I am a beginner of Win API programming,and there is a strange problem that the thread1 is blocked after excuting SuspendThread() function ,but it isn't blocked every time that i run this program. anybody can help me? i'm a chinese,this is my first time here,i hope that i express my problem clearly.

    #include
    #include
    #include
    DWORD WINAPI Thread1(void *arg);
    DWORD WINAPI Thread2(void *arg);

    int main(void)
    {
    DWORD threadid1,threadid2;
    HANDLE handle1,handle2;
    handle2 = CreateThread(NULL, 0, Thread2, (void *)handle2, 0, &threadid1);
    handle1 = CreateThread(NULL, 0, Thread1, (void *)handle2, 0, &threadid2);
    HANDLE hEvent;
    hEvent = CreateEvent(NULL, FALSE, FALSE, "MainEvent");

    if(handle1 == NULL)
    {
    	int err;
    	err = GetLastError();
    	printf("CreateThread1 error %d\\n", err);
    	exit(1);
    }
    else
    {
    	printf("CreateThread1 success\\n");
    }
    if(handle2 == NULL)
    {
    	int err;
    	err = GetLastError();
    	printf("CreateThread2 error %d\\n", err);
    	exit(1);
    }
    else
    {
    	printf("CreateThread2 success\\n");
    }
    int priority1;
    int priority2;
    priority1 = GetThreadPriority(handle1);
    printf("thread1's threadid is=%d,priority is %d\\n", (int)threadid1, priority1);
    
    priority2 = GetThreadPriority(handle2);
    printf("thread2's threadid is=%d,priority is %d\\n\\n", (int)threadid2, priority2);
    
    /\*	
    	printf("suspending for 3s\\n");
    	SuspendThread(handle);
    	Sleep(3000);	
    	printf("resume\\n");
    	ResumeThread(handle);
    \*/
    	
    WaitForSingleObject(handle1, INFINITE);
    
    DWORD exitCode;
    BOOL ret;
    ret = GetExitCodeThread(handle1, &exitCode);
    if(!ret)
    {
    	printf("GetThread1 ExitCode,The last error is %d", (int)GetLastError());
    }
    else
    	printf("thread1's exit code is %d\\n", (int)exitCode);
    ret = GetExitCodeThread(handle2, &exitCode);
    if(!ret)
    {
    	printf("GetThread2 ExitCode,The last error is %d", (int)GetLastError());
    }
    else
    	printf("thread2's exit code is %d\\n", (int)exitCode);
    return 0;
    

    }
    int cnt = 0;
    DWORD WINAPI Thread1(void *arg)
    {
    HANDLE hEvent;
    hEvent = OpenEvent(EVENT_ALL_ACCESS, TRUE, "MainEvent");
    if(hEvent == NULL)
    {
    printf("in thread1, OpenEvent Error, Error No. is %d!\n", (int)GetLastError());
    return 1;
    }
    else
    {
    printf("in thread1, OpenEvent success\n");
    }
    printf("3s later, the thread2 will be suspended!\n");
    for(int i = 0; i < 3; i++)
    {
    Sleep(1000);
    SetEvent(hEvent);
    }

    printf("...suspending.....\\n");
    int ret;
    **ret = SuspendThread((HA**
    
    L A U 3 Replies Last reply
    0
    • U User 10338706

      I am a beginner of Win API programming,and there is a strange problem that the thread1 is blocked after excuting SuspendThread() function ,but it isn't blocked every time that i run this program. anybody can help me? i'm a chinese,this is my first time here,i hope that i express my problem clearly.

      #include
      #include
      #include
      DWORD WINAPI Thread1(void *arg);
      DWORD WINAPI Thread2(void *arg);

      int main(void)
      {
      DWORD threadid1,threadid2;
      HANDLE handle1,handle2;
      handle2 = CreateThread(NULL, 0, Thread2, (void *)handle2, 0, &threadid1);
      handle1 = CreateThread(NULL, 0, Thread1, (void *)handle2, 0, &threadid2);
      HANDLE hEvent;
      hEvent = CreateEvent(NULL, FALSE, FALSE, "MainEvent");

      if(handle1 == NULL)
      {
      	int err;
      	err = GetLastError();
      	printf("CreateThread1 error %d\\n", err);
      	exit(1);
      }
      else
      {
      	printf("CreateThread1 success\\n");
      }
      if(handle2 == NULL)
      {
      	int err;
      	err = GetLastError();
      	printf("CreateThread2 error %d\\n", err);
      	exit(1);
      }
      else
      {
      	printf("CreateThread2 success\\n");
      }
      int priority1;
      int priority2;
      priority1 = GetThreadPriority(handle1);
      printf("thread1's threadid is=%d,priority is %d\\n", (int)threadid1, priority1);
      
      priority2 = GetThreadPriority(handle2);
      printf("thread2's threadid is=%d,priority is %d\\n\\n", (int)threadid2, priority2);
      
      /\*	
      	printf("suspending for 3s\\n");
      	SuspendThread(handle);
      	Sleep(3000);	
      	printf("resume\\n");
      	ResumeThread(handle);
      \*/
      	
      WaitForSingleObject(handle1, INFINITE);
      
      DWORD exitCode;
      BOOL ret;
      ret = GetExitCodeThread(handle1, &exitCode);
      if(!ret)
      {
      	printf("GetThread1 ExitCode,The last error is %d", (int)GetLastError());
      }
      else
      	printf("thread1's exit code is %d\\n", (int)exitCode);
      ret = GetExitCodeThread(handle2, &exitCode);
      if(!ret)
      {
      	printf("GetThread2 ExitCode,The last error is %d", (int)GetLastError());
      }
      else
      	printf("thread2's exit code is %d\\n", (int)exitCode);
      return 0;
      

      }
      int cnt = 0;
      DWORD WINAPI Thread1(void *arg)
      {
      HANDLE hEvent;
      hEvent = OpenEvent(EVENT_ALL_ACCESS, TRUE, "MainEvent");
      if(hEvent == NULL)
      {
      printf("in thread1, OpenEvent Error, Error No. is %d!\n", (int)GetLastError());
      return 1;
      }
      else
      {
      printf("in thread1, OpenEvent success\n");
      }
      printf("3s later, the thread2 will be suspended!\n");
      for(int i = 0; i < 3; i++)
      {
      Sleep(1000);
      SetEvent(hEvent);
      }

      printf("...suspending.....\\n");
      int ret;
      **ret = SuspendThread((HA**
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See the notes on this function at http://msdn.microsoft.com/en-us/library/windows/desktop/ms686345(v=vs.85).aspx[^].

      Veni, vidi, abiit domum

      U 1 Reply Last reply
      0
      • L Lost User

        See the notes on this function at http://msdn.microsoft.com/en-us/library/windows/desktop/ms686345(v=vs.85).aspx[^].

        Veni, vidi, abiit domum

        U Offline
        U Offline
        User 10338706
        wrote on last edited by
        #3

        Thank you,although it did not solve my problem... I heared that suspendThread() may cause some problems,like dead lock, it should be avoided ...

        1 Reply Last reply
        0
        • U User 10338706

          I am a beginner of Win API programming,and there is a strange problem that the thread1 is blocked after excuting SuspendThread() function ,but it isn't blocked every time that i run this program. anybody can help me? i'm a chinese,this is my first time here,i hope that i express my problem clearly.

          #include
          #include
          #include
          DWORD WINAPI Thread1(void *arg);
          DWORD WINAPI Thread2(void *arg);

          int main(void)
          {
          DWORD threadid1,threadid2;
          HANDLE handle1,handle2;
          handle2 = CreateThread(NULL, 0, Thread2, (void *)handle2, 0, &threadid1);
          handle1 = CreateThread(NULL, 0, Thread1, (void *)handle2, 0, &threadid2);
          HANDLE hEvent;
          hEvent = CreateEvent(NULL, FALSE, FALSE, "MainEvent");

          if(handle1 == NULL)
          {
          	int err;
          	err = GetLastError();
          	printf("CreateThread1 error %d\\n", err);
          	exit(1);
          }
          else
          {
          	printf("CreateThread1 success\\n");
          }
          if(handle2 == NULL)
          {
          	int err;
          	err = GetLastError();
          	printf("CreateThread2 error %d\\n", err);
          	exit(1);
          }
          else
          {
          	printf("CreateThread2 success\\n");
          }
          int priority1;
          int priority2;
          priority1 = GetThreadPriority(handle1);
          printf("thread1's threadid is=%d,priority is %d\\n", (int)threadid1, priority1);
          
          priority2 = GetThreadPriority(handle2);
          printf("thread2's threadid is=%d,priority is %d\\n\\n", (int)threadid2, priority2);
          
          /\*	
          	printf("suspending for 3s\\n");
          	SuspendThread(handle);
          	Sleep(3000);	
          	printf("resume\\n");
          	ResumeThread(handle);
          \*/
          	
          WaitForSingleObject(handle1, INFINITE);
          
          DWORD exitCode;
          BOOL ret;
          ret = GetExitCodeThread(handle1, &exitCode);
          if(!ret)
          {
          	printf("GetThread1 ExitCode,The last error is %d", (int)GetLastError());
          }
          else
          	printf("thread1's exit code is %d\\n", (int)exitCode);
          ret = GetExitCodeThread(handle2, &exitCode);
          if(!ret)
          {
          	printf("GetThread2 ExitCode,The last error is %d", (int)GetLastError());
          }
          else
          	printf("thread2's exit code is %d\\n", (int)exitCode);
          return 0;
          

          }
          int cnt = 0;
          DWORD WINAPI Thread1(void *arg)
          {
          HANDLE hEvent;
          hEvent = OpenEvent(EVENT_ALL_ACCESS, TRUE, "MainEvent");
          if(hEvent == NULL)
          {
          printf("in thread1, OpenEvent Error, Error No. is %d!\n", (int)GetLastError());
          return 1;
          }
          else
          {
          printf("in thread1, OpenEvent success\n");
          }
          printf("3s later, the thread2 will be suspended!\n");
          for(int i = 0; i < 3; i++)
          {
          Sleep(1000);
          SetEvent(hEvent);
          }

          printf("...suspending.....\\n");
          int ret;
          **ret = SuspendThread((HA**
          
          A Offline
          A Offline
          Albert Holguin
          wrote on last edited by
          #4

          Not sure what you're trying to achieve by suspending the thread, but a better approach would be to signal to your thread2 that it should stop and have it stop itself instead of suspending it. When you call suspend, the state of thread2 is arbitrary (undefined) so it's probably not a good approach. When you say "sometimes the program will be blocked here", what do you mean? How do you know it's there? As far as I know, SuspendThread is non-blocking, so there should be no reason for that to be hung-up there (although not sure what happens if you call SuspendThread on a thread that no longer exists).

          1 Reply Last reply
          0
          • U User 10338706

            I am a beginner of Win API programming,and there is a strange problem that the thread1 is blocked after excuting SuspendThread() function ,but it isn't blocked every time that i run this program. anybody can help me? i'm a chinese,this is my first time here,i hope that i express my problem clearly.

            #include
            #include
            #include
            DWORD WINAPI Thread1(void *arg);
            DWORD WINAPI Thread2(void *arg);

            int main(void)
            {
            DWORD threadid1,threadid2;
            HANDLE handle1,handle2;
            handle2 = CreateThread(NULL, 0, Thread2, (void *)handle2, 0, &threadid1);
            handle1 = CreateThread(NULL, 0, Thread1, (void *)handle2, 0, &threadid2);
            HANDLE hEvent;
            hEvent = CreateEvent(NULL, FALSE, FALSE, "MainEvent");

            if(handle1 == NULL)
            {
            	int err;
            	err = GetLastError();
            	printf("CreateThread1 error %d\\n", err);
            	exit(1);
            }
            else
            {
            	printf("CreateThread1 success\\n");
            }
            if(handle2 == NULL)
            {
            	int err;
            	err = GetLastError();
            	printf("CreateThread2 error %d\\n", err);
            	exit(1);
            }
            else
            {
            	printf("CreateThread2 success\\n");
            }
            int priority1;
            int priority2;
            priority1 = GetThreadPriority(handle1);
            printf("thread1's threadid is=%d,priority is %d\\n", (int)threadid1, priority1);
            
            priority2 = GetThreadPriority(handle2);
            printf("thread2's threadid is=%d,priority is %d\\n\\n", (int)threadid2, priority2);
            
            /\*	
            	printf("suspending for 3s\\n");
            	SuspendThread(handle);
            	Sleep(3000);	
            	printf("resume\\n");
            	ResumeThread(handle);
            \*/
            	
            WaitForSingleObject(handle1, INFINITE);
            
            DWORD exitCode;
            BOOL ret;
            ret = GetExitCodeThread(handle1, &exitCode);
            if(!ret)
            {
            	printf("GetThread1 ExitCode,The last error is %d", (int)GetLastError());
            }
            else
            	printf("thread1's exit code is %d\\n", (int)exitCode);
            ret = GetExitCodeThread(handle2, &exitCode);
            if(!ret)
            {
            	printf("GetThread2 ExitCode,The last error is %d", (int)GetLastError());
            }
            else
            	printf("thread2's exit code is %d\\n", (int)exitCode);
            return 0;
            

            }
            int cnt = 0;
            DWORD WINAPI Thread1(void *arg)
            {
            HANDLE hEvent;
            hEvent = OpenEvent(EVENT_ALL_ACCESS, TRUE, "MainEvent");
            if(hEvent == NULL)
            {
            printf("in thread1, OpenEvent Error, Error No. is %d!\n", (int)GetLastError());
            return 1;
            }
            else
            {
            printf("in thread1, OpenEvent success\n");
            }
            printf("3s later, the thread2 will be suspended!\n");
            for(int i = 0; i < 3; i++)
            {
            Sleep(1000);
            SetEvent(hEvent);
            }

            printf("...suspending.....\\n");
            int ret;
            **ret = SuspendThread((HA**
            
            U Offline
            U Offline
            User 10485248
            wrote on last edited by
            #5

            handle1 = CreateThread(NULL, 0, Thread1, (void *)handle2, 0, &threadid1);
            handle2 = CreateThread(NULL, 0, Thread2, (void *)handle1, 0, &threadid2);

            创建Thread1的时候你调用了thread2

            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