1

我是 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;
}
4

2 回答 2

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();
}
于 2013-06-10T18:17:10.447 回答
0

这是处理您的情况的简化版本。

您正在创建 2 个线程来调用 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);       
 }
于 2013-06-10T18:45:36.127 回答