同步两个线程,在彼此之间传递事件
Synchronise two threads passing events between each other
我是windowsc++编程新手。请参阅下面的代码,我想让两个线程同步。第一个线程应该打印"Hello",然后将控件/事件传递给第二个线程。不知道该怎么做。到目前为止,我正在使用Sleep(1000)。但是如果我不使用Sleep,它会导致未定义的行为。请帮助…
#include <windows.h>
#include <process.h>
#include <iostream>
void thread1(void*);
void thread2(void*);
int main(int argc, char **argv) {
_beginthread(&thread1,0,(void*)0);
_beginthread(&thread2,0,(void*)0);
Sleep(1000);
}
void thread1(void*)
{
std::cout<<"Hello "<<std::endl;
}
void thread2(void*)
{
std::cout<<"World"<<std::endl;
}
问题是你问的问题真的没有意义。多个线程被设计为同时运行,并且您正在尝试玩将责任从一个线程传递到另一个线程以获得顺序序列化行为的游戏。这就像用一个非常复杂的工具,问它如何解决一个通常非常简单的问题。
然而,多线程是一个非常重要的话题,所以我会尽我所能回答你的问题。
首先,我建议使用新的、标准的c++ 11函数和库。对于windows,您可以下载Visual Studio 2012 Express Edition来试用。
有了这个,你可以使用std::thread, std::mutex和许多[但不是全部]其他c++ 11的好东西(如std::condition_variable)。
要解决您的问题,您确实需要一个条件变量。这让你可以向另一个线程发出信号,告诉它们已经准备好了:
#include <iostream>
#include <mutex>
#include <atomic>
#include <condition_variable>
#include <thread>
static std::atomic<bool> ready;
static std::mutex lock;
static std::condition_variable cv;
// ThreadOne immediately prints Hello then 'notifies' the condition variable
void ThreadOne()
{
std::cout << "Hello ";
ready = true;
cv.notify_one();
}
// ThreadTwo waits for someone to 'notify' the condition variable then prints 'World'
// Note: The 'cv.wait' must be in a loop as spurious wake-ups for condition_variables are allowed
void ThreadTwo()
{
while(true)
{
std::unique_lock<std::mutex> stackLock(lock);
cv.wait(stackLock);
if(ready) break;
}
std::cout << "World!" << std::endl;
}
// Main just kicks off two 'std::thread's. We must wait for both those threads
// to finish before we can return from main. 'join' does this - its the std
// equivalent of calling 'WaitForSingleObject' on the thread handle. its necessary
// to call join as the standard says so - but the underlying reason is that
// when main returns global destructors will start running. If your thread is also
// running at this critical time then it will possibly access global objects which
// are destructing or have destructed which is *bad*
int main(int argc, char **argv)
{
std::thread t1([](){ThreadOne();});
std::thread t2([](){ThreadTwo();});
t1.join();
t2.join();
}
下面是处理您的情况的简化版本。
你正在创建两个线程来调用两个不同的函数。理想情况下,线程同步用于在线程之间序列化相同的代码,但在您的情况下并不需要。您正在尝试序列化两个线程,这两个线程彼此之间没有任何关联。你可以通过不进行异步调用来等待每个线程完成。
#include <windows.h>
#include <process.h>
#include <iostream>
#include<mutex>
using namespace std;
void thread1(void*);
void thread2(void*);
int main(int argc, char **argv) {
HANDLE h1 = (HANDLE)_beginthread(&thread1,0,(void*)0);
WaitForSingleObject(h1,INFINITE);
HANDLE h2 = (HANDLE)_beginthread(&thread2,0,(void*)0);
WaitForSingleObject(h2,INFINITE);
}
void thread1(void*)
{
std::cout<<"Hello "<<std::endl;
}
void thread2(void*)
{
std::cout<<"World"<<std::endl;
}
你可以把beginthread放在一个函数中,如果你想打印多次,可以在while循环中调用这个函数。
void fun()
{
HANDLE h1 = (HANDLE)_beginthread(&thread1,0,(void*)0);
WaitForSingleObject(h1,INFINITE);
HANDLE h2 = (HANDLE)_beginthread(&thread2,0,(void*)0);
WaitForSingleObject(h2,INFINITE);
}
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- Android NDK传感器向事件队列报告奇怪的间隔
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- 查找彼此之间具有不同最小距离的随机数
- Qt:工作线程和 GUI 事件之间的关系
- 基于两个程序之间的事件触发器访问数据(内存)
- 复制n char*彼此之间
- 服务和用户模式进程之间的共享全局事件不起作用
- 窗口之间的事件
- Qt 中两个线程之间的事件同步
- 两个事件之间的计时持续时间
- PHP和C++可以在彼此之间传递数据吗
- 比较 c++ std::vector 的元素彼此之间
- 管理Win32应用程序和QWinMigrate之间的键盘事件
- 同步两个线程,在彼此之间传递事件
- 不同的C/ c++模块在编译和链接时彼此之间有什么通信?
- c++和Objective C之间的事件处理