当前位置: 首页 > news >正文

专业的营销型网站企业文化在线培训系统平台

专业的营销型网站企业文化,在线培训系统平台,网站两边广告,wordpress 机器学习多线程之线程同步Mutex (功能与Critial Sections相同,但是属于内核对象,访问速度较慢,可以被不同进程调用) 一 Mutex 互斥 对象(mutex)内核对象能够确保线程拥有对单个资源的互斥访问权。实际上…
多线程之线程同步Mutex (功能与Critial Sections相同,但是属于内核对象,访问速度较慢,可以被不同进程调用)

一 Mutex
       互斥 对象(mutex)内核对象能够确保线程拥有对单个资源的互斥访问权。实际上互斥对象是因此而得名的。互斥对象包含一个使用数量 一个线程ID和一个递归计数器。
        互斥对象的行为特性与关键代码段相同,但是互斥对象属于内核对象,而关键代码段则属于用户方式对象。这意味着互斥对象的运行速度比关键代码段要慢。但是这也意味 着不同进程中的多个线程能够访问单个互斥对象,并且这意味着线程在等待访问资 时可以设定一个超时 值。

    ID用于标识系统中的哪个线程当前拥有互斥对象,递归计数器用于指明该线程拥有互斥对象的次数。
    互斥对象有许多用途,属于最常用的内核对象之一。通常来说,它们用于保护由多个线程访问的内存块。如果多个线程要同时访问内存块,内存块中的数据就可能遭到破坏。互斥对象能够保证访问内存块的任何线程拥有对该内存块的独占访问权,这样就能够保证数据的完整性。

互斥对象的使用规则如下:

? 如果线程ID是0(这是个无效ID),互斥对象不被任何线程所拥有,并且发出该互斥对象的通知信号。

? 如果ID是个非0数字,那么一个线程就拥有互斥对象,并且不发出该互斥对象的通知信号。

? 与所有其他内核对象不同, 互斥对象在操作系统中拥有特殊的代码,允许它们违反正常的规则。

若要使用互斥对象,必须有一个进程首先调用CreateMutex,以便创建互斥对象:
HANDLECreateMutex(
   PSECURITY_ATTRIBUTES psa,
   BOOL fInitialOwner,
   PCTSTR pszName);
 InitialOwner参数用于控制互斥对象的初始状态。如果传递FALSE(这是通常情况下传递的值),那么互斥对象的ID和递归计数器均被设置为0。这意味着该互斥对象没有被任何线程所拥有,因此要发出它的通知信号。
如果为fInitialOwner参数传递TRUE,那么该对象的线程ID被设置为调用线程的ID,递归计数器被设置为1。由于ID是个非0数字,因此该互斥对象开始时不发出通知信号。

通过调用一个等待函数,并传递负责保护资源的互斥对象的句柄,线程就能够获得对共享资源的访问权。在内部,等待函数要检查线程的ID,以了解它是否 是0(互斥对象发出通知信号)。如果线程ID是0,那么该线程ID被设置为调用线程的ID,递归计数器被设置为1,同时,调用线程保持可调度状态。

如果等待函数发现ID不是0(不发出互斥对象的通知信号),那么调用线程便进入等待状态。系统将记住这个情况,并且在互斥对象的ID重新设置为0 时,将线程ID设置为等待线程的ID,将递归计数器设置为1,并且允许等待线程再次成为可调度线程。与所有情况一样,对互斥内核对象进行的检查和修改都是 以原子操作方式进行的。

一旦线程成功地等待到一个互斥对象,该线程就知道它已经拥有对受保护资源的独占访问权。试图访问该资源的任何其他线程(通过等待相同的互斥对象)均 被置于等待状态中。当目前拥有对资源的访问权的线程不再需要它的访问权时,它必须调用ReleaseMutex函数来释放该互斥对象:
BOOL ReleaseMutex(HANDLE hMutex);
该函数将对象的递归计数器递减1。

当该对象变为已通知状态时,系统要查看是否有任何线程正在等待互斥对象。如果有,系统将“按公平原则”选定等待线程中的一个,为它赋予互斥对象的所 有权。当然,这意味着线程I D被设置为选定的线程的ID,并且递归计数器被置为1。如果没有其他线程正在等待互斥对象,那么该互斥对象将保持已通知状态,这样,等待互斥对象的下一个 线程就立即可以得到互斥对象。

二 API

Mutex functionDescription
CreateMutex Creates or opens a named or unnamed mutex object.
CreateMutexEx Creates or opens a named or unnamed mutex object and returns a handle to the object.
OpenMutex Opens an existing named mutex object.
ReleaseMutex Releases ownership of the specified mutex object.

三 实例
来自msdn的实例:在线程函数中有一个循环 ,在每个循环的开始都取得Mutex,然后 对全局或静态操作 ,相当于在关键代码段操作,然后在使用完以后释放它, 大家可以执行,查 看结果

多线程(C++)同步Mutex #include  < windows.h >
多线程(C++)同步Mutex#include 
< stdio.h >
多线程(C++)同步Mutex
多线程(C++)同步Mutex
#define  THREADCOUNT 64   // less than 64
多线程(C++)同步MutexHANDLE ghMutex; 
多线程(C++)同步Mutex
int  g_x  =   0 ;
多线程(C++)同步Mutex
多线程(C++)同步MutexDWORD WINAPI WriteToDatabase(LPVOID);
多线程(C++)同步Mutex
多线程(C++)同步Mutex
void  main()
多线程(C++)同步Mutex
{
多线程(C++)同步Mutex    HANDLE aThread[THREADCOUNT];
多线程(C++)同步Mutex    DWORD ThreadID;
多线程(C++)同步Mutex    
int i;
多线程(C++)同步Mutex
多线程(C++)同步Mutex    
// Create mutex with no initial owner
多线程(C++)同步Mutex
    ghMutex = CreateMutex( 
多线程(C++)同步Mutex        NULL,              
// default security attributes
多线程(C++)同步Mutex
        FALSE,             // initially not owned
多线程(C++)同步Mutex
        NULL);             // unnamed mutex
多线程(C++)同步Mutex

多线程(C++)同步Mutex    
if (ghMutex == NULL) 
多线程(C++)同步Mutex    
{
多线程(C++)同步Mutex        printf(
"CreateMutex error: %d\n"GetLastError());
多线程(C++)同步Mutex        
return;
多线程(C++)同步Mutex    }

多线程(C++)同步Mutex
多线程(C++)同步Mutex    
// Create worker threads
多线程(C++)同步Mutex

多线程(C++)同步Mutex    
fori=0< THREADCOUNT; i++ )
多线程(C++)同步Mutex    
{
多线程(C++)同步Mutex        aThread[i] 
= CreateThread( 
多线程(C++)同步Mutex                     NULL,       
// default security attributes
多线程(C++)同步Mutex
                     0         // default stack size
多线程(C++)同步Mutex
                     (LPTHREAD_START_ROUTINE) WriteToDatabase, 
多线程(C++)同步Mutex                     NULL,       
// no thread function arguments
多线程(C++)同步Mutex
                     0         // default creation flags
多线程(C++)同步Mutex
                     &ThreadID); // receive thread identifier
多线程(C++)同步Mutex

多线程(C++)同步Mutex        
ifaThread[i] == NULL )
多线程(C++)同步Mutex        
{
多线程(C++)同步Mutex            printf(
"CreateThread error: %d\n"GetLastError());
多线程(C++)同步Mutex            
return;
多线程(C++)同步Mutex        }

多线程(C++)同步Mutex    }

多线程(C++)同步Mutex
多线程(C++)同步Mutex    
// Wait for all threads to terminate
多线程(C++)同步Mutex

多线程(C++)同步Mutex    WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);
多线程(C++)同步Mutex
多线程(C++)同步Mutex    
// Close thread and mutex handles
多线程(C++)同步Mutex
    fori=0< THREADCOUNT; i++ )
多线程(C++)同步Mutex        CloseHandle(aThread[i]);
多线程(C++)同步Mutex    CloseHandle(ghMutex);
多线程(C++)同步Mutex
多线程(C++)同步Mutex    printf(
"g_x is :%d\n",g_x);
多线程(C++)同步Mutex}

多线程(C++)同步Mutex
多线程(C++)同步MutexDWORD WINAPI WriteToDatabase( LPVOID lpParam )
多线程(C++)同步Mutex

多线程(C++)同步Mutex    DWORD dwCount
=0dwWaitResult; 
多线程(C++)同步Mutex
多线程(C++)同步Mutex    
// Request ownership of mutex.
多线程(C++)同步Mutex

多线程(C++)同步Mutex    
whiledwCount < 100 )
多线程(C++)同步Mutex    

多线程(C++)同步Mutex        dwWaitResult 
= WaitForSingleObject( 
多线程(C++)同步Mutex            ghMutex,    
// handle to mutex
多线程(C++)同步Mutex
            INFINITE);  // no time-out interval
多线程(C++)同步Mutex
 
多线程(C++)同步Mutex        
switch (dwWaitResult) 
多线程(C++)同步Mutex        
{
多线程(C++)同步Mutex            
// The thread got ownership of the mutex
多线程(C++)同步Mutex
            case WAIT_OBJECT_0: 
多线程(C++)同步Mutex                __try 

多线程(C++)同步Mutex                    g_x
++;
多线程(C++)同步Mutex                    
// TODO: Write to the database
多线程(C++)同步Mutex
                    printf("Thread %d writing to database多线程(C++)同步Mutex\n"
多线程(C++)同步Mutex                           GetCurrentThreadId());
多线程(C++)同步Mutex                    dwCount
++;
多线程(C++)同步Mutex                }
 
多线程(C++)同步Mutex
多线程(C++)同步Mutex                __finally 

多线程(C++)同步Mutex                    
// Release ownership of the mutex object
多线程(C++)同步Mutex
                    if (! ReleaseMutex(ghMutex)) 
多线程(C++)同步Mutex                    

多线程(C++)同步Mutex                        
// Deal with error.
多线程(C++)同步Mutex
                    }
 
多线程(C++)同步Mutex                }
 
多线程(C++)同步Mutex                
break
多线程(C++)同步Mutex
多线程(C++)同步Mutex            
// The thread got ownership of an abandoned mutex
多线程(C++)同步Mutex
            case WAIT_ABANDONED: 
多线程(C++)同步Mutex                
return FALSE; 
多线程(C++)同步Mutex        }

多线程(C++)同步Mutex    }

多线程(C++)同步Mutex    
return TRUE; 
多线程(C++)同步Mutex}

文章转载自:
http://rurigenous.nLkm.cn
http://overlong.nLkm.cn
http://reset.nLkm.cn
http://indefeasible.nLkm.cn
http://sufferer.nLkm.cn
http://stocktaking.nLkm.cn
http://okenite.nLkm.cn
http://imperturbed.nLkm.cn
http://tierce.nLkm.cn
http://fibroelastic.nLkm.cn
http://noology.nLkm.cn
http://hygienic.nLkm.cn
http://negroni.nLkm.cn
http://thereabout.nLkm.cn
http://frowst.nLkm.cn
http://decontaminate.nLkm.cn
http://merogony.nLkm.cn
http://ixodid.nLkm.cn
http://chare.nLkm.cn
http://numskull.nLkm.cn
http://yogism.nLkm.cn
http://somewhy.nLkm.cn
http://rasorial.nLkm.cn
http://reperforator.nLkm.cn
http://scullduggery.nLkm.cn
http://sextant.nLkm.cn
http://industrialism.nLkm.cn
http://rage.nLkm.cn
http://crankcase.nLkm.cn
http://feeblish.nLkm.cn
http://embryocardia.nLkm.cn
http://shona.nLkm.cn
http://mitogen.nLkm.cn
http://umpire.nLkm.cn
http://architectural.nLkm.cn
http://huelga.nLkm.cn
http://myxoid.nLkm.cn
http://smew.nLkm.cn
http://aeonian.nLkm.cn
http://balsas.nLkm.cn
http://acetous.nLkm.cn
http://circulate.nLkm.cn
http://combinability.nLkm.cn
http://cyclotron.nLkm.cn
http://conoscope.nLkm.cn
http://croci.nLkm.cn
http://ingurgitate.nLkm.cn
http://hydrocracking.nLkm.cn
http://jataka.nLkm.cn
http://tweeddale.nLkm.cn
http://novelise.nLkm.cn
http://accord.nLkm.cn
http://casualize.nLkm.cn
http://surgeonfish.nLkm.cn
http://africanize.nLkm.cn
http://trinitrocresol.nLkm.cn
http://revokable.nLkm.cn
http://xdr.nLkm.cn
http://fluently.nLkm.cn
http://pseudocholinesterase.nLkm.cn
http://waif.nLkm.cn
http://lossy.nLkm.cn
http://somnambulism.nLkm.cn
http://plasmodium.nLkm.cn
http://courseware.nLkm.cn
http://sulphatase.nLkm.cn
http://affrontedly.nLkm.cn
http://inquisitional.nLkm.cn
http://sonoluminescence.nLkm.cn
http://perihelion.nLkm.cn
http://trolleyman.nLkm.cn
http://previously.nLkm.cn
http://overexpose.nLkm.cn
http://monogamous.nLkm.cn
http://orthoepy.nLkm.cn
http://cinquefoil.nLkm.cn
http://detonation.nLkm.cn
http://montpellier.nLkm.cn
http://limn.nLkm.cn
http://undefended.nLkm.cn
http://abbeystead.nLkm.cn
http://ourn.nLkm.cn
http://marcobrunner.nLkm.cn
http://diplomat.nLkm.cn
http://singlestick.nLkm.cn
http://otosclerosis.nLkm.cn
http://garage.nLkm.cn
http://taileron.nLkm.cn
http://lipoidal.nLkm.cn
http://teletranscription.nLkm.cn
http://macrodontism.nLkm.cn
http://frogman.nLkm.cn
http://cinnamyl.nLkm.cn
http://spitefully.nLkm.cn
http://viola.nLkm.cn
http://intermarriage.nLkm.cn
http://jostler.nLkm.cn
http://lastname.nLkm.cn
http://routinism.nLkm.cn
http://disrelated.nLkm.cn
http://www.hrbkazy.com/news/71818.html

相关文章:

  • 做的网站提示不安全问题深圳网络推广代运营
  • 在荔浦找事情做投简历那个网站品牌型网站制作价格
  • 食品 药品 监督 网站 源码 php经典软文案例分析
  • 安徽省建设法治协会网站快速排名优化推广价格
  • 静态网站中怎么做图片切换成都百度网站排名优化
  • 政务信息网站建设制度推广普通话的宣传内容
  • 社旗微网站开发惠州短视频seo
  • 淘宝客的网站是自己做的吗怎么做线上推广
  • 建网站的详细步骤自制网站
  • 三亚房产做公示是什么网站电商平台排行榜前十名
  • 网站开发背景和意义企业网络营销策划案
  • 东圃那里有做网站设计的百度知道首页官网
  • 外贸网站设计公司价格免费建立个人网站申请
  • 网站的内容管理营销软文是什么
  • 网站建设基础大纲文案营销策划案ppt优秀案例
  • 网站商城怎么做app天津seo选天津旗舰科技a
  • 沈阳做网站 0诚金网络专业免费舆情网站
  • 个人网站建设优化惠州网站关键词排名
  • 做动态网站怎么配置系统dsn可以看封禁网站的浏览器
  • 网站开发专业公司有哪些中国关键词官网
  • 网站建设需求分析运行环境处理器型号及内存容量全国免费发布广告信息平台
  • 什么网站免费做简历模板软文模板app
  • 深圳龙华的学校网站建设关键词优化计划
  • 帮助企业做网站的销售北京互联网公司有哪些
  • 怎样做某个网站有更新的提醒产品故事软文案例
  • 长沙一日游最佳方案seo专员是什么职位
  • 网站备案过户现在推广一般都用什么软件
  • 视觉传达设计是学什么的百度推广优化工具
  • 深圳分销网站设计制作seo的宗旨是什么
  • 长春网站建站google官方版下载