我住包子山

this->blog.MoveTo("blog.baozishan.in")

翻译一篇文章Introduction to Multi-threaded Code 多线程编程的一些代码 已经全译好了

Someone recently asked me what I recommend for synchronizing worker threads and I suggested setting an event. This person's response was that you could not do that since worker threads do not support a message pump (UI threads are required to support messages). The confusion here is that events and messages are different animals under windows.

我忘记了我从哪里copy的这些例子代码,他们可是非常简单而有趣的。如果有人知道这些代码的作者,我一定要好好感谢你和这位作者。

注意这里有很多对于没有提及的MFC的支持。像_beginthread(一个C运行时库调用)的API可以在MFC应用程序中替换成AfxBeginThread

无同步(No Synchronization)

这第一个例子描述了两个互不同步的线程。进程中的首要线程--主函数循环,输出全局整形数组的内容。还有一个线程“Thread”不停的给数组每个元素+1。
 The thread called "Thread" continuously populates the global array of integers.

				  #include <process.h>
				  #include <stdio.h>
				int a[ 5 ];
  
  void Thread( void* pParams )
  { int i, num = 0;
  
    while ( 1 )
    { 
       for ( i = 0; i < 5; i++ ) a[ i ] = num;
       num++;
    }
  }
  
  int main( void )
  { 
     _beginthread( Thread, 0, NULL );
  
     while( 1 )
        printf("%d %d %d %d %d\n", 
               a[ 0 ], a[ 1 ], a[ 2 ],
               a[ 3 ], a[ 4 ] );
  
   return0;
  }

注意这个例子的输出,红色的数处在一个主线程抢先于Thread工作过程中执行的打印动作

81751652 81751652 81751651 81751651 81751651
81751652 81751652 81751651 81751651 81751651
83348630 83348630 83348630 83348629 83348629
83348630 83348630 83348630 83348629 83348629
83348630 83348630 83348630 83348629 83348629

 

关键区域/临界区域 对象(Critical Section Objects)

如果你想让主线程等待Thread线程处理好全局数组再做打印,一种解决方法是使用关键区域对象。
关键区域对象提供同步于使用互斥器(Mutex)对象很相似, 除了关键区域对象之能在一个进程内发挥效用。Event, mutex, 以及 semaphore 对象也可以用在单进程的应用程序中, 但是关键区域对象提供一个相对快捷更加高效的同步机制. 就像互斥器一样, 一个关键区域对象只能同时被一个线程拥有, 这个关键区域能够在同时发生的数据存取时保护共享资源. 获取关键区域的先后顺序不定,可是不用太担心,系统对于每一个线程都是平等的。

     
  CRITICAL_SECTION cs;
  int a[ 5 ];
  
  void Thread( void* pParams )
  {
    int i, num = 0;
  
    while ( TRUE )
    {
       EnterCriticalSection( &cs );
       for ( i = 0; i < 5; i++ ) a[ i ] = num;
       LeaveCriticalSection( &cs );
       num++;
    }
  }
  
  int main( void )
{ InitializeCriticalSection( &cs ); _beginthread( Thread, 0, NULL ); while( TRUE ) { EnterCriticalSection( &cs ); printf( "%d %d %d %d %d\n", a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ] ); LeaveCriticalSection( &cs ); } return 0; }

If you are running Windows 9x/NT/2000, you can run this program by clicking here.

互斥器(Mutex Objects)

一个互斥器是一个信号状态的同步对象,当它不属于任何一个线程时就用信号来体现,当被拥有时他的信号状态就为None. 同一时刻只有一个线程可以拥有互斥器, 互斥器这个名字来自于他们对于并列的线程存取共享资源时表现出的行为。举个例子,避免两个线程同时写入一个共享内存,每一个线程当需要执行存取共享资源的代码时首先等待直到自己获得拥有权. 在存取共享资源之后,线程释放对互斥器的拥有权。

两个或以上的进程可以调用CreateMutex 来建立同样名字的互斥器. 实际上第一个进程建立的这个互斥器, 随后的进程只是得到了那个存在的互斥器的句柄. 这能使多进程共用一个互斥器, 当然用户应该有确保建立互斥器的进程首先启动的责任. 使用这种技术,你应该将这个 bInitialOwner标记设置成FALSE; 否则, 它可以因不同的进程最初拥有它而带来困难.

多进程可以有同一个mutex对象的句柄, 让mutex对象能够用于多进程间同步. 下面的对象共享机制是适用的:

  • 一个子进程通过CreateProcess 函数被建立,当CreateMutex的lpMutexAttributes 参数给予相应的mutex对象指针它可以继承到一个mutex对象的句柄.
  • 一个进程可以在DuplicateHandle 函数中指定一个mutex对象句柄来建立一个句柄的拷贝由其他进程使用.
  • 一个继承可以指定一个mutex的名字通过 CreateMutex 函数得到这个mutex对象的句柄.

总的来说, 如果你想要进行线程同步,临界区域更高效些.

				#include <windows.h>
				#include <process.h>
				#include <stdio.h>
  
  HANDLE hMutex;
  int a[ 5 ];
  
  void Thread( void* pParams )
  { 
     int i, num = 0;
  
     while ( TRUE )
     { 
        WaitForSingleObject( hMutex, INFINITE );
        for ( i = 0; i < 5; i++ ) a[ i ] = num;
        ReleaseMutex( hMutex );
        num++;
     }
  }
  
  int main( void )
  {
     hMutex = CreateMutex( NULL, FALSE, NULL );
     _beginthread( Thread, 0, NULL );
  
     while( TRUE )
{ WaitForSingleObject( hMutex, INFINITE ); printf( "%d %d %d %d %d\n", a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ] ); ReleaseMutex( hMutex ); } return0; }

If you are running Windows 9x/NT/2000, you can run this program by clicking here.

Event Objects事件对象

若我们想要强制第二线程在主线程完成全局数组的内容输出时执行该如何?这样的话每行的输出就只是递增1。

一个事件对象也是一个可以通过SetEvent or PulseEvent 函数设置像信号般的状态的同步对象. 下面是两种类型的事件对象.

Object Description
Manual-reset event
手动激发对象
只有使用ResetEvent 函数才可以将其设置为无激发状态. 当它在激发状态时, 它会激发所有正在等待的线程, 执行对相同 event对象的线程会立即从wait函数返回.
Auto-reset event
自动激发对象
一个只相应一个线程的wait函数的事件对象(当这个对象是激发状态),wait函数返回同时事件对象自动变成无激发状态 ,当没有线程执行wait事件对象仍然是激发状态.

event object的用处就在于它可以在它发生时向等待着的线程发出信号标志从而使其wait结束. 举个例子, 在overlapped I/O 操作时, 当异步操作完成时系统设置了那个由程序员指定(specified)的事件对象为信号状态. A 一个单一线程可以指定许多不同的事件对象在许多同时发生的overlapped 操作运作, 调用一个多对象的wait函数可以当任意一个event object激发时结束等待.

在一个线程中可使用 CreateEvent 函数建立一个event object. 在这个线程中指定这个event object 的特性是manual-reset 或者 auto-reset . 在这个线程中也可以命名一个event object. 其他进程中的线程也可以使用 OpenEvent 通过event object的名字打开一个现存event object . 另外关于mutex, event, semaphore, 以及 timer objects的其他信息, 就参考《Interprocess Synchronization》的文章.

一个线程能够用 PulseEvent 函数设置一个event object 为信号状态而后激发当前适当数量的wait线程,之后切换为无信号状态 . 对于一个manual-reset event object, 所有的等待线程被返回(release). 对于一个auto-reset event object, 这个函数只能释放一个等待的线程, 即使有更多线程在等待. 如果没有线程在函数调用时等待, PulseEvent 只是简单的将事件状态设为无信号并且返回(个人注释,这应该是跟setevent最不相同的地方!).

Collapse
				  #include <windows.h>
				  #include <process.h>
				  #include <stdio.h>
  
  HANDLE hEvent1, hEvent2;
  int a[ 5 ];
  
  void Thread( void* pParams )
  {
     int i, num = 0;

     while ( TRUE )
     {
        WaitForSingleObject( hEvent2, INFINITE );
        for ( i = 0; i < 5; i++ ) a[ i ] = num;
        SetEvent( hEvent1 );
        num++;
     }
  }
  
  int main( void )
  {
     hEvent1 = CreateEvent( NULL, FALSE, TRUE, NULL );
     hEvent2 = CreateEvent( NULL, FALSE, FALSE, NULL );
  
     _beginthread( Thread, 0, NULL );
  
     while( TRUE )
     { 
        WaitForSingleObject( hEvent1, INFINITE );
        printf( "%d %d %d %d %d\n", 
                a[ 0 ], a[ 1 ], a[ 2 ],
                a[ 3 ], a[ 4 ] );
        SetEvent( hEvent2 );
     }
     return0;
  }

If you are running Windows 9x/NT/2000, you can run this program by clicking here.

Summary of Synchronization Objects

The MSDN News for July/August 1998 has a front page article on Synchronization Objects. The following table is from that article:

Name Relative speed Cross process Resource counting Supported platforms
Critical Section Fast No No (exclusive access) 9x/NT/CE
Mutex Slow Yes No (exclusive access) 9x/NT/CE
Semaphore Slow Yes Automatic 9x/NT
Event Slow Yes Yes 9x/NT/CE
Metered Section Fast Yes Automatic 9x/NT/CE

by William T. Block


from codeproject

谢谢回复的补充 ~~,上面拼错了个词,改过。。译完了

posted on 2007-02-16 14:06 Gohan 阅读(956) 评论(1)  编辑 收藏 引用 所属分类: C++

Feedback

# re: 翻译一篇文章Introduction to Multi-threaded Code 多线程编程的一些代码(先翻一点) 2007-02-22 17:10 池凤彬

http://www.codeproject.com/script/profile/whos_who.asp?vt=arts&id=244

William T. Block View details
Status Gold. Member No. 244

View Member's Blog.
Awards
Messages Posted 11 - Poster
Articles Submitted
3 - Contributor
Biography Bill's recent projects include graphical displays and printing of real-time data for the Oil Industry.

"I started programming Windows' applications right after the release of Windows 1.0 and I am now actively working with Microsoft .NET"

He currently works for Baker Hughes in the Houston, Texas area.
Birthday Thursday 17th November, 1949
Location United States
Occupation Software development
Interests C++, MFC, Win32, C#, ASP.NET
Member since Thursday 6th July, 2000
(6 years, 7 months) Gold Level
Homepage http://www.wtblock.com/resume/  回复  更多评论   


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