子弹 の VISIONS

NEVER back down ~~

C++博客 首页 新随笔 联系 聚合 管理
  112 Posts :: 34 Stories :: 99 Comments :: 0 Trackbacks

// created by ztwaker on 2006-8-16

// Used for:  multithread app.

 

#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#include < iostream >

using namespace std ;

 

#include <process.h>

 

const int n      = 8;            // threads count

const int size   = 1025;         // 1048577 = 1024*1024+1;

const int psize = ( size -1)/ n ;   // size per thread

char   pch [ size ] = { 0 };    // share memory

HANDLE threadpool [ n ];

 

void proc( void * p )

{

    char * px = static_cast < char *>( p );

    for ( int i = 0; i != psize ; ++ i )

    {

        //*(px+i) = char('0' + rand() % 2);

        *( px + i ) = char ( 'A' + ( px - pch )/ psize );

    }

    cout << "done" << endl ;

    _endthread ();

}

 

void test()

{

    for ( int i = 0; i != n ; ++ i )

    {

        char * pt = pch + psize * i ;

        unsigned long handle = _beginthread (proc, 0, static_cast < void *>( pt ));

        if ( handle != -1)

        {

            threadpool [ i ] = reinterpret_cast < HANDLE >( handle );

            CloseHandle ( threadpool [ i ]);

         }

    }

   

    WaitForMultipleObjects ( n , threadpool , true , INFINITE );

}

 

 

int main()

{

    test();

    Sleep (0);

    cout << pch << endl ;

    return 0;

}

posted on 2006-08-16 09:42 子弹のVISIONS 阅读(657) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理