1、使用操作系统自带的原生线程库
在C++中,信号量是一种用于线程同步的基本工具,用于控制对共享资源的访问。C++标准库并没有直接提供信号量,但是可以使用操作系统提供的信号量或者使用第三方库来实现。
在使用操作系统提供的信号量时,可以使用POSIX线程库(pthread)或Windows线程库来创建和管理信号量。下面是一个简单的使用POSIX线程库中信号量的示例:
/*
使用了POSIX线程库中的sem_init()、sem_wait()和sem_post()函数来初始化、等待和释放信号量。semaphore是一个全局的信号量,控制两个线程对临界区的访问
*/
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
sem_t semaphore;
void* thread_function(void* arg) {
// 等待信号量
sem_wait(&semaphore);
// 临界区代码
std::cout << "Thread is in critical section" << std::endl;
// 释放信号量
sem_post(&semaphore);
return NULL;
}
int main() {
// 初始化信号量,第二个参数表示信号量的共享性(0为进程内共享,非0为进程间共享),第三个参数表示信号量的初始值
sem_init(&semaphore, 0, 1);
pthread_t thread1, thread2;
// 创建线程
pthread_create(&thread1, NULL, thread_function, NULL);
pthread_create(&thread2, NULL, thread_function, NULL);
// 等待线程结束
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
// 销毁信号量
sem_destroy(&semaphore);
return 0;
}
2、使用C++标准库
C++标准库在C++11之后提供了std::mutex(互斥锁)和std::condition_variable(条件变量)等线程同步工具,这些工具通常更加方便和安全,推荐在C++中使用。
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex mtx;
std::condition_variable cv;
bool ready = false;
void thread_function(int thread_id) {
std::unique_lock<std::mutex> lock(mtx);
// 等待条件变量满足
cv.wait(lock, []{ return ready; });
// 临界区代码
std::cout << "Thread " << thread_id << " is in critical section" << std::endl;
}
int main() {
std::thread threads[2];
// 创建两个线程
for (int i = 0; i < 2; ++i) {
threads[i] = std::thread(thread_function, i+1);
}
// 做一些其他的工作
// 通知线程条件已满足
{
std::lock_guard<std::mutex> lock(mtx);
ready = true;
}
cv.notify_all();
// 等待线程结束
for (auto& th : threads) {
th.join();
}
return 0;
}
在这个源码中,std::mutex用于保护共享资源,std::condition_variable用于在条件满足时唤醒等待的线程。ready变量表示条件是否满足,cv.wait()用于阻塞线程直到条件满足,并在条件满足时自动解锁互斥量。cv.notify_all()用于通知所有等待的线程条件已满足。
这种方法使用了更加现代化的C++线程库,避免了直接操作操作系统提供的原生线程库,更加安全和方便。