标签:itoa 信号 tps 创建 线程池 produce card 允许 closed
#include <stdio.h> #include <pthread.h> #include <windows.h> #define N 100 #define true 1 #define producerNum 10 #define consumerNum 5 #define sleepTime 1000 typedef int semaphore; typedef int item; item buffer[N] = {0}; int in = 0; int out = 0; int proCount = 0; semaphore mutex = 1, empty = N, full = 0, proCmutex = 1; void * producer(void * a){ while(true){ while(proCmutex <= 0); proCmutex--; proCount++; printf("生产一个产品ID%d, 缓冲区位置为%d\n",proCount,in); proCmutex++; while(empty <= 0){ printf("缓冲区已满!\n"); } empty--; while(mutex <= 0); mutex--; buffer[in] = proCount; in = (in + 1) % N; mutex++; full++; Sleep(sleepTime); } } void * consumer(void *b){ while(true){ while(full <= 0){ printf("缓冲区为空!\n"); } full--; while(mutex <= 0); mutex--; int nextc = buffer[out]; buffer[out] = 0;//消费完将缓冲区设置为0 out = (out + 1) % N; mutex++; empty++; printf("\t\t\t\t消费一个产品ID%d,缓冲区位置为%d\n", nextc,out); Sleep(sleepTime); } } int main() { pthread_t threadPool[producerNum+consumerNum]; int i; for(i = 0; i < producerNum; i++){ pthread_t temp; if(pthread_create(&temp, NULL, producer, NULL) == -1){ printf("ERROR, fail to create producer%d\n", i); exit(1); } threadPool[i] = temp; }//创建生产者进程放入线程池 for(i = 0; i < consumerNum; i++){ pthread_t temp; if(pthread_create(&temp, NULL, consumer, NULL) == -1){ printf("ERROR, fail to create consumer%d\n", i); exit(1); } threadPool[i+producerNum] = temp; }//创建消费者进程放入线程池 void * result; for(i = 0; i < producerNum+consumerNum; i++){ if(pthread_join(threadPool[i], &result) == -1){ printf("fail to recollect\n"); exit(1); } }//运行线程池 return 0; }
2
#include<stdio.h> #include<stdlib.h> #include<semaphore.h> #include<pthread.h> #include<unistd.h> #define NumOf_Producer 5 //the max num of producer #define NumOf_Consumer 10 //the max num of consumer #define Maxnum 10 // the max num of product sem_t Empty_sem; //the goal of whether the num of product is null sem_t Full_sem; //the goal of whether the num of product is equal to Maxnum pthread_mutex_t Mutex; //the goal of whether someone use the buff int Producer_id = 0; int Consumer_id = 0; int NowNumOfProduce = 0; void *Producer(void *arg) //the thread of producer { int id = Producer_id++; while(1) { sleep(0.1); sem_wait(&Full_sem); //when it comes to zero ,it means that the num of product is equal to Maxnum pthread_mutex_lock(&Mutex); //lock the buff NowNumOfProduce++; printf("Producerthread %d product one,the num is:%d \n",id%NumOf_Producer,NowNumOfProduce); pthread_mutex_unlock(&Mutex); sem_post(&Empty_sem); //when it comes to ten ,it means there are ten products can be used by consumer } return ((void *)0); } void *Consumer(void *arg) { int id = Consumer_id++; while(1) { sleep(0.2); sem_wait(&Empty_sem); pthread_mutex_lock(&Mutex); NowNumOfProduce--; printf("Consumerthread %d use product one,the num is:%d \n",id%NumOf_Consumer,NowNumOfProduce); pthread_mutex_unlock(&Mutex); sem_post(&Full_sem); } return ((void *)0); } int main() { pthread_t Con[NumOf_Consumer]; pthread_t Pro[NumOf_Producer]; int temp1 = sem_init(&Empty_sem,0,0); int temp2 = sem_init(&Full_sem,0,Maxnum); if(temp1&&temp2!=0) { printf("sem init failed \n"); exit(1); } int temp3 = pthread_mutex_init(&Mutex,NULL); if(temp3!=0) { printf("Mutex init failed \n"); exit(1); } for(int i=0 ;i<NumOf_Producer;i++) { int temp4 = pthread_create(&Pro[i],NULL,Producer,(void *)&i); if(temp4!=0) { printf("thread create failed !\n"); exit(1); } } for(int i=0;i<NumOf_Consumer;i++) { int temp5 = pthread_create(&Con[i],NULL,Consumer,(void *)&i); if(temp5!=0) { printf("thread create failed !\n"); } exit(1); } //destroy the thread for(int i=0;i<NumOf_Consumer;i++) { pthread_join(Con[i],NULL); } for(int i=0;i<NumOf_Producer;i++) { pthread_join(Pro[i],NULL); } return 0; }
3
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<semaphore.h> #include<pthread.h> #define msleep(x) usleep(x*1000) #define PRODUCT_SPEED 3 //生产速度 #define CONSUM_SPEED 1 //消费速度 #define INIT_NUM 3 //仓库原有产品数 #define TOTAL_NUM 10 //仓库容量 sem_t p_sem, c_sem, sh_sem; int num=INIT_NUM; void product(void) //生产产品 { sleep(PRODUCT_SPEED); } int add_to_lib() //添加产品到仓库 { num++;//仓库中的产品增加一个 msleep(500); return num; } void consum() //消费 { sleep(CONSUM_SPEED); } int sub_from_lib() //从仓库中取出产品 { num--; //仓库中的产品数量减一 msleep(500); return num; } void *productor(void *arg) //生产者线程 { while(1) { sem_wait(&p_sem);//生产信号量减一 product();// 生产延时 sem_wait(&sh_sem);//这个信号量是用来互斥的 printf("push into! tatol_num=%d\n",add_to_lib()); sem_post(&sh_sem); sem_post(&c_sem); //消费信号量加一 } } void *consumer(void *arg) //消费者线程 { while(1) { sem_wait(&c_sem); //消费者信号量减一 sem_wait(&sh_sem); printf("pop out! tatol_num=%d\n",sub_from_lib()); sem_post(&sh_sem); sem_post(&p_sem);//生产者信号量加一 consum();//消费延时 } } int main() { pthread_t tid1,tid2; sem_init(&p_sem,0,TOTAL_NUM-INIT_NUM); sem_init(&c_sem,0,INIT_NUM); sem_init(&sh_sem,0,1); pthread_create(&tid1,NULL,productor,NULL); pthread_create(&tid2,NULL,consumer,NULL); pthread_join(tid1,NULL); pthread_join(tid2,NULL); return 0; }
er
1https://www.cnblogs.com/Dzhouqi/p/3362259.html
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<semaphore.h> #include<pthread.h> #define msleep(x) usleep(x*1000) #define PRODUCT_SPEED 3 //生产速度 #define CONSUM_SPEED 1 //消费速度 #define INIT_NUM 3 //仓库原有产品数 #define TOTAL_NUM 10 //仓库容量 sem_t p_sem, c_sem, sh_sem; int num=INIT_NUM; void product(void) //生产产品 { sleep(PRODUCT_SPEED); } int add_to_lib() //添加产品到仓库 { num++;//仓库中的产品增加一个 msleep(500); return num; } void consum() //消费 { sleep(CONSUM_SPEED); } int sub_from_lib() //从仓库中取出产品 { num--; //仓库中的产品数量减一 msleep(500); return num; } void *productor(void *arg) //生产者线程 { while(1) { sem_wait(&p_sem);//生产信号量减一 product();// 生产延时 sem_wait(&sh_sem);//这个信号量是用来互斥的 printf("push into! tatol_num=%d\n",add_to_lib()); sem_post(&sh_sem); sem_post(&c_sem); //消费信号量加一 } } void *consumer(void *arg) //消费者线程 { while(1) { sem_wait(&c_sem); //消费者信号量减一 sem_wait(&sh_sem); printf("pop out! tatol_num=%d\n",sub_from_lib()); sem_post(&sh_sem); sem_post(&p_sem);//生产者信号量加一 consum();//消费延时 } } int main() { pthread_t tid1,tid2; sem_init(&p_sem,0,TOTAL_NUM-INIT_NUM); sem_init(&c_sem,0,INIT_NUM); sem_init(&sh_sem,0,1); pthread_create(&tid1,NULL,productor,NULL); pthread_create(&tid2,NULL,consumer,NULL); pthread_join(tid1,NULL); pthread_join(tid2,NULL); return 0; }
2https://blog.csdn.net/qq_33783291/article/details/70147401
#include <iostream> #include <string> #include <cmath> using namespace std; const double PRECISION = 1E-6; const int COUNT_OF_NUMBER = 4; const int NUMBER_TO_BE_CAL = 24; double number[COUNT_OF_NUMBER]; string expression[COUNT_OF_NUMBER]; bool Judgement = false; //判断是否有解。 int count = 0; void Search(int n) { if (n == 1) { if ( fabs(number[0] - NUMBER_TO_BE_CAL) <= PRECISION ) //对于除法,要小心小数的精确位数 { cout << expression[0] << "\t\t"; Judgement = true; count ++; if((count % 3)==0) cout<<endl; } else { } } for(int i=0; i < n; i++) { for (int j = i + 1; j < n; j++) { double a, b; string expa, expb; a = number[i]; b = number[j]; number[j] = number[n - 1]; //递归之后,n比以前小一位,所以可以不停向前赋值 expa = expression[i]; expb = expression[j]; expression[j] = expression[n - 1]; //递归之后,n比以前小一位,所以可以不停向前赋值 expression[i] = ‘(‘ + expa + ‘+‘ + expb + ‘)‘; //加法不需要分顺序 number[i] = a + b; Search(n-1); expression[i] = ‘(‘ + expa + ‘-‘ + expb + ‘)‘; //减法应该分顺序,减数以及被减数 number[i] = a - b; Search(n-1); expression[i] = ‘(‘ + expb + ‘-‘ + expa + ‘)‘; //减法应该分顺序,减数以及被减数 number[i] = b - a; Search(n-1); expression[i] = ‘(‘ + expa + ‘*‘ + expb + ‘)‘; //乘法不需要分顺序 number[i] = a * b; Search(n-1); if (b != 0) { expression[i] = ‘(‘ + expa + ‘/‘ + expb + ‘)‘; //除法应该分顺序,除数以及被除数 number[i] = a / b; Search(n-1); } if (a != 0) { expression[i] = ‘(‘ + expb + ‘/‘ + expa + ‘)‘; //除法应该分顺序,除数以及被除数 number[i] = b / a; Search(n-1); } number[i] = a; //这4句语句是为了防止如果上面几种可能都失败了的话, number[j] = b; //就把原来的赋值撤消回去,以无干扰的正确的进入到下一次 expression[i] = expa; //for循环队列中。 expression[j] = expb; // } } } int main() { cout<<"请依次输入4个数字:\n"; for (int i = 0; i < COUNT_OF_NUMBER; i++) { char buffer[20]; cout<<"第"<<i+1<<"个卡片:"; cin >> number[i]; itoa(number[i], buffer, 10); //itoa()函数的作用是把第一个参数(数值)传送到第二个参数(字符串)中去,第三个 //参数(int型)是该数值在字符串里以什么进制存放。 expression[i] = buffer; } cout<<endl; Search(COUNT_OF_NUMBER) ; if(Judgement==true) { cout << "\n成功" << endl; cout<<"所以可以计算的次数总和 = "<<count<<endl; } else { cout << "失败" << endl; } system("pause"); return 0; }
3
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<semaphore.h> #include<pthread.h> #define msleep(x) usleep(x*1000) #define PRODUCT_SPEED 3 //生产速度 #define CONSUM_SPEED 1 //消费速度 #define INIT_NUM 3 //仓库原有产品数 #define TOTAL_NUM 10 //仓库容量 sem_t p_sem, c_sem, sh_sem; int num=INIT_NUM; void product(void) //生产产品 { sleep(PRODUCT_SPEED); } int add_to_lib() //添加产品到仓库 { num++;//仓库中的产品增加一个 msleep(500); return num; } void consum() //消费 { sleep(CONSUM_SPEED); } int sub_from_lib() //从仓库中取出产品 { num--; //仓库中的产品数量减一 msleep(500); return num; } void *productor(void *arg) //生产者线程 { while(1) { sem_wait(&p_sem);//生产信号量减一 product();// 生产延时 sem_wait(&sh_sem);//这个信号量是用来互斥的 printf("push into! tatol_num=%d\n",add_to_lib()); sem_post(&sh_sem); sem_post(&c_sem); //消费信号量加一 } } void *consumer(void *arg) //消费者线程 { while(1) { sem_wait(&c_sem); //消费者信号量减一 sem_wait(&sh_sem); printf("pop out! tatol_num=%d\n",sub_from_lib()); sem_post(&sh_sem); sem_post(&p_sem);//生产者信号量加一 consum();//消费延时 } } int main() { pthread_t tid1,tid2; sem_init(&p_sem,0,TOTAL_NUM-INIT_NUM); sem_init(&c_sem,0,INIT_NUM); sem_init(&sh_sem,0,1); pthread_create(&tid1,NULL,productor,NULL); pthread_create(&tid2,NULL,consumer,NULL); pthread_join(tid1,NULL); pthread_join(tid2,NULL); return 0; } // 1.16.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include<string> #include "stdio.h" #include <math.h> using namespace std; const int CardsNumber = 4;//24点有四张卡 const double ErrorThreshold=1E-6;//由于舍入误差,需要加入一个误差阈值,误差在一定范围之内为,判断相等 const int ResultValue = 24; #define N 4 string result[CardsNumber]={"3","1","3","4"}; double number[CardsNumber]={3,1,3,4}; bool PointsGame(int n) { if(n == 1) { //如果结果为24 //由于舍入误差,应允许一定范围内的误差 if( fabs(number[0]-ResultValue)<ErrorThreshold) { cout<<result[0]<<endl; //cout<<‘1‘<<endl; return true; } else { return false; } } for(int i=0 ;i<n ;i++) { for(int j=i+1;j<n;j++) { double a,b; string expa,expb; a=number[i]; b=number[j]; number[j]=number[n-1]; expa=result[i]; expb=result[j]; result[j]=result[n-1]; result[i]=‘(‘+ expa +‘+‘+expb+‘)‘; number[i]=a+b; if(PointsGame(n-1)) return true; result[i]=‘(‘+ expa +‘-‘ + expb +‘)‘; number[i]=a-b; if(PointsGame(n-1)) return true; result[i]=‘(‘+ expa + ‘*‘ + expb +‘)‘; number[i]=a*b; if(PointsGame(n-1)) return true; if(b!=0) {result[i]=‘(‘+expa +‘/‘+ expb +‘)‘; number[i]=a/b; if(PointsGame(n-1)) return true; } number[i]=a; number[j]=b; result[i]=expa; result[j]=expb; } } return false; } int _tmain(int argc, _TCHAR* argv[]) { PointsGame(4); return 0; } #include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; int a1[5],b1[4]; void gol(char f,int k,int num) { if (f==‘a‘) for(int i=k;i<=num;i++) a1[i-1]=a1[i]; if (f==‘b‘) for(int i=k;i<=num;i++) b1[i-1]=b1[i]; } int cal(int a,int b,int num) { if (num==0) return a+b; if (num==1) return a-b; if (num==2) return a*b; if (num==3) return a/b; } string cha(int a) { string p; p=""; while(a>0) { p=(char)(a%10)+p; a/=10; } return p; } string chaf(int a) { if (a==0) return "+"; if (a==1) return "-"; if (a==2) return "*"; if (a==3) return "/"; } int main() { string ans[5]; int a[5],b[5],i,anum,bnum; for(i=1;i<=4;i++) cin >> a[i]; memset(b,0,sizeof(b)); bool f[4]; int s,j,d[4],ansnum; while(b[0]==0) { /*for(i=1;i<=3;i++) { cout << b[i] << " "; }*/ //cout << endl; memset(f,0,sizeof(f)); s=0; for(i=1;i<=3;i++) { if (b[i]==1 || b[i]==0) { if (b[i-1]==3 || b[i-1]==2 || b[i+1]==3 || b[i+1]==2) { s++; d[s]=i; } } } while(f[0]==0) { for(i=1;i<=s;i++) { cout << f[i] << " "; } cout << endl; for(i=1;i<=4;i++) a1[i]=a[i]; for(i=1;i<=3;i++) b1[i]=b[i]; anum=4;bnum=3; for(i=1;i<=4;i++) ans[i]=""; ansnum=0; j=1; while(j<=bnum) { if (f[j]==1) { ansnum++; ans[ansnum]=cha(a1[d[j]])+chaf(b1[d[j]])+chaf(a1[d[j]+1])+"="; a1[d[j]]=cal(a1[d[j]],a1[d[j]+1],b1[d[j]]); ans[ansnum]+=cha(a1[d[j]]); gol(‘a‘,d[j]+1,anum); anum--; gol(‘b‘,d[j]+1,bnum); bnum--; } j++; } j=1; //cout << anum << " " << bnum << endl; while(j<=bnum) { if (b[j]==2 || b[j]==3) { ansnum++; ans[ansnum]=cha(a1[j])+chaf(b1[j])+chaf(a1[j+1])+"="; a1[j]=cal(a1[j],a1[j+1],b1[j]); ans[ansnum]+=cha(a1[j]); gol(‘a‘,j+1,anum); anum--; gol(‘b‘,j+1,bnum); bnum--; } j++; } //cout << anum << " " << bnum << endl; j=1; while(j<=bnum) { ansnum++; ans[ansnum]=cha(a1[j])+chaf(b1[j])+chaf(a1[j+1])+"="; a1[j]=cal(a1[j],a1[j+1],b1[j]); ans[ansnum]+=cha(a1[j]); gol(‘a‘,j+1,anum); anum--; gol(‘b‘,j+1,bnum); bnum--; } //cout << anum << " " << bnum << endl; //cout << a1[1] << endl; if (a1[1]==24) { for(j=1;j<=ansnum;j++) { cout << ans[j] << endl; } return 0; } j=s; while(f[j]==1) { f[j]==0; j--; } f[j]=1; } //cout << "---------------\n"; i=3; while(b[i]==3) { b[i]=0; i--; } b[i]++; } } #include <iostream> #include <string> #include <cmath> using namespace std; const double PRECISION = 1E-6; const int COUNT_OF_NUMBER = 4; const int NUMBER_TO_BE_CAL = 24; double number[COUNT_OF_NUMBER]; string expression[COUNT_OF_NUMBER]; bool Judgement = false; //判断是否有解。 int count = 0; void Search(int n) { if (n == 1) { if ( fabs(number[0] - NUMBER_TO_BE_CAL) <= PRECISION ) //对于除法,要小心小数的精确位数 { cout << expression[0] << "\t\t"; Judgement = true; count ++; if((count % 3)==0) cout<<endl; } else { } } for(int i=0; i < n; i++) { for (int j = i + 1; j < n; j++) { double a, b; string expa, expb; a = number[i]; b = number[j]; number[j] = number[n - 1]; //递归之后,n比以前小一位,所以可以不停向前赋值 expa = expression[i]; expb = expression[j]; expression[j] = expression[n - 1]; //递归之后,n比以前小一位,所以可以不停向前赋值 expression[i] = ‘(‘ + expa + ‘+‘ + expb + ‘)‘; //加法不需要分顺序 number[i] = a + b; Search(n-1); expression[i] = ‘(‘ + expa + ‘-‘ + expb + ‘)‘; //减法应该分顺序,减数以及被减数 number[i] = a - b; Search(n-1); expression[i] = ‘(‘ + expb + ‘-‘ + expa + ‘)‘; //减法应该分顺序,减数以及被减数 number[i] = b - a; Search(n-1); expression[i] = ‘(‘ + expa + ‘*‘ + expb + ‘)‘; //乘法不需要分顺序 number[i] = a * b; Search(n-1); if (b != 0) { expression[i] = ‘(‘ + expa + ‘/‘ + expb + ‘)‘; //除法应该分顺序,除数以及被除数 number[i] = a / b; Search(n-1); } if (a != 0) { expression[i] = ‘(‘ + expb + ‘/‘ + expa + ‘)‘; //除法应该分顺序,除数以及被除数 number[i] = b / a; Search(n-1); } number[i] = a; //这4句语句是为了防止如果上面几种可能都失败了的话, number[j] = b; //就把原来的赋值撤消回去,以无干扰的正确的进入到下一次 expression[i] = expa; //for循环队列中。 expression[j] = expb; // } } } int main() { cout<<"请依次输入4个数字:\n"; for (int i = 0; i < COUNT_OF_NUMBER; i++) { char buffer[20]; cout<<"第"<<i+1<<"个卡片:"; cin >> number[i]; itoa(number[i], buffer, 10); //itoa()函数的作用是把第一个参数(数值)传送到第二个参数(字符串)中去,第三个 //参数(int型)是该数值在字符串里以什么进制存放。 expression[i] = buffer; } cout<<endl; Search(COUNT_OF_NUMBER) ; if(Judgement==true) { cout << "\n成功" << endl; cout<<"所以可以计算的次数总和 = "<<count<<endl; } else { cout << "失败" << endl; } system("pause"); return 0; }
https://www.cnblogs.com/hnrainll/archive/2011/04/21/2024089.html
标签:itoa 信号 tps 创建 线程池 produce card 允许 closed
原文地址:https://www.cnblogs.com/vactor/p/10594165.html