Introduction to Multi-threaded Code

大数据法律监督平台课题项目管理制度.docx 立即下载

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.

I have forgotten where I originally copied these examples, but I found them to be interesting because of their simplicity. If anyone is aware of the author of this code, I would appreciate hearing from you, so I can give him/her credit.

Note there is considerable support for threads in MFC that is not covered here. API's like _beginthread (a C runtime library call) would likely be replaced by MFC API's like AfxBeginThread in an MFC application.

No Synchronization

This first example illustrates two unsynchronized threads. The main loop, which is the primary thread of a process, prints the contents of a global array of integers. The thread called "Thread" continuously populates the global array of integers.

Collapse | Copy Code
  #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 ] );
  
   return 0;
  }

Note in this sample output, the numbers in red illustrate a state where the primary thread preempted the secondary thread in the middle of populating the values of the array:

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

If you are running Windows 9x/NT/2000, you can run this program by clicking here. After the program begins to run, press the "Pause" key stop the display output (this stops the primary thread's I/O, but the secondary thread continues to run in the background) and any other key to restart it.

Critical Section Objects

What if your main thread needed all elements of the array to processed prior to reading? One solution is to use a critical section.

Critical section objects provide synchronization similar to that provided by mutex objects, except critical section objects can be used only by the threads of a single process. Event, mutex, and semaphore objects can also be used in a single-process application, but critical section objects provide a slightly faster, more efficient mechanism for mutual-exclusion synchronization. Like a mutex object, a critical section object can be owned by only one thread at a time, which makes it useful for protecting a shared resource from simultaneous access. There is no guarantee about the order in which threads will obtain ownership of the critical section, however, the system will be fair to all threads.

Collapse | Copy Code
  #include <windows.h>
  #include <process.h>
  #include <stdio.h>
  
  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

A mutex object is a synchronization object whose state is set to signaled when it is not owned by any thread, and non-signaled when it is owned. Only one thread at a time can own a mutex object, whose name comes from the fact that it is useful in coordinating mutually exclusive access to a shared resource. For example, to prevent two threads from writing to shared memory at the same time, each thread waits for ownership of a mutex object before executing the code that accesses the memory. After writing to the shared memory, the thread releases the mutex object.

Two or more processes can call CreateMutex to create the same named mutex. The first process actually creates the mutex, and subsequent processes open a handle to the existing mutex. This enables multiple processes to get handles of the same mutex, while relieving the user of the responsibility of ensuring that the creating process is started first. When using this technique, you should set the bInitialOwner flag to FALSE; otherwise, it can be difficult to be certain which process has initial ownership.

Multiple processes can have handles of the same mutex object, enabling use of the object for interprocess synchronization. The following object-sharing mechanisms are available:

  • A child process created by the CreateProcess function can inherit a handle to a mutex object if the lpMutexAttributes parameter of CreateMutex enabled inheritance.
  • A process can specify the mutex-object handle in a call to the DuplicateHandle function to create a duplicate handle that can be used by another process.
  • A process can specify the name of a mutex object in a call to the OpenMutex or CreateMutex function.

Generally speaking, if you are synchronizing threads within the same process, a critical section object is more efficient.

Collapse | Copy Code
  #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 ); } return 0; }

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

Event Objects

What if we want to force the secondary thread to run each time the primary thread finishes printing the contents of the global array, so that the values in each line of output is only incremented by one?

An event object is a synchronization object whose state can be explicitly set to signaled by use of the SetEvent or PulseEvent function. Following are the two types of event object.

ObjectDescription
Manual-reset eventAn event object whose state remains signaled until it is explicitly reset to non-signaled by the ResetEvent function. While it is signaled, any number of waiting threads, or threads that subsequently specify the same event object in one of the wait functions, can be released.
Auto-reset eventAn event object whose state remains signaled until a single waiting thread is released, at which time the system automatically sets the state to non-signaled. If no threads are waiting, the event object's state remains signaled.

The event object is useful in sending a signal to a thread indicating that a particular event has occurred. For example, in overlapped input and output, the system sets a specified event object to the signaled state when the overlapped operation has been completed. A single thread can specify different event objects in several simultaneous overlapped operations, then use one of the multiple-object wait functions to wait for the state of any one of the event objects to be signaled.

A thread uses the CreateEvent function to create an event object. The creating thread specifies the initial state of the object and whether it is a manual-reset or auto-reset event object. The creating thread can also specify a name for the event object. Threads in other processes can open a handle to an existing event object by specifying its name in a call to the OpenEvent function. For additional information about names for mutex, event, semaphore, and timer objects, see Interprocess Synchronization.

A thread can use the PulseEvent function to set the state of an event object to signaled and then reset it to non-signaled after releasing the appropriate number of waiting threads. For a manual-reset event object, all waiting threads are released. For an auto-reset event object, the function releases only a single waiting thread, even if multiple threads are waiting. If no threads are waiting, PulseEvent simply sets the state of the event object to non-signaled and returns.

Collapse | Copy Code
  #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 );
     }
     return 0;
  }

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:

NameRelative speedCross processResource countingSupported platforms
Critical SectionFastNoNo (exclusive access)9x/NT/CE
MutexSlowYesNo (exclusive access)9x/NT/CE
SemaphoreSlowYesAutomatic9x/NT
EventSlowYesYes9x/NT/CE
Metered SectionFastYesAutomatic9x/NT/CE

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

夏BC1700年世界历史地图矢量文件SHP格式.zip 本资源为《夏BC1700年世界历史地图矢量文件SHP格式》压缩包,文件大小约 16.17 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。 立即下载

相关推荐

YOLO26算法公共场所口罩佩戴状态目标检测+训练好的模型+1616张数据集+pyqt可视化界面.zip

附训练好的 YOLO 权重与 PyQt 可视化检测界面,页面底部含可视化效果预览,毕设/课设开箱即用。 【数据集概况】 · 检测类别(中文):[戴口罩(with_mask), 未戴口罩(withoute_mask)] · 总计:1616 张 · 测试集:107 张 · 验证集:247 张 · 训练集:1262 张 【训练曲线与评估图】 【模型训练配置】 参数 | 值 模型 | yolo26n 训练轮数 | 100 epochs 输入尺寸 | 640x640 批次大小 | 24 优化器 | auto 初始学习率 | 0.01 训练设备 【关键指标汇总】 训练了 68 个 epoch,最终轮指标: 指标 | 数值 mAP50 | **0.9730** mAP50-95 | 0.6995 Precision | 0.9338 Recall | 0.9466 train/box_loss | 0.7919 train/cls_loss | 0.3896 val/box_loss | 0.8619 val/cls_loss | 0.3208 【训练过程分析】 这次训练从第一天的早晨开始,我就感受到了那股紧绷的气氛。模型刚开始时,loss 下降迅速,像是一直在寻找着正确的方向。尤其是前 20 轮,loss 从一个相对较高的 1.5 跌到了 1.0 以下,这让我在工作日的上午都有点喘不过气来。然而,从第 20 轮开始,loss 似乎进入了一个平台期,从 0.9 跃升到 0.7 需要的时间明显长了。虽然这段时间有点艰难,但也让我意识到模型已经学会了基本的特征。进入最后的 20 轮,我明显感觉到收敛的速度放慢了,loss 在 0.5 和 0.6 之间反复。在这个阶段,我不断地调整一些参数,希望能够找到一个平衡点。不过,最终... 使用场景:毕业设计、课程设计、深度学习入门、工业场景落地验证。

并行分布式框架:Celery、rq、funboost

Celery是一个异步任务的调度工具,也可以叫做 "分布式任务队列(Distributed Task Queue)"。

freeking101的博客 5万+

夏BC1640年世界历史地图矢量文件SHP格式.zip

本资源为《夏BC1640年世界历史地图矢量文件SHP格式》压缩包,文件大小约 15.66 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。

【视觉目标跟踪】摄像机与目标运动因果关系分析及Matlab仿真系统设计【视觉目标跟踪】视觉目标跟踪中摄像机的运动与被跟踪对象的运动之间存在因果关系研究(Matlab代码实现)

内容概要:本文聚焦于视觉目标跟踪领域,深入研究了摄像机运动与被跟踪对象运动之间的因果关系,构建了相应的数学模型以描述二者间的动态耦合机制,并基于Matlab平台实现了完整的算法仿真与验证流程。研究融合控制系统理论(如LOS导引律、PID控制、反步法、李雅普诺夫稳定性分析等)与计算机视觉技术,通过仿真实验有效提升了复杂动态环境下目标跟踪的精度、鲁棒性与自适应能力。文中提供的Matlab代码涵盖了轨迹预测、控制器设计、状态反馈与系统仿真等关键环节,具有较强的可复现性和工程参考价值,适用于智能监控、自动驾驶、无人机导航与机器人感知等应用场景。; 适合人群:具备一定Matlab编程基础和控制理论知识,从事计算机视觉、自动控制、 robotics、智能系统或相关方向研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①深入理解视觉跟踪中摄像机与目标间的运动耦合与因果建模方法;②掌握基于现代控制理论(如非线性控制、自适应控制)的目标跟踪算法设计与Matlab仿真技术;③为开发高精度、强鲁棒性的自主系统提供算法原型与代码支撑; 阅读建议:建议结合文中的控制理论(如LOS、PID、反步法、李雅普诺夫函数设计)与配套Matlab代码进行同步调试与仿真,通过参数调优与案例复现加深对算法内在机理的理解,可进一步拓展至多目标跟踪、传感器融合或深度学习结合的视觉伺服系统研究。

国家级孵化器申报:核心材料清单与数智化转型路径.docx

国家级孵化器申报:核心材料清单与数智化转型路径

政府搭建创新联合体管理服务系统有哪些主流选型?.docx

政府搭建创新联合体管理服务系统有哪些主流选型?

技术转移机构拓展高校客户资源的高效市场推广策略.docx

科易网基于行业知识库+行业大数据+科技资源要素+AI模型,以AI,赋能技术转移、成果转化、技术经理人培训、知识产权管理、区域产业分析与布局、地方科技招商、创新创业大赛等场景,推出科技创新领域的AI工具、场景化服务和数智化解决方案,推动科技创新与产业创新智能化发展。

c++系统环境配置g++文件

c++系统环境配置g++文件

技术转移从业人员想要考取相关资质,目前国家认可的培训和认证渠道有哪些?.docx

科易网基于行业知识库+行业大数据+科技资源要素+AI模型,以AI,赋能技术转移、成果转化、技术经理人培训、知识产权管理、区域产业分析与布局、地方科技招商、创新创业大赛等场景,推出科技创新领域的AI工具、场景化服务和数智化解决方案,推动科技创新与产业创新智能化发展。

创新联合体数据大屏展示系统有哪些?.docx

创新联合体数据大屏展示系统有哪些?

智慧市政井盖物联网大数据平台建设综合解决方案.pptx

智慧市政井盖物联网大数据平台建设综合解决方案.pptx

水下无人船UUV三维轨迹跟踪、LOS制导+PID优化控制程序(Matlab代码实现)

内容概要:本文详细介绍了水下无人船(UUV)在三维空间中的轨迹跟踪控制方法,重点研究了视线制导(Line-of-Sight, LOS)与PID控制器相结合的优化控制策略,并提供了基于Matlab的完整仿真代码实现。文中系统阐述了UUV的三维动力学建模过程,深入分析了LOS制导律在横向与纵向平面内的投影机制及其参数设计原则,同时结合PID控制对速度与姿态进行精确调节,有效提升了轨迹跟踪的精度与系统鲁棒性。研究特别针对三维螺旋线轨迹这一复杂路径进行了仿真验证,展示了系统在不同海洋环境干扰下的稳定响应能力,涵盖位置、速度、角度等多个维度的动态性能表现。此外,还探讨了控制器参数整定方法及其对系统动态特性的影响。; 适合人群:具备自动控制理论基础和Matlab编程能力的研究生、科研人员以及从事水下机器人、海洋装备控制等相关领域的工程技术人员。; 使用场景及目标:①用于水下无人航行器三维路径规划与运动控制的教学演示与科研仿真;②为实际UUV控制系统开发提供可复用的算法原型与技术参考;③支持对LOS制导原理、PID参数优化及三维轨迹跟踪性能的深入研究与改进;④适用于复杂轨迹(如螺旋线)跟踪控制算法的验证与对比分析。; 阅读建议:建议结合Matlab代码与文中的理论描述逐模块调试与运行,重点关注LOS制导参数的选择与PID控制器的整定过程,可通过修改期望轨迹形状、引入外部扰动等方式测试系统的适应性、稳定性与抗干扰能力。

技术转移机构评估咨询费定价差异的行业逻辑与数智化路径.docx

科易网基于行业知识库+行业大数据+科技资源要素+AI模型,以AI,赋能技术转移、成果转化、技术经理人培训、知识产权管理、区域产业分析与布局、地方科技招商、创新创业大赛等场景,推出科技创新领域的AI工具、场景化服务和数智化解决方案,推动科技创新与产业创新智能化发展。

AUTOSAR 存储知识体系

AUTOSAR 存储知识体系

用于无速度传感器交流电机驱动的扩展卡尔曼滤波器EKF(Matlab代码、Simulink仿真实现)

内容概要:本文系统阐述了用于无速度传感器交流电机驱动的扩展卡尔曼滤波器(EKF)的设计与实现方法,结合Matlab编程与Simulink仿真平台,详细展示了EKF在电机状态估计中的关键技术环节。通过构建交流电机的非线性动态模型,利用EKF算法对转速、转子位置等关键状态变量进行高精度实时估计,从而替代传统机械传感器,实现高性能的无传感器矢量控制。资源包含完整的Matlab代码、Simulink仿真模型及详尽的说明文档,便于用户理解算法原理、复现实验结果,并支持进一步优化与拓展。该方法具有良好的鲁棒性和工程实用性,适用于对系统可靠性与成本控制要求较高的电机控制场合。; 适合人群:具备电机控制理论基础和Matlab/Simulink仿真能力的高校研究生、从事电气自动化与电力电子方向的科研人员,以及工业领域中从事电机驱动系统开发的工程技术人员。; 使用场景及目标:①开展无速度传感器交流电机控制系统的设计与仿真验证;②深入学习扩展卡尔曼滤波器在非线性动态系统状态估计中的应用机制;③支撑相关课题研究、学术论文复现或实际工程项目的技术攻关。; 阅读建议:建议读者结合提供的Matlab代码与Simulink模型同步运行,分模块调试并观察状态估计过程,以加深对EKF算法结构、参数整定及数值稳定性的理解,同时可对比其他观测器方法(如滑模观测器、龙伯格观测器等),提升综合分析与设计能力。

电子专业-微机原理仿真实验1-4.rar

电子专业-微机原理仿真实验1-4.rar

水下无人艇AUV轨迹跟踪增量式PID控制仿真【带参考文献】

水下无人艇AUV轨迹跟踪增量式PID控制仿真【带参考文献】内容概要:本文围绕水下无人艇(AUV)的轨迹跟踪问题,研究并实现了基于增量式PID控制的仿真方法。文中首先建立了AUV在水平面上的运动模型,并构建了参考坐标系下的路径跟踪误差方程;随后设计了基于反步法的路径跟踪控制器,通过构造趋近角与角度误差期望值,将路径参数作为虚拟控制量进行反步递推设计;最后结合Simulink进行仿真分析,验证了所提控制策略的有效性与闭环系统的稳定性。该研究为AUV高精度轨迹跟踪提供了理论支持和仿真实现方案。; 适合人群:具备自动控制、海洋工程或机器人相关背景,从事控制系统仿真与设计的研究生及科研人员。; 使用场景及目标:①学习AUV运动建模与非线性控制系统设计方法;②掌握反步法与增量式PID在轨迹跟踪中的应用;③开展水下机器人控制算法的仿真验证与性能优化研究。; 阅读建议:建议读者结合Matlab/Simulink环境动手实践,重点关注控制器设计过程中的稳定性分析与参数 tuning,并参考文中的参考文献拓展对先进控制策略的理解。

基于双卡尔曼滤波算法(DEKF)的电池内部温度(SOT)估计m脚本仿真

内容概要:本文围绕基于双卡尔曼滤波算法(DEKF)的电池内部温度(SOT)估计展开,重点介绍了通过改进的DEKF算法实现对锂离子电池内部热状态的高精度在线估计。研究结合电池等效电路模型与热模型,构建联合状态估计算法框架,利用Matlab实现完整的m脚本仿真,详细阐述了系统建模、状态方程与观测方程设计、噪声协方差设定、滤波递推过程及仿真结果分析等关键技术环节。该方法有效解决了电池内部温度难以直接测量的问题,提升了电池热管理系统的安全性与可靠性,适用于电动汽车、储能系统等对电池安全要求较高的应用场景。同时,文档附带丰富的科研资源链接,涵盖智能优化、电力系统、雷达跟踪等多个领域,便于读者拓展学习。; 适合人群:具备一定Matlab编程能力,从事新能源技术、电动汽车、电池管理系统(BMS)、状态估计或相关方向研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握双卡尔曼滤波在电池状态估计中的建模与实现方法;②开展电池热管理系统的仿真设计与优化研究;③拓展应用于电池SOC、SOH等其他状态参数的联合估计问题;④为动力电池的安全监控与优化调度提供技术支持。; 阅读建议:建议结合文中提供的Matlab代码逐模块调试运行,对照算法流程深入理解状态估计的每一步实现细节,同时参考所列文献以加强理论基础,推荐关注公众号“荔枝科研社”获取完整资源包以便于复现实验与进一步研究。

上一篇: 如何从61850中获益
下一篇: 云计算:构建未来电力系统的核心计算平台(论文)
GZFStudy
博客等级 码龄20年 42粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值