条件变量--pthread_cond_t(小程序)

条件变量,利用线程共享全局数据机制来进行同步,两种操作,一种是等待条件成立而挂起,另一种是给出条件成立的信号。
(1)int pthread_cond_t mycond; 定义一个条件变量
         两种初始化方式,第一种使用常量PTHREAD_COND_INITIALIZER,
         第二种使用int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr)
(2)int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex),等待条件cond成立,线程阻塞,解开mutex。当条件成立后,对mutex加锁,然后回到线程运行中。
(3)int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)    在有限的时间内等待,超过时间以后返回ETIMEOUT
(4)int pthread_cond_signal(pthread_cond_t *cond),发送成功信号,激活一个等待该信号的线程。
(5)int pthread_cond_broadcast(pthread_cond_t *cond),发送成功信号,激活所有等待该信号的线程。
互斥锁贯穿在条件变量中间。
计算完美数,如果一个数的所有小于自身的因子的和仍然等于自身,如果6 = 1 + 2 + 3,这样的数叫做完美数,用多线程完成这个操作,并让结果输出到文件中。主线程完成计算,thread完成输出到文件中,用双重vector存贮,如果vector不为空,就向文件中输出。使用条件变量和互斥锁完成。

#include <iostream>
#include <fstream>
#include <pthread.h>
#include <vector>
#include <time.h>
using namespace std;
#define MAXN 100000
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
vector<vector<int> > vec;
void deal(void *arg) {
	vec.clear();
	//vector<vector<int> >().swap(vec);
	pthread_mutex_unlock(&mutex);
	return ;
}
void *print(void *arg) {
	pthread_cleanup_push(deal, NULL);
	pthread_mutex_lock(&mutex);
	ofstream fcout("./text");
	while( true ) {
		while( !vec.size() ) {
			pthread_cond_wait(&cond, &mutex);
		}
		auto vec_iter = vec.end()-1;
		for(auto iter = vec_iter->begin(); iter < vec_iter->end(); iter++) {
			if( iter == vec_iter->end()-2 ) fcout << *iter << "=";
			else if( iter == vec_iter->end()-1 ) fcout << *iter << "\n";
			else fcout << *iter << "+";
			for(int i = 1; i <= 1000000; i++) fcout << i;		
		}
		vec.erase(vec_iter);
		//vector<int>().swap(*vec_iter);
	}
	pthread_mutex_unlock(&mutex);
	pthread_cleanup_pop(0);
	return NULL;
}
int main() {
	time_t start = clock();
	pthread_t pthread;
	auto state = pthread_create(&pthread, NULL, print, NULL);
	if( state ) {
		cout << "thread error" << endl;
	}
	vec.clear();
	//vector<vector<int> >().swap(vec);
	for(int i = 1; i <= MAXN; i++) {
		pthread_mutex_lock(&mutex);
		int sum = 0;
		vec.push_back( vector<int>() );
		for(int j = 1; j < i; j++) {
			if( i%j == 0 ) {
				sum += j;
				auto iter = vec.end()-1;
				iter->push_back(j);
			}
		}
		auto iter = vec.end()-1;
		iter->push_back(i);
		if( sum == i  ) {
			pthread_cond_signal(&cond);
		}
		else {
			vec.erase(iter);
			//vector<int>().swap( *iter );
		}
		pthread_mutex_unlock(&mutex);
	}
	pthread_cancel(pthread);
	pthread_join(pthread, NULL);
	time_t end = clock();
	cout << end-start << endl;
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分析下面代码的每一步功能:#include <stdio.h> #include <pthread.h> #define QUEUE_SIZE 20 #define THREAD_NUM 10 #define MAX_NUM 30000200 #define MIN_NUM 30000000 int queue[QUEUE_SIZE]; int front = 0; int rear = 0; int finished = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; int is_prime(int num) { int i; if (num <= 1) { return 0; } for (i = 2; i * i <= num; i++) { if (num % i == 0) { return 0; } } return 1; } // 子线程函数 void *thread_func(void arg) { int thread_num = (int)arg; while (1) { pthread_mutex_lock(&mutex); while (front == rear && finished == 0) { pthread_cond_wait(&cond, &mutex); } if (front == rear && finished == 1) { pthread_mutex_unlock(&mutex); break; } int num = queue[front++]; if (front == QUEUE_SIZE) { front = 0; } pthread_mutex_unlock(&mutex); if (is_prime(num)) { printf("Thread %d: %d\n", thread_num, num); } } pthread_exit(NULL); } int main() { int i, j; pthread_t tids[THREAD_NUM]; int thread_num[THREAD_NUM]; for (i = 0; i < THREAD_NUM; i++) { thread_num[i] = i; pthread_create(&tids[i], NULL, thread_func, (void)&thread_num[i]); } for (i = MIN_NUM; i <= MAX_NUM; ) { pthread_mutex_lock(&mutex); if ((rear + 1) % QUEUE_SIZE == front) { pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); continue; } queue[rear++] = i++; if (rear == QUEUE_SIZE) { rear = 0; } pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); } pthread_mutex_lock(&mutex); finished = 1; pthread_cond_broadcast(&cond); pthread_mutex_unlock(&mutex); for (i = 0; i < THREAD_NUM; i++) { pthread_join(tids[i], NULL); } return 0; }
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值