方法1:
pthread
库
https://subingwen.cn/linux/thread-sync/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>
#define MAX 100
// 全局变量
int number;
// 创建一把互斥锁
// 全局变量, 多个线程共享
pthread_mutex_t mutex;
// 线程处理函数
void* funcA_num(void* arg)
{
for(int i=0; i<MAX; ++i)
{
// 如果线程A加锁成功, 不阻塞
// 如果B加锁成功, 线程A阻塞
pthread_mutex_lock(&mutex);
int cur = number;
cur++;
usleep(10);
number = cur;
pthread_mutex_unlock(&mutex);
printf("Thread A, id = %lu, number = %d\n", pthread_self(), number);
}
return NULL;
}
void* funcB_num(void* arg)
{
for(int i=0; i<MAX; ++i)
{
// a加锁成功, b线程访问这把锁的时候是锁定的
// 线程B先阻塞, a线程解锁之后阻塞解除
// 线程B加锁成功了
pthread_mutex_lock(&mutex);
int cur = number;
cur++;
number = cur;
pthread_mutex_unlock(&mutex);
printf("Thread B, id = %lu, number = %d\n", pthread_self(), number);
usleep(5);
}
return NULL;
}
int main(int argc, const char* argv[])
{
pthread_t p1, p2;
// 初始化互斥锁
pthread_mutex_init(&mutex, NULL);
// 创建两个子线程
pthread_create(&p1, NULL, funcA_num, NULL);
pthread_create(&p2, NULL, funcB_num, NULL);
// 阻塞,资源回收
pthread_join(p1, NULL);
pthread_join(p2, NULL);
// 销毁互斥锁
// 线程销毁之后, 再去释放互斥锁
pthread_mutex_destroy(&mutex);
return 0;
}
作者: 苏丙榅
链接: https://subingwen.cn/linux/thread-sync/#2-1-%E4%BA%92%E6%96%A5%E9%94%81%E4%BD%BF%E7%94%A8
来源: 爱编程的大丙
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
方法2:
使用多线程交替打印1-10,线程1打印奇数,线程2打印偶数
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int number;
mutex mutex_number;
const int MAXNUM = 10;
// 打印奇数
void add_1() {
while (1) {
mutex_number.lock();
if (number > MAXNUM) {
mutex_number.unlock();
break;
}
if (number % 2 == 1) {
cout << "mythread_1: " << number++ << endl; // 输出
}
mutex_number.unlock();
}
cout << "mythread_1 finish" << endl; // mythread_1完成
}
// 打印偶数
void add_2() {
while (1) {
mutex_number.lock();
if (number > MAXNUM) {
mutex_number.unlock();
break;
}
if (number % 2 == 0) {
cout << "mythread_2: " << number++ << endl; // 输出
}
mutex_number.unlock();
}
cout << "mythread_2 finish" << endl; // mythread_2完成
}
int main() {
number = 1;
cout << endl << "Create and Start!" << endl;
thread mythread_1(add_1);
thread mythread_2(add_2);
mythread_1.join();
mythread_2.join();
cout << endl << "Finish and Exit!" << endl;
system("pause");
return 0;
}