Sleep和WaitForSingleObject区别

一、Msdn种Sleep解读

This function suspends the execution of thecurrent thread for a specified interval.

终止线程指定的时间间隔。

void Sleep(

DWORD dwMilliseconds);

Parameters

dwMilliseconds

Specifies the time, in milliseconds, forwhich to suspend execution. A value of zero causes the thread to relinquish theremainder of its time slice to any other thread of equal priority that is readyto run. If there are no other threads of equal priority ready to run, thefunction returns immediately, and the thread continues execution. A value ofINFINITE causes an infinite delay.

Suspend执行指定的毫秒,0导致线程放弃它的剩余时间片转向执行其他同等优先级准备执行的线程。如果没有同等优先级的其他线程在运行,函数立即返回,并且当前线程继续执行。INFINIT导致其无限延时。

VOID Sleep(DWORDdwMilliseconds); 该函数可使线程暂停自己的运行,直到dwMilliseconds过去为止。

关于Sleep函数,有下面几个重要问题值得注意:

* 调用Sleep,可使线程自愿放弃它剩余的时间片。

* 系统将在大约的指定毫秒数内使线程不可调度

* 你可以调用Sleep,并且为dwMilliseconds参数传递INFINITE。这将告诉系统永远不要调度该线程。这不是一件值得去做的事情。最好是让线程退出,并还原它的堆栈和内核对象。

* 你可以将0传递给Sleep。这将告诉系统,调用线程将释放剩余的时间片,并迫使系统调度另一个线程。但是,系统可以对刚刚调用Sleep的线程重新调度。如果不存在多个拥有相同优先级的可调度线程,就会出现这种情况。

二、Sleep和WaitForSingleObject区别

Sleep的话,只是睡一段时间,时间一到,就变成可调度状态。而WaitForSingleObject则是等待某个内核对象变成有信号状态,其目的和意义很明显,因此更具有针对性。我个人的习惯,在主线程中不用WaitForSingleObject,因为它容易引起死锁

WaitForSingleObject

Sleep

等待信号返回,

等待一定的时间

灵活;等待信号的时间内,信号也可能提前返回

需要强制等待固定的时间

总结:如果在工作线程中有可能涉及到了消息驱动的API,那么不能在主线程中使用WaitForSingleObject一类函数,而必须使用上述的方案。[尚未遇到此类情况].

但[MSDN]上有详细介绍:

Ifa thread creates any windows, it must process messages. Message broadcasts aresent to all windows in the system. A thread that uses a wait function with notime-out interval may cause the system to become deadlocked. Two examples ofcode that indirectly creates windows are DDE and COM CoInitialize.Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjectsor MsgWaitForMultipleObjectsEx,rather than WaitForSingleObject.

如果线程创建了窗口,他必须处理消息。消息广播发送至系统中的所有窗口。一个使用没有时延间隔的等待函数可能导致系统死锁。在DDE和COM初始化的时候间接地创建了窗口。因此,如果通过线程创建了窗口,应该使用MsgWaitForMultipleObjectsor MsgWaitForMultipleObjectsEx,而不是WaitForSingleObject.。

借鉴了论坛里的一些回复,谢谢!

在Windows中,`WaitForSingleObject` 是一个用于线程同步的函数,它允许一个线程等待某个同步对象(如互斥量、信号量、事件等)变为有信号状态或超时。在Linux系统中,并没有直接与 `WaitForSingleObject` 对应的接口,但可以通过不同的机制来实现类似的功能。 ### 1. **使用 `pthread_cond_wait` 条件变量** 在POSIX线程(pthreads)中,可以使用条件变量(`pthread_cond_t`)配合互斥锁(`pthread_mutex_t`)来实现类似于 `WaitForSingleObject` 的功能。条件变量允许一个或多个线程等待某个条件发生,并在该条件发生变化时被唤醒。 ```c #include <pthread.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; int flag = 0; void* thread_func(void* arg) { pthread_mutex_lock(&mutex); while (!flag) { pthread_cond_wait(&cond, &mutex); // 等待条件满足 } // 条件满足后的处理逻辑 pthread_mutex_unlock(&mutex); return NULL; } int main() { pthread_t thread; pthread_create(&thread, NULL, thread_func, NULL); // 模拟一些工作后设置条件 sleep(2); pthread_mutex_lock(&mutex); flag = 1; pthread_cond_signal(&cond); // 唤醒等待的线程 pthread_mutex_unlock(&mutex); pthread_join(thread, NULL); return 0; } ``` ### 2. **使用 `sem_wait` 信号量** 如果需要实现类似于 `WaitForSingleObject` 在信号量上的行为,可以使用POSIX信号量(`sem_t`)。`sem_wait` 函数会阻塞调用线程,直到信号量的值大于零,并将其减一。 ```c #include <semaphore.h> #include <pthread.h> sem_t sem; void* thread_func(void* arg) { sem_wait(&sem); // 等待信号量 // 处理逻辑 return NULL; } int main() { sem_init(&sem, 0, 0); // 初始化信号量为0 pthread_t thread; pthread_create(&thread, NULL, thread_func, NULL); // 模拟一些工作后释放信号量 sleep(2); sem_post(&sem); // 增加信号量的值 pthread_join(thread, NULL); sem_destroy(&sem); return 0; } ``` ### 3. **使用 `poll` 或 `select` 实现定时等待** 如果你希望实现带有超时的等待机制,可以使用 `poll` 或 `select` 函数,它们通常用于I/O多路复用,但也适用于简单的定时等待场景。虽然这些函数主要用于文件描述符的监控,但可以通过传递空的文件描述符数组并指定超时时间来模拟 `WaitForSingleObject` 的超时行为。 ```c #include <poll.h> int main() { struct pollfd fds[0]; // 不需要监控任何文件描述符 int timeout_ms = 5000; // 超时时间为5秒 int ret = poll(fds, 0, timeout_ms); // 等待指定的时间 if (ret == 0) { // 超时处理逻辑 } else if (ret < 0) { // 错误处理逻辑 } return 0; } ``` ### 4. **使用 `std::condition_variable`(C++11及以上)** 在现代C++中,可以使用标准库中的 `std::condition_variable` `std::mutex` 来实现线程间的同步。这种方式更加高级且易于管理,适合C++开发者。 ```cpp #include <iostream> #include <thread> #include <mutex> #include <condition_variable> std::mutex mtx; std::condition_variable cv; bool ready = false; void wait_for_flag() { std::unique_lock<std::mutex> lock(mtx); cv.wait(lock, []{ return ready; }); // 等待条件满足 std::cout << "Condition met!" << std::endl; } int main() { std::thread t(wait_for_flag); // 模拟一些工作后设置条件 std::this_thread::sleep_for(std::chrono::seconds(2)); { std::lock_guard<std::mutex> lock(mtx); ready = true; } cv.notify_one(); // 唤醒等待的线程 t.join(); return 0; } ``` 以上几种方法都可以根据具体的应用场景选择使用,以实现与Windows中 `WaitForSingleObject` 类似的功能。每种方法都有其适用的场合,例如条件变量适用于复杂的同步需求,而信号量则更适合于资源计数简单的同步操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值