
<span style="font-family: Arial, Helvetica, sans-serif;">事件对象也可以通过通知操作的方式来保持线程的同步。并且可以实现不同进程中的线程同步操作。 </span>
事件对象包含的几个操作原语:
CreateEvent() 创建一个信号量
OpenEvent() 打开一个事件
SetEvent() 回置事件
WaitForSingleObject() 等待一个事件

</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">#include <windows.h>
#include <stdio.h>
#define THREADCOUNT 4
HANDLE ghWriteEvent;
HANDLE ghThreads[THREADCOUNT];
DWORD WINAPI ThreadProc(LPVOID);
void CreateEventsAndThreads(void)
{
int i;
DWORD dwThreadID;
// Create a manual-reset event object. The write thread sets this
// object to the signaled state when it finishes writing to a
// shared buffer.
ghWriteEvent = CreateEvent(
NULL, // default security attributes
TRUE, // manual-reset event
FALSE, // initial state is nonsignaled
TEXT("WriteEvent") // object name
);
if (ghWriteEvent == NULL)
{
printf("CreateEvent failed (%d)\n", GetLastError());
return;
}
// Create multiple threads to read from the buffer.
for(i = 0; i < THREADCOUNT; i++)
{
// TODO: More complex scenarios may require use of a parameter
// to the thread procedure, such as an event per thread to
// be used for synchronization.
ghThreads[i] = CreateThread(
NULL, // default security
0, // default stack size
ThreadProc, // name of the thread function
NULL, // no thread parameters
0, // default startup flags
&dwThreadID);
if (ghThreads[i] == NULL)
{
printf("CreateThread failed (%d)\n", GetLastError());
return;
}
}
}
void WriteToBuffer(VOID)
{
// TODO: Write to the shared buffer.
printf("Main thread writing to the shared buffer...\n");
// Set ghWriteEvent to signaled
if (! SetEvent(ghWriteEvent) )
{
printf("SetEvent failed (%d)\n", GetLastError());
return;
}
}
void CloseEvents()
{
// Close all event handles (currently, only one global handle).
CloseHandle(ghWriteEvent);
}
int main( void )
{
DWORD dwWaitResult;
// TODO: Create the shared buffer
// Create events and THREADCOUNT threads to read from the buffer
CreateEventsAndThreads();
// At this point, the reader threads have started and are most
// likely waiting for the global event to be signaled. However,
// it is safe to write to the buffer because the event is a
// manual-reset event.
WriteToBuffer();
printf("Main thread waiting for threads to exit...\n");
// The handle for each thread is signaled when the thread is
// terminated.
dwWaitResult = WaitForMultipleObjects(
THREADCOUNT, // number of handles in array
ghThreads, // array of thread handles
TRUE, // wait until all are signaled
INFINITE);
switch (dwWaitResult)
{
// All thread objects were signaled
case WAIT_OBJECT_0:
printf("All threads ended, cleaning up for application exit...\n");
break;
// An error occurred
default:
printf("WaitForMultipleObjects failed (%d)\n", GetLastError());
return 1;
}
// Close the events to clean up
CloseEvents();
system("pause");
return 0;
}
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
// lpParam not used in this example.
UNREFERENCED_PARAMETER(lpParam);
DWORD dwWaitResult;
printf("Thread %d waiting for write event...\n", GetCurrentThreadId());
dwWaitResult = WaitForSingleObject(
ghWriteEvent, // event handle
INFINITE); // indefinite wait
switch (dwWaitResult)
{
// Event object was signaled
case WAIT_OBJECT_0:
//
// TODO: Read from the shared buffer
//
printf("Thread %d reading from buffer\n",
GetCurrentThreadId());
break;
// An error occurred
default:
printf("Wait error (%d)\n", GetLastError());
return 0;
}
// Now that we are done reading the buffer, we could use another
// event to signal that this thread is no longer reading. This
// example simply uses the thread handle for synchronization (the
// handle is signaled when the thread terminates.)
printf("Thread %d exiting\n", GetCurrentThreadId());
return 1;
}

C ++ 11多线程同步互斥锁锁定条件变量
读数数6924

在多线程程序中多线程mutex,线程同步(多个线程访问资源保证序列)是一个非常重要的问题. Linux下常见的线程同步方法如下: 互斥量条件变量信号灯该博客仅介绍互斥量和条件变量的用法. 互斥锁和条件变量通常情况下,互斥锁和条件变量一起使用. 互斥锁用于短期锁定,主要是为了确保线程进入关键部分. 条件变量用于线程的长期等待,等待时将释放锁. 操作API如下(最常用的介绍): std :: m
博客文章来自: yangbodong22011
C ++多线程: 互斥锁
读数数3293
目录1.前言2.互斥锁2.1互斥锁的功能2.2互斥锁的使用2.2std :: lock_guard 3.死锁3.1死锁的含义3.2死锁的示例3.3死锁的解决方案1.前言例如,现在,我们使用列表容器来模仿消息队列. 消息到来时,它会暂时插入列表的尾部. 读取消息后,将读出头消息并删除消息. 在代码中,使用两个线程来实现消息编写...
博客文章来自: qq_28114615

C ++多线程互斥锁作战
读数数336
互斥和同步的基本功能. 互斥锁的多线程累积功能. pthread_mutex_init()互斥锁的初始化. pthread_mutex_lock()锁定互斥锁. 然后阻塞,直到可用pthread_mutex_trylock ...
博客帖子来自chengqiuming
C ++多线程并发(2)---互斥锁,用于线程同步
读数数2436

什么是线程同步?在上一篇文章“ C ++多线程并发编程(1)-线程管理”中,当解释多线程并发时,提到了两个重要概念: 多线程并发: 在同一时间段内彼此交替处理多个操作,线程切换时间片时间非常短(通常为毫秒级),在大多数情况下,时间片为时已晚,无法处理对资源的访问;线程间通信: 将一个任务分为多个并发线程进行处理时多线程mutex,多个线程可能必须处理共享内存中的数据,并且多个线程对同一共享内存数据的访问需要准确且有序. 如果像上一篇文章一样...
博客发自: m0_37621078
了解c ++多线程编程
读数数3402
多线程编程人员必须首先使用其CLion工具安装mingw并将其配置为支持c ++ 11多线程编程. 该博客文章主要不介绍互斥锁等,而是了解线程的执行,以便对未来充满信心地编写多线程程序. #include&amp; amp; amp; lt;线程和放大器; amp; amp; gt; #include&amp; amp; amp; lt; iostream和放大器; amp; amp; gt; #include&amp; amp; amp; lt;互斥锁amp; amp; gt;
博客帖子来自: liunan199481
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-181049-1.html
一来在世界舆论面前证明自己仍是世界老大
这种被动的方式有其客观背景
我们不针对美国