<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;">使线程同步</span><br style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;" /><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;"> 在程序中使用多线程时,一般很少有多个线程能在其生命期内进行完全独立的操作。更多的情况是一些线程进行某些处理操作,而其他的线程必须对其处理结果进行了解。正常情况下对这种处理结果的了解应当在其处理任务完成后进行。</span><br style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;" /><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;"> 如果不采取适当的措施,其他线程往往会在线程处理任务结束前就去访问处理结果,这就很有可能得到有关处理结果的错误了解。例如,多个线程同时访问同一个全局变量,</span><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;">如果都是读取操作,则不会出现问题</span><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;">。如果一个线程负责改变此变量的值,而其他线程负责同时读取变量内容,则不能保证读取到的数据是经过写线程修改后的。</span><br style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;" /><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;"> 为了确保读线程读取到的是经过修改的变量,</span><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;">就必须在向变量写入数据时禁止其他线程对其的任何访问</span><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;">,</span><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;">直至赋值过程结束后再解除对其他线程的访问限制</span><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;">。象这种保证线程能了解其他线程任务处理结束后的处理结果而采取的保护措施即为线程同步。</span>
#include <iostream> #include <Windows.h> using namespace std; HANDLE hSemaphnore1; HANDLE hSemaphnore2; int global_variable =0; void Threadfun1(LPVOID pParam) { WaitForSingleObject(hSemaphnore1,INFINITE); while(1) { global_variable++; printf("%d\n",global_variable); Sleep(100); //睡眠下,让其他线程有机会抢占 if(global_variable ==10) { ReleaseSemaphore(hSemaphnore2, 1, NULL); //通知线程2 WaitForSingleObject(hSemaphnore1,INFINITE); //让线程1等待 } } } void Threadfun2(LPVOID nParam) { WaitForSingleObject(hSemaphnore2,INFINITE); while(1) { global_variable--; printf("%d\n",global_variable); Sleep(100); if(global_variable ==0) { ReleaseSemaphore(hSemaphnore1, 1, NULL); //通知线程1 WaitForSingleObject(hSemaphnore2,INFINITE); } } } int main() { DWORD threadLidar_receive,threadlidar_process; hSemaphnore1 = CreateSemaphore(NULL,1,1,NULL); hSemaphnore2 = CreateSemaphore(NULL,0,1,NULL); CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Threadfun1, NULL, 0, &threadLidar_receive); CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Threadfun2,NULL, 0, &threadlidar_process); Sleep(100); system("pause"); return 0; }
时间: 2024-10-08 20:27:15