大家好:
  我是第一次用,我有个问题,如何用C++实现单/多线程.谢谢各位大哥大姐!

解决方案 »

  1.   

    #include <windows.h> //使用多线程必须的 
    #include <iostream> //这是观看效果用的
    using namespace std;DWORD WINAPI MTOne(LPVOID param); 
    DWORD WINAPI MTTwo(LPVOID param); DWORD WINAPI MTOne(LPVOID param){
      while(true)
      {
        Sleep(1000);
        cout<<"hi! ";
      }
      //一旦返回,这个线程就结束了。
      return 0;

    DWORD WINAPI MTTwo(LPVOID param){
      while(true)
      {
        Sleep(1000);
        cout<<"joy! ";
      }
      return 0;
    }int main(int argc, char* argv[])
    {
      int inp=0;
      HANDLE hand=CreateThread (NULL, 0, MTOne, (void*)&inp, CREATE_SUSPENDED, NULL);
      HANDLE hand2=CreateThread (NULL, 0, MTTwo, (void*)&inp, CREATE_SUSPENDED, NULL);
      while(true){
        cin>>inp;
        if(inp==1) //运行线程
        {
          ResumeThread(hand);
          ResumeThread(hand2);
        }
        else //暂停线程
        {
          SuspendThread(hand);
         SuspendThread(hand2);
        }
      };
      //终止线程
      TerminateThread(hand,1);
      TerminateThread(hand2,1);  return 0;

      

  2.   

    HANDLE CreateThread(
      LPSECURITY_ATTRIBUTES lpThreadAttributes,  // pointer to security attributes
      DWORD dwStackSize,                         // initial thread stack size
      LPTHREAD_START_ROUTINE lpStartAddress,     // pointer to thread function
      LPVOID lpParameter,                        // argument for new thread
      DWORD dwCreationFlags,                     // creation flags
      LPDWORD lpThreadId                         // pointer to receive thread ID
    );
    创建线程就可以了。
      

  3.   

    用_beginThread#include <windows.h>
    #include <process.h>    /* _beginthread, _endthread */
    #include <stddef.h>
    #include <stdlib.h>
    #include <conio.h>void Bounce( void *ch );
    void CheckKey( void *dummy );/* GetRandom returns a random integer between min and max. */
    #define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))BOOL repeat = TRUE;     /* Global repeat flag and video variable */
    HANDLE hStdOut;         /* Handle for console window */
    CONSOLE_SCREEN_BUFFER_INFO csbi;    /* Console information structure */void main()
    {
        CHAR    ch = 'A';    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );    /* Get display screen's text row and column information. */
       GetConsoleScreenBufferInfo( hStdOut, &csbi );    /* Launch CheckKey thread to check for terminating keystroke. */
        _beginthread( CheckKey, 0, NULL );    /* Loop until CheckKey terminates program. */
        while( repeat )
        {
            /* On first loops, launch character threads. */
            _beginthread( Bounce, 0, (void *) (ch++)  );        /* Wait one second between loops. */
            Sleep( 1000L );
        }
    }/* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */
    void CheckKey( void *dummy )
    {
        _getch();
        repeat = 0;    /* _endthread implied */}/* Bounce - Thread to create and and control a colored letter that moves
     * around on the screen.
     *
     * Params: ch - the letter to be moved
     */
    void Bounce( void *ch )
    {
        /* Generate letter and color attribute from thread argument. */
        char    blankcell = 0x20;
        char    blockcell = (char) ch;
        BOOL    first = TRUE;
       COORD   oldcoord, newcoord;
       DWORD   result;
        /* Seed random number generator and get initial location. */
        srand( _threadid );
        newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );
        newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );
        while( repeat )
        {
            /* Pause between loops. */
            Sleep( 100L );        /* Blank out our old position on the screen, and draw new letter. */
            if( first )
                first = FALSE;
            else
             WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result );
             WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result );        /* Increment the coordinate for next placement of the block. */
            oldcoord.X = newcoord.X;
            oldcoord.Y = newcoord.Y;
            newcoord.X += GetRandom( -1, 1 );
            newcoord.Y += GetRandom( -1, 1 );        /* Correct placement (and beep) if about to go off the screen. */
            if( newcoord.X < 0 )
                newcoord.X = 1;
            else if( newcoord.X == csbi.dwSize.X )
                newcoord.X = csbi.dwSize.X - 2;
            else if( newcoord.Y < 0 )
                newcoord.Y = 1;
            else if( newcoord.Y == csbi.dwSize.Y )
                newcoord.Y = csbi.dwSize.Y - 2;        /* If not at a screen border, continue, otherwise beep. */
            else
                continue;
            Beep( ((char) ch - 'A') * 100, 175 );
        }
        /* _endthread given to terminate */
        _endthread();
    }
      

  4.   

    我写了一个简单的线程类, 有兴趣看一看, 不是MFC的;我在G++下用的
    blog.csdn.net/wujian53
      

  5.   

    最好不要用CreateThread(...),用_beginthreadex(...)
      

  6.   

    可以用setjmp()longjmp()自己实现多线程
      

  7.   

    http://hao.kjz.cn/xml/040917124516.xml
      

  8.   

    #include<windows.h>
    #include<conio.h>
    #include<stdio.h>
    DWORD ChildMain();
    DWORD ChildMain1();
    int Global=0;
    main()
    {
     int j ,b;
     INT giThreadNumber=1;
     DWORD dwThreadId;
     HANDLE hThread_2,hThread_3;
     DWORD dwCreationFlags=0;
     hThread_2=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ChildMain,(LPVOID)giThreadNumber,dwCreationFlags,&dwThreadId);
     hThread_3=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ChildMain1,(LPVOID)giThreadNumber,dwCreationFlags,&dwThreadId);
        printf("创建线程完成,线程1运行!\n");
     for(j=0;j<50000;j++);
     printf("线程1空循环%d次Global=%d\n",j,Global);
     printf("线程1输入!\n");
     b=getche();
     printf("线程1第1次输入结果:%c\n",b);
     b=getche();
     printf("线程1第2次输入结果:%c\n",b);
     getch();
    }
    DWORD ChildMain(giThreadNumber)
    {
     int j,b;
     printf("线程2开始运行!\n");
     Global=1000;
     for(j=0;j<4000000;j++);
      printf("线程2空循环%d次Global=%d\n",j,Global);
     Global=2;
     printf("线程2输入!\n");
     b=getche();
     printf("线程2第1次输入结果:%c\n",b);
     b=getche();
     printf("线程2第2次输入结果:%c\n",b);
     while(1)
      b=j+5;
    }
    DWORD ChildMain1(giThreadNumber)
    {
     int j,b;
     printf("线程3开始运行!\n");
     Global=6;
     for(j=0;j<40000;j++);
      printf("线程3空循环%d次Global=%d\n",j,Global);
     Global=2;
     printf("线程3输入!\n");
     b=getche();
     printf("线程3第1次输入结果:%c\n",b);
     b=getche();
     printf("线程3第2次输入结果:%c\n",b);
     while(1)
      b=j+5;