我是Windows c ++编程的新手。请参阅下面的代码,我想让两个线程同步。第一个线程应该打印“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;
}
答案 0 :(得分:5)
问题是你问的问题真的没有意义。多个线程被设计为同时运行 并且您正在尝试从一个线程传递到另一个线程的游戏以获得顺序序列化行为。它就像采用一个非常复杂的工具,并询问它如何解决通常一个非常简单的问题。
但是,多线程是一个非常重要的学习主题,所以我会尽力回答你的需要。
首先,我建议使用新的标准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();
}
答案 1 :(得分:0)
以下是处理您情况的简化版本。
您正在创建2个线程来调用2个不同的函数。 理想情况下,线程同步用于在线程之间序列化相同的代码,但在您的情况下,它不是必需的。您正在尝试序列化两个彼此无关的线程。 任何你如何通过不进行异步调用等待每个线程完成。
#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);
}