计算机操作系统
今儿个看了看consumer-producer问题感觉应该比较简单,就打算用C&C++实现一下,好吧果然很简单。
唯一困扰我比较久的问题就是,C和C++的隐藏机制有一部分不是很一样,导致两者代码不能很好的融合。
问题如下:
线程创建时由于C会隐式的把通用指针转换成其他类型指针,但是在C11标准中C++并不会这么做,SO我在这上面浪费了好久的时间FUCK!!!体现出来就是C++的线程函数必须是(void [*])(*) (void *)形式。
下面就直接上代码啦
C代码:
/*
* > @CopyRight
* > PROJECTNAME : PthreadTest
* > FILENAME : Thread.c
* > AUTHOR : Catalpeak
* > TIME : 2018.9.29
* > CONNECTION : 784734297@163.com
* > LIAONING UNIVERSITY LNU
*/
#include "Thread.h"
void
Consumer (void * _msg);
void
Producer (void * _msg);
int main ()
{
char *msg1 = "Consumer pthread has been created";
char *msg2 = "Producer pthread has been created";
char *msg3 = "Consumer pthread created fail";
char *msg4 = "Producer pthread created fail";
char *msg5 = "Consumer pthread join fail";
char *msg6 = "Producer pthread join fail";
pthread_t ConsumerPthread, ProducerPthread;
void * retval; // Return Value
int ConsumerPthreadCreateSuccess = 0, ProducerPthreadCreateSuccess = 0;
int ConsumerPthreadJoinSuccess = 0, ProducerPthreadJoinSuccess = 0;
InitThread ();
ConsumerPthreadCreateSuccess =
pthread_create (&ConsumerPthread, NULL, (void *) &Consumer, (void *) msg1);
if (ConsumerPthreadCreateSuccess != 0) {
printf ("%sn", msg3);
return 0x00000002;
}
ProducerPthreadCreateSuccess =
pthread_create (&ProducerPthread, NULL, (void *) &Producer, (void *) msg2);
if (ProducerPthreadCreateSuccess != 0) {
printf ("%sn", msg4);
return 0x00000003;
}
// Start Pthread
ConsumerPthreadJoinSuccess =
pthread_join (ConsumerPthread, &retval);
if (ConsumerPthreadJoinSuccess != 0) {
printf ("%sn", msg5);
return 0x00000004;
}
ProducerPthreadJoinSuccess =
pthread_join (ProducerPthread, &retval);
if (ProducerPthreadJoinSuccess != 0) {
printf ("%sn", msg6);
return 0x00000005;
}
return 1;
}
void
Consumer (void * _msg) {
int nowNumber = 0;
printf ("%sn", (char *)_msg);
do {
wait (BUFFER_FULL);
wait (PTHREAD_MUTEX);
nowNumber = DeQueue (&buffer);
signature (PTHREAD_MUTEX);
signature (BUFFER_EMPTY);
Caculate (nowNumber);
} while (1);
}
void
Producer (void * _msg) {
int AddNumber = 0;
printf ("%sn", (char *)_msg);
do {
AddNumber = rand () % 10 + 1;
wait (BUFFER_EMPTY);
wait (PTHREAD_MUTEX);
EnQueue (&buffer, AddNumber);
signature (PTHREAD_MUTEX);
signature (BUFFER_FULL);
} while (1);
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "../MySignal/mSignal.h"
#include <semaphore.h>
#include <time.h>
#define semaphore pthread_mutex_t
#define BUFFER_EMPTY 1
#define BUFFER_FULL 2
#define PTHREAD_MUTEX 3
Signal buffer;
semaphore mutex = PTHREAD_MUTEX_INITIALIZER;
sem_t empty;
sem_t full;
double SUMNUMBER = 0.0;
void
wait (int type);
void
signature (int type);
void
Caculate (int number);
void InitThread ();
void InitThread () {
sem_init (&empty, 0, 999);
sem_init (&full, 0, 1);
srand ((unsigned int) time (NULL));
InitSignalQueue (&buffer);
}
void
wait (int type) {
switch (type) {
case BUFFER_EMPTY: {
sem_wait (&empty);
} break;
case BUFFER_FULL: {
sem_wait (&full);
} break;
case PTHREAD_MUTEX: {
if (0 != pthread_mutex_lock (&mutex)) {
printf ("ERROR 0x00000008 in Wait "PTHREAD_MUTEX_LOCK"n");
perror ("ERROR 0x00000008 in Wait "PTHREAD_MUTEX_LOCK"n");
exit (EXIT_FAILURE);
}
} break;
default : {
printf ("ERROR 0x00000006 Wait inputs wrong valuen");
} break;
};
return;
}
void
signature (int type) {
switch (type) {
case BUFFER_EMPTY: {
sem_post (&empty);
} break;
case BUFFER_FULL: {
sem_post (&full);
} break;
case PTHREAD_MUTEX: {
if (0 != pthread_mutex_unlock (&mutex)) {
printf ("ERROR 0x00000009 in signature "PTHREAD_MUTEX_UNLOCK"n");
perror ("ERROR 0x00000009 in signature "PTHREAD_MUTEX_UNLOCK"n");
exit (EXIT_FAILURE);
}
} break;
default : {
printf ("ERROR 0x00000007 Signature inputs wrong valuen");
} break;
};
return;
}
void
Caculate (int number) {
SUMNUMBER += number;
if (SUMNUMBER >= 10000000000.0) {
SUMNUMBER = 0.0;
}
printf ("%.lfn", SUMNUMBER);
}
/*
* > mSignal.h
*/
/* @CopyRight 2018-9-21 21:52 LiaoNing University
#ifndef MSIGNAL_H
#define MSIGNAL_H
#include <stdio.h>
// Change Type of Element here
#define ElemType char
#define SIGNALLENGTH 1000 // The length of all signals in one period
typedef struct Signal {
ElemType signal [SIGNALLENGTH]; // Save Element
int SignalSize; // The MAX Size of Signal Queue
int head; // The number point to head of Queue
int tail; // The number point to tail of Queue
} Signal ;
/* Function : Initialize Signal Queue
void InitSignalQueue (Signal* _signal);
/* Function : Insert a value in signal's tail
* Para : ElemType macro definition
* Para : struct Signal pointer
* Return : void
*/
void EnQueue (Signal* _signal, ElemType value);
/* Function : Get the first element of signal vector
* Para : struct Signal signal pointer
* Return : ElemType
*/
ElemType DeQueue (Signal* _signal);
/* Function : judge if this Queue is empty
* Para : struct signal pointer
* Return : int , 1 - yes 2 - no
*/
int IfEmpty (Signal* _signal);
/* Function : judge if this Queue is filled
* Para : struct signal pointer
* Return : int , 1 - yes / 2 - no
*/
int IfFull (Signal* _signal);
#endif // MSIGNAL_H
/*
* > mSignal.c
*/
/* @CopyRight 2018-9-21 21:52 LiaoNing University
#include "mSignal.h"
void InitSignalQueue (Signal* _signal) {
_signal->head = 0;
_signal->tail = 0;
_signal->SignalSize = SIGNALLENGTH;
}
void EnQueue(Signal* _signal, ElemType value) {
if ((_signal->tail + 1) % _signal->SignalSize == _signal->head) {
// Signal Queue has filled can't insert
} else {
_signal->signal [_signal->tail] = value;
_signal->tail = (_signal->tail + 1) % _signal->SignalSize;
}
}
ElemType DeQueue (Signal* _signal) {
int temp;
if (_signal->head == _signal->tail) {
// This Queue is empty can't get element
} else {
temp = _signal->signal [_signal->head];
_signal->head = (_signal->head + 1) % _signal->SignalSize;
}
return temp;
}
int IfEmpty (Signal* _signal) {
if (_signal->head == _signal->tail) {
return 1;
} else {
return 0;
}
}
int IfFull (Signal* _signal) {
if ((_signal->tail + 1) % _signal->SignalSize == _signal->head) {
return 1;
} else {
return 0;
}
}
C++代码如下:
/*
* > main.cpp
*/
#include "../Header/main.h"
int main ()
{
pthread_t consumerPthread, producerPthread;
char *_msg1 = "FUCK1";// = "Consumer thread has been created";
char *_msg2 = "FUCK2";// = "Producer thread has been created";
int returnProducer, returnConsumer;
void *retConsumer, *retProducer;
returnConsumer = pthread_create (&consumerPthread, NULL, Consumer, (void *) _msg1);
returnProducer = pthread_create (&producerPthread, NULL, Producer, (void *) _msg2);
returnConsumer = pthread_join (consumerPthread, &retConsumer);
returnProducer = pthread_join (producerPthread, &retProducer);
return 1;
}
printf ("%sn", (char *)msg);
do {
//Sleep (100);
while (produceMutex < 0);
produceMutex--;
while (mutex <= 0);
mutex--;
/*
cout << "In Producer thread" << endl;
cout << "produceMutex is : " << produceMutex << endl;
cout << "consumeMutex is : " << consumeMutex << endl;
cout << "mutex should be 0 " << "And now is ; " << mutex << endl;
int temp = rand () % 10;
buffer [consumeMutex] = to_String (temp, 1);
//cout << buffer [consumeMutex] << endl;
mutex++;
consumeMutex++;
} while (1);
return (void*) 1;
}
printf ("%sn", (char*)msg);
do {
//Sleep (100);
while (consumeMutex <= 0);
consumeMutex--;
while (mutex <= 0);
mutex--;
/*
cout << "In Consumer thread" << endl;
cout << "produceMutex is : " << produceMutex << endl;
cout << "consumeMutex is : " << consumeMutex << endl;
cout << "mutex should be 0 " << "And now is ; " << mutex << endl;
cout << buffer [consumeMutex] << endl;
mutex++;
produceMutex++;
} while (1);
return (void*) 1;
}
/*
* > main.h
*/
#include <iostream>
#include <string>
#include <pthread.h>
#include <time.h>
#include <windows.h>
#include <cstdio>
#include "../../utils/utils.h"
using namespace std;
// The size of buffer
#define SIZE_ 10
// Mutually variable
int mutex = 1;
// consumer Mutex
int consumeMutex = 0;
// Produce Mytex
int produceMutex = SIZE_;
// Buffer for save
string buffer [SIZE_];
Producer (void * msg);
Consumer (void * msg);
/*
* > utils.h
*/
#include <string>
#define API_ ;
API_ std::string to_String(int n, int max);
/*
* > utils.cpp
*/
#include "utils.h"
std::string to_String(int n, int max)
{
int m = n;
char s[max];
char ss[max];
int i=0,j=0;
if (n < 0)// ´¦Àí¸ºÊý
{
m = 0 - m;
j = 1;
ss[0] = '-';
}
while (m>0)
{
s[i++] = m % 10 + '0';
m /= 10;
}
s[i] = ' ';
i = i - 1;
while (i >= 0)
{
ss[j++] = s[i--];
}
ss[j] = ' ';
return ss;
}
我的编辑器不支持.cc的后缀,写cpp感觉好不舒服啊啊啊啊。
相关知识
一家宠物医院通过计算机应用程序把每次宠物就诊的账单提供给客户。需要在操作系统的帮
计算机协议有哪些
基于计算机虚拟宠物的个性化服务系统及其方法
基于计算机虚拟宠物的个性化服务系统及其方法技术方案
[附源码]JAVA+ssm计算机毕业设计宠物寄养预约系统(程序+Lw)
计算机理论论文通用12篇
全国计算机四级
人工智能产业的未来之星大PK 中国大学生计算机设计大赛落幕
基于springboot实现宠物咖啡馆平台管理系统项目【项目源码+论文说明】计算机毕业设计
计算机视觉
网址: 计算机操作系统 https://www.mcbbbk.com/newsview263090.html
上一篇: 宠物健身训练装置、系统及方法 |
下一篇: 布尔代数入门 |
推荐分享

- 1我的狗老公李淑敏33——如何 5096
- 2南京宠物粮食薄荷饼宠物食品包 4363
- 3家养水獭多少钱一只正常 3825
- 4豆柴犬为什么不建议养?可爱的 3668
- 5自制狗狗辅食:棉花面纱犬的美 3615
- 6狗交配为什么会锁住?从狗狗生 3601
- 7广州哪里卖宠物猫狗的选择性多 3535
- 8湖南隆飞尔动物药业有限公司宠 3477
- 9黄金蟒的价格 3396
- 10益和 MATCHWELL 狗 3352