📄 thread.cpp
字号:
#include <iostream.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <memory>
#include <stdinc.h>
#include <afxmt.h>
// thread.cpp.
// Demonstrates OCI++ in a multithreaded environment, accessing an Oracle Server to
// perform a read and a write operation simultaneously.
// Ensure Project->Settings->General->Microsoft Foundation Classes = Use MFC in a shared DLL
// MFC specific thread management
// See Bottom of file for DDL statements.
// Thread stuff...
CRITICAL_SECTION critical_section;
static int ActiveThreadCounter = 0;
// Thread counters. Increment and decrement need mutually exclusive access, hence
// the use of EnterCriticalSection and LeaveCriticalSection (Win32 specific)
void IncCount(int& Counter)
{
EnterCriticalSection(&critical_section);
Counter++;
LeaveCriticalSection(&critical_section);
}
void DecCount(int& Counter)
{
EnterCriticalSection(&critical_section);
Counter--;
LeaveCriticalSection(&critical_section);
}
// Read objects from database
void read_obj(void* pParam);
// Write objects to database
void write_obj(void* pParam);
// Initialise threads
void init_thread();
void main()
{
init_thread();
}
void init_thread()
{
InitializeCriticalSection(&critical_section);
// create 10 threads to write objects, and 10 threads to read objects
for(int i = 0; i < 10; i++)
{
::IncCount( ActiveThreadCounter ); _beginthread(write_obj, 4096, (void*)i);
::IncCount( ActiveThreadCounter ); _beginthread(read_obj, 4096, (void*)i);
}
while( ActiveThreadCounter != 0 ){ /* Wait for all threads */ }
}
void read_obj(void* pParam)
{
try
{
COCISession s;
// connect to server
s.connect("temp/temp",OCI_OBJECT | OCI_THREADED);
cout << "<<< Read start >>>\n";
cout.flush();
std::vector<COCIObject> objs;
COCIStatement stmt(s);
stmt = "select ref(r) from m r";
std::vector<COCIRef> refs(100, COCIRef(s));
stmt.define(1,refs);
if(stmt.execute())
{
do
{
objs = COCIObject::array_pin(refs);
for(std::vector<COCIObject>::iterator itr = objs.begin(); itr != objs.end(); itr++)
{
//cout << (int)itr->get("id") << "," << flush;
}
}while(stmt.fetch());
//cout << endl << flush;
}
cout << "<<< Read end >>>\n";
cout.flush();
}
catch(CBaseException& exc)
{
char* buff = exc.GetError();
cout << buff << endl;
cout.flush();
}
::DecCount( ActiveThreadCounter );
}
void write_obj(void* pParam)
{
try
{
COCISession s;
s.connect("temp/temp",OCI_OBJECT | OCI_THREADED);
cout << "<<< Write start >>>\n";
cout.flush();
COCICache c(s);
COCIObject m(s, COCITable(s, "M"), COCIType(s, "M_T"));
m.set("id",(int)pParam);
// Copy m, 99 times => 99 + 1 objects get put on database
std::vector<COCIObject> objs(99, m);
c.flush();
cout << "<<< Write end >>>\n";
cout.flush();
}
catch(CBaseException& exc)
{
char* buff = exc.GetError();
cout << buff << endl;
cout.flush();
}
::DecCount( ActiveThreadCounter );
}
// DDL
// create type m_t as object (id number);
// create table m of m_t;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -