WaitForSingleObject()在Linux,vxWorks下的等价函数

本文介绍了一个简单的跨平台程序示例,使用Linux下的semaphore来实现类似Windows中的WaitForSingleObject和SetEvent的功能。通过主线程与子线程之间的信号量传递,确保数据的正确同步。

Windows中的WaitForSingleObject()函数对应在Linux中的sem_wait(),SetEvent对应sem_post(),
Windows中的WaitForSingleObject()函数对应在vxworks中semTake(),SetEvent对应semGive().

参考下面的Linux程序:

 

char tem[10]; //读写公共区
sem_t sem;
void* thread_fun(void*);
int main()
{
int counter=0;
pthread_t mythread;
sem_init(&sem,0,0);
pthread_create(&mythread,NULL,thread_fun,NULL);
while(counter<10) //往读写区里写10次'f'
{
tem[counter]='f';
counter++;
sem_post(&sem);
}
pthread_join(mythread,NULL); //等待子线程
sem_destroy(&sem);
exit(0);
}
void* thread_fun(void* arg) //子线程函数
{
int counter=0;
while(counter<10&&sem_wait(&sem)==0)
{
printf("%c",tem[counter]); //读出来显示
counter++;
//sem_wait(&sem);
}
pthread_exit(NULL);
}


 

在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、付费专栏及课程。

余额充值