百科狗-知识改变命运!
--

怎样用C++实现生产者消费者的模拟

一语惊醒梦中人11个月前 (12-22)阅读数 4#综合百科
文章标签文件日志

// 首先你要有一个生产者和消费者公用的Buffer,它要求用mutex和condition variable来实现锁

struct BoundedBuffer {

int* buffer;

int capacity;

int front;

int rear;

int count;

std::mutex lock;

std::condition_variable not_full;

std::condition_variable not_empty;

BoundedBuffer(int capacity) : capacity(capacity), front(0), rear(0), count(0) {

buffer = new int[capacity];

}

~BoundedBuffer(){

delete[] buffer;

}

void deposit(int data){

std::unique_lock l(lock);

not_full.wait(l, [this](){return count != capacity; });

buffer[rear] = data;

rear = (rear + 1) % capacity;

++count;

not_empty.notify_one();

}

int fetch(){

std::unique_lock l(lock);

not_empty.wait(l, [this](){return count != 0; });

int result = buffer[front];

front = (front + 1) % capacity;

--count;

not_full.notify_one();

return result;

}

};

// 这里以下是实现。

void consumer(int id, BoundedBuffer& buffer){

for(int i = 0; i

int value = buffer.fetch();

std::cout SELECT GROUP#, BYTES FROM V$LOG;

在备库库查看日志组的数量和每个日志文件的大小

SQL> SELECT GROUP#, BYTES FROM V$STANDBY_LOG;

创建日志组和redo log文件

SQL> alter database add standby logfile

group 4 ('/ora11g/oracle/oracle/oradata/highfly/stdby_redo04.log') size50m,

group 5 ('/ora11g/oracle/oracle/oradata/highfly/stdby_redo05.log')size 50m,

group 6 ('/ora11g/oracle/oracle/oradata/highfly/stdby_redo06.log')size 50m,

group 7 ('/ora11g/oracle/oracle/oradata/highfly/stdby_redo07.log') size50m;

18 Start Redo Apply

在备库

SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USINGCURRENT LOGFILE DISCONNECT FROM SESSION;

SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;

SQL>alter user scott account lock;

查看哪些归档日志被APPLY了

在备库

SQL>SELECT SEQUENCE#,APPLIED FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;

在主库强制日志切换到当前的online redo log file.

SQL>ALTER SYSTEM ARCHIVE LOG CURRENT;

在备库查看新的被归档的redo data

SQL>SELECT SEQUENCE#, FIRST_TIME, NEXT_TIME FROMV$ARCHIVED_LOG ORDER BY SEQUENCE#;

在备库查看接收到的被应用的redo

SQL> SELECT SEQUENCE#,APPLIED FROM V$ARCHIVED_LOG ORDER BYSEQUENCE#;

查看数据库的角色

SQL>select database_role,protection_mode,protection_levelfrom v$database;

19 主备库切换

1.查看主库的状态

SQL> SELECTSWITCHOVER_STATUS FROM V$DATABASE;

2.将主库切换至备用模式

SQL> ALTERDATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY WITH -

> SESSION SHUTDOWN;

3.关闭、装载主数据库

SQL> SHUTDOWNABORT;

SQL> STARTUP MOUNT;

4.查看备库准备向主库模式切换

SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;

SWITCHOVER_STATUS

-----------------

TO_PRIMARY

1 row selected

5.切换备库至主库模式

SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY WITH SESSIONSHUTDOWN;

6.打开新的主数据库

怎样用C++实现生产者消费者的模拟

SQL> ALTER DATABASEOPEN;

7. 在新的备库服务器上启动 REDO apply。

SQL> ALTER DATABASE RECOVER MANAGED STANDBYDATABASE USING CURRENT LOGFILE -

> DISCONNECT FROMSESSION;

20 灾难恢复(failover)

Step 1

Flushany unsent redo from the primary database to the target standby

database

SQL> ALTER SYSTEMFLUSH REDO TO target_db_name;

Step 2

Verify that the standby database has the mostrecently archived redo log

filefor each primary database redo thread.

SQL>SELECT UNIQUE THREAD# AS THREAD, MAX(SEQUENCE#) -

> OVER (PARTITION BYthread#) AS LAST from V$ARCHIVED_LOG;

SQL> ALTER DATABASEREGISTER PHYSICAL LOGFILE 'filespec1';

Step 3

Identifyand resolve any archived redo log gaps.

SQL> SELECT THREAD#,LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;

SQL> ALTER DATABASEREGISTER PHYSICAL LOGFILE 'filespec1';

Step 4

RepeatStep 3 until all gaps are resolved.

Step 5

StopRedo Apply.

Issuethe following SQL statement on the target standby database:

SQL> ALTER DATABASERECOVER MANAGED STANDBY DATABASE CANCEL;

Step 6

Finishapplying all received redo data.

Issuethe following SQL statement on the target standby database:

SQL> ALTER DATABASERECOVER MANAGED STANDBY DATABASE FINISH;

Step 7

Verifythat the target standby database is ready to become a primary

database.

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)