怎样用C++实现生产者消费者的模拟
// 首先你要有一个生产者和消费者公用的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.打开新的主数据库
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.