PIXNET Logo登入

<font face="新細明體"><B>蕭沖的書房</font>

跳到主文

各種知識的象牙塔

部落格全站分類:數位生活

  • 相簿
  • 部落格
  • 留言
  • 名片
  • 12月 11 週四 200802:10
  • Thread-Safe的理解與分析

 
⓪著作: 蕭沖
 
何謂thread-safe? 這個問題我看過許多論壇都有討論過,都總讓人覺得不很滿意。在此,筆者想要用更logical的方式來把議題說清楚。首先,我們要了解它的定義! 定義若都不明白就難以判別安不安全了!
(繼續閱讀...)
文章標籤

aftcast 發表在 痞客邦 留言(7) 人氣(44,318)

  • 個人分類:Win32
▲top
  • 12月 11 週四 200801:51
  • DLL 共享全域變數的議題

⓪編著: 蕭沖
我們知道dll最主要的用途就是在於程式碼共享。而共享的過程中又會演生出全域資料(變數)是否共享的問題。
在多個process間共用同一個dll時,變數的共享與否是靠二個機制:
1/ 不共享全域變數 : 使用copy-on-write的觀念來處理  ( windows 系統自動預設是如此)
2/ 共享全域變數: 我們可以透過把變數放入一個share的segment的記憶體區段內,然後process間就可以共用。
(繼續閱讀...)
文章標籤

aftcast 發表在 痞客邦 留言(2) 人氣(2,563)

  • 個人分類:Win32
▲top
  • 12月 09 週二 200801:34
  • VC6 與 DirectX 版本問題

⓪編著: 蕭沖
 
經實測VC6能夠使用最穩定的DirectX版本為 sum2004版,往後的版本因為有加入bufferoverflow的安全性問題函數,使得VC6無法正確編譯。
然而,在使用sum2004版的時候,確也發生了一個致命的問題,在網路上找了許久才在日本的網頁上找到答案… 此問題是
(繼續閱讀...)
文章標籤

aftcast 發表在 痞客邦 留言(0) 人氣(342)

  • 個人分類:C/C++
▲top
  • 12月 09 週二 200801:14
  • BCB 與 DsPack 安裝筆記

⓪編著: 蕭沖
經過數回合的安裝測式後發現,最好的組合有二種:
1/ BcB6(經過update4) + DsPack231 + DirectX 9.0c 第一版(sum2004) + dxsdk_oct2004_extras (修正vc6用的lib)
2/ BcB6(經過update4) + DsPack234 + DirectX 9.0c dec2004
(繼續閱讀...)
文章標籤

aftcast 發表在 痞客邦 留言(4) 人氣(2,878)

  • 個人分類:BCB
▲top
  • 11月 17 週一 200809:54
  • C++中關於extern "C"的意義

⓪編著 : 蕭沖
extern "C" 是C++特有的組合關鍵字,在C裡並沒有這個的組合,僅有extern這個關鍵字!
為什麼C++會需要這樣的關鍵字組呢? 原因是C++它有一個複載(overloading)的功能,也就是說同樣的函式名稱可以有多個定義只要參數簽名不同即可。比如說C++裡可以有以下的二個宣告
bar(int i, int j);
(繼續閱讀...)
文章標籤

aftcast 發表在 痞客邦 留言(3) 人氣(54,722)

  • 個人分類:C/C++
▲top
  • 10月 23 週四 200805:25
  • 386 addressing mode Note (386 定址模式)

⓪編著 : 蕭沖
看了好幾年的Assembly了,總一段時間就會翻一下,尤其是在Debug hi-level language或是做reserve engineer(dont say it crack :->)時會參考。直接用ASM來寫程式是難得有機會了,所以某些學習過的記憶也經常就忘了。對於386 addressing的部份,看過許多不同的書,分類方式總有些些不同,在此想把各種版本的精華歸納出來。
Instruction Format (微指令格式)
Lower mem addr ----------------------->Higher mem addr
prefix|opcode|mod|reg|r/m|displacement|immediate data
Prefix :1 Byte, Optional, chage the default size of opcode size field(W),or
change address size, override selector....
Opcode :1~2 Bytes micro instruction
Mod+r/m :combined to operand (運算元之一).
mod 2Byte indicate that r/m (3Bytes) is mem or reg.
Reg :3 bytes operand (另一個運算元)
Displacement : 1~ 4 Bytes (位移)
Immediate data : 1~4 Bytes (立即資料,為常數)
所謂定址法(addressing mode)就是:「CPU找資料所在位置的方法」
以Instruction Format來看就是指opcode後面的那些值,(mod+r/m, reg, disp, immediate data)。
以mov ax, bx 這個Instruction來說,就是用二個相同的暫存器定址模式(register addressing mode),也就是說資料都是存放在register裡,即ax和bx裡。整個指令的意思就是把bx裡的資料copy到ax裡(注:有人直接翻mov為"搬",這樣感覺指令結束後bx會變成空的,然而不是這樣的)。
以mov ax, [mem] 來說,則用了二種不同的定址法: 其一是上面的reg addressing,另一個則是記憶體直接模式(memory direct mode or called displacement)。意思是去mem這個位址找資料然後把資料copy到ax暫存器裡。
我的分類法:
1/ 依容器來分 2/把memory模式再依取法分 3/在把memory indirect的部份依元素來分


+-REG mode +---General REG mode
| |___Segment(selector) REG mode
+-Immediate mode
|
+-Memory mode +--Direct mode(Displacement mode)
|__Indirect mode+-Memory Indirect mode(REG indirect modeS mix)
|_REG indirect mode+- base reg indirect
|_ base reg + displacement
|_ base reg + index
|_ base reg + index + disp
|_ ESP- mode
|_ ESP+ mode



定址法與高階語言的關聯:
1/ 程式中的全域變數在compile的時候就會經過計算把變數名換成"固定"的相對記憶體位址。比如說變數gVar被轉成0x11A0000的位址,而和gVar相關的statement則會被轉成Direct mode addresing,for example:
gVar += 2; 在runtime時的ASM被轉成

add dword ptr [0x004ce408], 0x02

這指令即用了 Direct mode + Immediate mode。故我稱gVar為直間變數。
2/在函式裡編寫下面的方程:


int i = 7; //C745 F4 07000000 MOV DWORD PTR SS:[EBP-C],7
int *p_var = &i;
//8D45 F4 LEA EAX,DWORD PTR SS:[EBP-C]
//8945 F0 MOV DWORD PTR SS:[EBP-10],EAX
(*p_ar)++;
//8B55 F0 MOV EDX,DWORD PTR SS:[EBP-10]
//FF02 INC DWORD PTR DS:[EDX]



注解的部份是被組譯後的情形。而最後一行INC DWORD PTR DS:[EDX]則使用了Indirect mode。
也是我想強調的部份,指標最終都是使用間接模式。故我稱指標為接間接變數!
順便也解說一下上面幾行機器碼的Instruction Foramt:
C745 F4 07000000 <<-- C7是MOV 的opcode,45是mod+r/m 與reg共1Byte,F4(C的補數)為1byte的有號值,等於-12,此為displacement的部份,最後一部份是立即值07000000共4byte。
同樣是MOV微指令,但當定址方式不同,或是二個operand位置不同等,都會造成MOV這個微指令有所不同。
(繼續閱讀...)
文章標籤

aftcast 發表在 痞客邦 留言(0) 人氣(2,069)

  • 個人分類:Assembly
▲top
  • 10月 23 週四 200805:24
  • Multithread Pool and Socket Programming Note

⓪編著 :蕭沖
Thread Pool
1/ Create map container of thread data MAY including idle property for later SetEvent, event handle property for Reset event later, thread handle property for close handle later .....by thread id as its key.
2/ Create WorkItem Queue (list for insert priority item or deque for normal queue) data with object,parameter pointer,function pointer.....
3/ Create thread function with thread map container parameter. Impelement function as : get event handler by thread id key from container, WaitForMultipleObjects for handler just get and more utra event handler like shutdown notify. Or using simple semaphore mechanism. Get WorkItem from Queue, and call funtion. Keep looping...
4/ Create threads and insert each thread data into map.
5/ Queue the WorkItem and set event handler for each idle thread in map.
One thread can create its own event object, and be controled after queue work.
Semaphore is a global object, and every thread check it to see if it's count greater than 0,
if yes, then go work, if equal to 0, then keep waiting....
Multithread Socket programming
1/ normal model is after listening imcoming request.... loop for accept call.
2/ accept function is a bloking function, ie, if there's no request, it won't return, for this reason,
process beter create one thread to create server socket and binding and listening....
3/ when accept call returned, it create a new client socket for further communication with client.
4/ create a thread to deal with this new client socket for sending msg or rcv msg....
Traditional model is just like above, it using one thread per connection. and dynamically create thread.
Threads are created and then terminated, cycling....but this may cause taking much overhead.
Using thread pool for deal with client sockets is a good ideal for saving resource.
PS.The other model is using IOCP model for more robust and scalable....
(繼續閱讀...)
文章標籤

aftcast 發表在 痞客邦 留言(0) 人氣(1,596)

  • 個人分類:Win32
▲top
  • 10月 23 週四 200805:23
  • Socket mode Note

⓪編著 :蕭沖
總的來說,Socket 可分為二種模式 : blocking mode, non-blocking mode。
要使socket成為non-blocking mode只要設定ioctlsocket為non-blocking mode就可以,然而,若配合使用了blocking functions,會使得讀/取不到資料時產生WSAEWOULDBLOCK的error。因此若要成功而完整的使用non-blocking mode,就必需配合適當的socket IO models。
Socket I/O 有六種模型:blocking, select, WSAAsyncSelect, WSAEventSelect, overlapped, and completion port。
針對不同的模型,我以一個虛擬的例子來說明: 假設有128條水管可以出水,但每一條出水的時間不同,且有快有慢,若我們的任務是在每一條水管必需各裝3桶水,那麼我們可以達成的方法有:  (人數相當於thread的數量,水管數相當於socket數,水桶相當於傳送或接收的Buffer)
(繼續閱讀...)
文章標籤

aftcast 發表在 痞客邦 留言(2) 人氣(7,759)

  • 個人分類:Win32
▲top
  • 10月 23 週四 200805:22
  • 使 用 BCB開 發 大 型 系 統 - Package 的應用重點

⓪編著 :蕭沖
寫這篇文章的靈感來源是來自李維在多年前的一篇注名文章:
http://www2.borland.com.tw/tw/reference9.html (2013年補:此文已不見了,有需要的可以向我要備份)
裡面提到如何使用package切割子系統方便開發與管理,並可增進效能。曾經看網友問到要如何把裡面的範例轉換成對應bcb的語法?因為裡面用到許多delphi才特有的一些功能,在c++裡原本沒有那些功能,比如說delphi的unit裡有initialization段、finalization段。再者delphi可以直接使用TComponentClass(VMT(Virtual Method Table)的實作)這個特別的類別來create instance。以下是我測試出對應到delphi的實作方式,希望以bcb開發的用戶也可以使用bpl開發出較大型的模組化程式。
★c++ builder裡的實作方式:
(繼續閱讀...)
文章標籤

aftcast 發表在 痞客邦 留言(0) 人氣(2,625)

  • 個人分類:BCB
▲top
  • 10月 23 週四 200805:21
  • How to make dpk to install into BCB by DCC32

New switches for the Pascal DCC32.EXE compiler:
[lb] -jp switch: creates Borland C++ compatible .OBJ files.
[lb] -jph switch: creates C++Builder compatible header (.HPP) files from Object Pascal unit files (.DCL).
[lb] -jphn switch: uses the Object Pascal unit name as the enclosing C++ namespace for both .OBJs and .HPPs that are generated.
[lb] -n switch: specify .DCU output directory
[lb] -nh switch: specify .HPP output directory
[lb] -no switch: specify .OBJ output directory
Example from Indy batch file: Fullc6.bat
@echo off
if (%1)==() goto test_command
if (%1)==(start) goto start
goto endok
:test_command
if (%COMSPEC%)==() goto no_command
%COMSPEC% /E:9216 /C %0 start %1 %2 %3
goto endok
:no_command
echo No Command Interpreter found
goto endok
:start
call clean.bat
computil SetupC6
if exist setenv.bat call setenv.bat
if not exist ..\C6\*.* md ..\C6 >nul
if exist ..\C6\*.* call clean.bat ..\C6if (%NDC6%)==() goto enderror
if (%NDWINSYS%)==() goto enderror
copy *.pas ..\C6
copy *.dpk ..\C6
copy *.obj ..\C6
copy *.inc ..\C6
copy *.res ..\C6
copy *.dcr ..\C6
copy *.rsp ..\C6
if (%NDC6%)==() goto enderror
if (%NDWINSYS%)==() goto enderror
cd ..\C6
REM ***************************************************
REM Compile Runtime Package Indy60
REM ***************************************************
REM IdCompressionIntercept can never be built as part of a package. It has to be compileed separately
REM due to a DCC32 bug.
%NDC6%\bin\dcc32.exe IdCompressionIntercept.pas /O..\Source\objs /DBCB /M /H /W /JPHN -$d-l-n+p+r-s-t-w-y- %2 %3 %4
%NDC6%\bin\dcc32.exe Indy60.dpk /O..\Source\objs /DBCB /M /H /W /JPHN -$d-l-n+p+r-s-t-w-y- %2 %3 %4
if errorlevel 1 goto enderror
%NDC6%\bin\dcc32.exe IdDummyUnit.pas /LIndy60.dcp /DBCB /O..\Source\objs /M /H /W /JPHN -$d-l-n+p+r-s-t-w-y- %2 %3 %4
if errorlevel 1 goto enderror
del IdDummyUnit.dcu >nul
del IdDummyUnit.hpp >nul
del IdDummyUnit.obj >nul
%NDC6%\bin\dcc32.exe Indy60.dpk /M /DBCB /O..\Source\objs /H /W -$d-l-n+p+r-s-t-w-y- %2 %3 %4
if errorlevel 1 goto enderror
copy Indy60.bpl %NDWINSYS% >nul
del Indy60.bpl > nul
REM ***************************************************
REM Create .LIB file
REM ***************************************************
echo Creating Indy60.LIB file, please wait...
%NDC6%\bin\tlib.exe Indy60.lib /P32 @IndyWin32.rsp >nul
if exist ..\C6\Indy60.bak del ..\C6\Indy60.bak >nul
REM ***************************************************
REM Compile Design-time Package RPDT30
REM ***************************************************
%NDC6%\bin\dcc32.exe dclIndy60.dpk /DBCB /O..\Source\objs /H /W /N..\C6 /LIndy60.dcp -$d-l-n+p+r-s-t-w-y- %2 %3 %4
if errorlevel 1 goto enderror
REM ***************************************************
REM Clean-up
REM ***************************************************
del dclIndy60.dcu >nul
del dclIndy60.dcp >nul
del Indy60.dcu >nul
del *.pas > nul
del *.dpk > nul
del *.inc > nul
del *.dcr > nul
del *.rsp > nul
REM ***************************************************
REM Design-time only unit .DCU's are not needed.
REM ***************************************************
if exist IdAbout.dcu del IdAbout.dcu >nul
if exist IdDsnBaseCmpEdt.dcu del IdDsnBaseCmpEdt.dcu >nul
if exist IdDsnPropEdBinding.dcu del IdDsnPropEdBinding.dcu >nul
if exist IdDsnRegister.dcu del IdDsnRegister.dcu >nul
if exist IdRegister.dcu del IdRegister.dcu >nul
goto endok
:enderror
call clean
echo Error!
:endok
cd ..\Source
ref: http://homepages.borland.com/ccalvert/Books/BcbUnleashed/Part14.htm
(繼續閱讀...)
文章標籤

aftcast 發表在 痞客邦 留言(0) 人氣(604)

  • 個人分類:BCB
▲top
«12345»

自訂側欄

自訂側欄

自訂側欄

大頭貼

aftcast
暱稱:
aftcast
分類:
數位生活
好友:
累積中
地區:

近期文章

  • DELPHI / C++ Builder 使用DDE client時出現亂碼問題
  • XE C++ builder 不能 source debug的原因
  • Windows 7 IE9 IE10 藍畫面 藍屏
  • xcode裡的git (source control->commit)無法成功
  • Windows Runtime ( WinRT) 的精簡介紹筆記
  • verify_signer_identity: Could not copy validate signature: -402620393
  • 我的iphone app --首創電話即時計費與通話警示軟體-電話王
  • CB 當畫面放了許多且很大的圖後無法debug
  • 民國100年5月1日勞動節遇星期日是否該補一天假??
  • open source 的GPL LGPL Apache MIT BSD 等 license 版權的問題 與感染 分析

最新迴響

  • [22/06/09] 路人 於文章「貼2002年文章 : 愛情的烏托邦...」留言:
    愛情烏托邦? 一個只有你跟我不被打擾污染的世界,進去不想被叫...
  • [18/02/23] Peter 於文章「Thread-Safe的理解與分析...」留言:
    您好,想請問讓不安全的thread變安全得第二種方法中, 只...
  • [16/04/18] Ben Chan 於文章「C++中關於extern "C"的意義...」留言:
    THX 大神!...
  • [15/04/30] wefuntw 於文章「C++中關於extern "C"的意義...」留言:
    讚! 大解惑!!! ...
  • [15/03/29] 溫啟清 於文章「Thread-Safe的理解與分析...」留言:
    若什麼是auto變數、stack都還不清楚,推薦閱讀"程式設...
  • [13/12/27] Kristi Huang 於文章「我的iphone app --首創電話即...」留言:
    請問未來是否會更新適用威寶的費率,很期待能使用您開發的APP...
  • [12/06/07] 訪客 於文章「Thread-Safe的理解與分析...」留言:
    很好,很容易理解,淺顯易懂。真厲害!...
  • [11/12/10] 長工老王 於文章「Thread-Safe的理解與分析...」留言:
    那call 副程式,算不算安全呢 ...
  • [11/11/11] EE 於文章「Socket mode Note...」留言:
    這水桶比喻還真的完全的看不懂阿~~~...
  • [10/11/10] RURU 於文章「Thread-Safe的理解與分析...」留言:
    Thread-Safe本身就是一種藝術啊~~~~~~讚!...

文章彙整

文章分類

toggle 電腦維修 (1)
  • 電腦維修 (1)
toggle 軟體程式設計 (7)
  • BCB (15)
  • Win32 (7)
  • C/C++ (7)
  • Assembly (1)
  • Web (1)
  • visual studio c++ (1)
  • iPhone develop (1)
  • IT相關 (1)
  • Life (4)
  • 未分類文章 (1)

部落格文章搜尋

誰來我家

參觀人氣

  • 本日人氣:
  • 累積人氣: