锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久成人国产精品二三区,久久久国产精品福利免费,国产综合免费精品久久久http://www.shnenglu.com/huangyi5209/category/15847.htmlzh-cnSun, 29 May 2011 02:54:37 GMTSun, 29 May 2011 02:54:37 GMT60maximum contiguous subsequence sum algorithm 鏈澶у瓙搴忓垪綆楁硶http://www.shnenglu.com/huangyi5209/articles/144084.htmlhuangyi5209huangyi5209Tue, 12 Apr 2011 23:13:00 GMThttp://www.shnenglu.com/huangyi5209/articles/144084.htmlhttp://www.shnenglu.com/huangyi5209/comments/144084.htmlhttp://www.shnenglu.com/huangyi5209/articles/144084.html#Feedback0http://www.shnenglu.com/huangyi5209/comments/commentRss/144084.htmlhttp://www.shnenglu.com/huangyi5209/services/trackbacks/144084.html闃呰鍏ㄦ枃

]]>
InterlockedExchange 綰跨▼鍚屾http://www.shnenglu.com/huangyi5209/articles/142860.htmlhuangyi5209huangyi5209Mon, 28 Mar 2011 08:09:00 GMThttp://www.shnenglu.com/huangyi5209/articles/142860.htmlhttp://www.shnenglu.com/huangyi5209/comments/142860.htmlhttp://www.shnenglu.com/huangyi5209/articles/142860.html#Feedback0http://www.shnenglu.com/huangyi5209/comments/commentRss/142860.htmlhttp://www.shnenglu.com/huangyi5209/services/trackbacks/142860.html 

    #include <iostream>
#include 
<Windows.h>

using namespace std;

volatile LONG lfsSpinLock = 0;
int num = 0;
// lfsSpinLock is a 'long' and when it is '1' it is locked, and '0' is unlocked
#define  ENTER_LFS_LOCK_   while( InterlockedExchange( &lfsSpinLock, 1 ) == 1 ){ Sleep( 10 ); }
#define  EXIT_LFS_LOCK_    InterlockedExchange( &lfsSpinLock, 0 );


void Func1() 


    
//Wait to access the resource.  絳夊緟璧勬簮 

    ENTER_LFS_LOCK_;

    
for (int i = 0;i < 20; i++)
    
{
        num
++;
        cout
<<num<<endl;
    }
    

    EXIT_LFS_LOCK_;

}
 



int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE hThrd[
4];

    DWORD dwThrdID[
4];
    hThrd[
0= CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Func1, NULL, 0&dwThrdID[0]);
    hThrd[
1= CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Func1, NULL, 0&dwThrdID[1]);
    hThrd[
2= CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Func1, NULL, 0&dwThrdID[2]);
    hThrd[
3= CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Func1, NULL, 0&dwThrdID[3]);

    WaitForMultipleObjects(
4, hThrd, TRUE, INFINITE);
    
for (int i=0;i<4;i++)
    
{
        CloseHandle(hThrd[i]);
    }


    system(
"pause");
    
return 0;
}


]]>
Thread Synchronization 綰跨▼鍚屾http://www.shnenglu.com/huangyi5209/articles/141190.htmlhuangyi5209huangyi5209Sun, 06 Mar 2011 01:26:00 GMThttp://www.shnenglu.com/huangyi5209/articles/141190.htmlhttp://www.shnenglu.com/huangyi5209/comments/141190.htmlhttp://www.shnenglu.com/huangyi5209/articles/141190.html#Feedback0http://www.shnenglu.com/huangyi5209/comments/commentRss/141190.htmlhttp://www.shnenglu.com/huangyi5209/services/trackbacks/141190.html
#include <windows.h>
#include 
<iostream>

using namespace std;

static int g_n;
CRITICAL_SECTION m_cs;

UINT ThreadOne(LPVOID lParam)
{
    EnterCriticalSection(
&m_cs);

    
for (int i = 0; i < 10; i++)
    
{
        g_n
++;
        cout
<<"Thread 1:"<<g_n<<"\n";
    }

    LeaveCriticalSection(
&m_cs);
    
return 0;
}


UINT ThreadTwo(LPVOID lParam)
{
    EnterCriticalSection(
&m_cs);

    
for (int i = 0; i < 10; i++)
    
{
        g_n
++;
        cout
<<"Thread 2:"<<g_n<<"\n";
    }

    LeaveCriticalSection(
&m_cs);
    
return 0;
}


int main()
{
    HANDLE hThrd[
2];
    DWORD IDThread1,IDThread2;

    InitializeCriticalSection(
&m_cs);
    
    hThrd[
0= CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadOne, (LPVOID)NULL, 0&IDThread1);
    hThrd[
1= CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadTwo, (LPVOID)NULL, 0&IDThread1);

    WaitForMultipleObjects(
2, hThrd, TRUE, INFINITE);
    DeleteCriticalSection(
&m_cs);

    system(
"pause");
    
return 0;
}

綰跨▼鍚屾(for MFC)
#include "win32_mfc.h"
#include 
<afxmt.h>
#include 
<iostream>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// 鍞竴鐨勫簲鐢ㄧ▼搴忓璞?/span>

CWinApp theApp;

using namespace std;

CCriticalSection c_s;
static int g_C;

UINT ThreadFunction1(LPVOID lParam)
{
    CSingleLock 
lock(&c_s);

    
lock.Lock();

    
for (int i = 0; i < 10; i++)
    
{
        g_C
++;
        cout
<<  "Thread 1 : " << g_C << endl;
    }

    
lock.Unlock();
    
return 0;
}


UINT ThreadFunction2(LPVOID lParam)
{
    CSingleLock 
lock(&c_s);

    
lock.Lock();

    
for (int i = 0; i < 10; i++)
    
{
        g_C
++;
        cout
<<  "Thread 2 : " << g_C << endl;
    }

    
lock.Unlock();
    
return 0;
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    
int nRetCode = 0;

    
// 鍒濆鍖?nbsp;MFC 騫跺湪澶辮觸鏃舵樉紺洪敊璇?/span>
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    
{
        
// TODO: 鏇存敼閿欒浠g爜浠ョ鍚堟?zhèn)ㄧ殑闇瑕?/span>
        _tprintf(_T("閿欒: MFC 鍒濆鍖栧け璐n"));
        nRetCode 
= 1;
    }

    
else
    
{
        
// TODO: 鍦ㄦ澶勪負(fù)搴旂敤紼嬪簭鐨勮涓虹紪鍐欎唬鐮併?/span>
        CWinThread *Thread[2];
        HANDLE hand[
2];

        Thread[
0= AfxBeginThread(ThreadFunction1, (LPVOID)NULL);
        Thread[
1= AfxBeginThread(ThreadFunction2, (LPVOID)NULL);

        
for (int i = 0; i < 2; i++)
            hand[i] 
= Thread[i]->m_hThread;

        WaitForMultipleObjects(
2, hand, TRUE, INFINITE);
    }


    system(
"pause");
    
return nRetCode;
}




]]>
How to use WIN32 Event Kernel Object 浜嬩歡鍐呮牳瀵硅薄鐨勪嬌鐢?/title><link>http://www.shnenglu.com/huangyi5209/articles/141142.html</link><dc:creator>huangyi5209</dc:creator><author>huangyi5209</author><pubDate>Sat, 05 Mar 2011 00:30:00 GMT</pubDate><guid>http://www.shnenglu.com/huangyi5209/articles/141142.html</guid><wfw:comment>http://www.shnenglu.com/huangyi5209/comments/141142.html</wfw:comment><comments>http://www.shnenglu.com/huangyi5209/articles/141142.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/huangyi5209/comments/commentRss/141142.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/huangyi5209/services/trackbacks/141142.html</trackback:ping><description><![CDATA[<br>鑷姩閲嶇疆浜嬩歡鍐呮牳瀵硅薄<br><br> <div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">stdafx.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">windows.h</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">DWORD WINAPI Tf()<br><img id=Codehighlighter1_102_452_Open_Image onclick="this.style.display='none'; Codehighlighter1_102_452_Open_Text.style.display='none'; Codehighlighter1_102_452_Closed_Image.style.display='inline'; Codehighlighter1_102_452_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_102_452_Closed_Image onclick="this.style.display='none'; Codehighlighter1_102_452_Closed_Text.style.display='none'; Codehighlighter1_102_452_Open_Image.style.display='inline'; Codehighlighter1_102_452_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_102_452_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_102_452_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">thread instantiated<img src="http://www.shnenglu.com/Images/dot.gif"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    HANDLE hEvent </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> OpenEvent(EVENT_ALL_ACCESS, FALSE, __T(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">MyEvent</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">hEvent)<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> counter </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; counter </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">; counter</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">) </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">char</span><span style="COLOR: #008000"><br><img id=Codehighlighter1_298_377_Open_Image onclick="this.style.display='none'; Codehighlighter1_298_377_Open_Text.style.display='none'; Codehighlighter1_298_377_Closed_Image.style.display='inline'; Codehighlighter1_298_377_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_298_377_Closed_Image onclick="this.style.display='none'; Codehighlighter1_298_377_Closed_Text.style.display='none'; Codehighlighter1_298_377_Open_Image.style.display='inline'; Codehighlighter1_298_377_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="COLOR: #000000">    </span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_298_377_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_298_377_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        WaitForSingleObject(hEvent, INFINITE);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Got The Single<img src="http://www.shnenglu.com/Images/dot.gif"></span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">    }</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    CloseHandle(hEvent);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">End of the Thread<img src="http://www.shnenglu.com/Images/dot.gif"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><br><img id=Codehighlighter1_465_1219_Open_Image onclick="this.style.display='none'; Codehighlighter1_465_1219_Open_Text.style.display='none'; Codehighlighter1_465_1219_Closed_Image.style.display='inline'; Codehighlighter1_465_1219_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_465_1219_Closed_Image onclick="this.style.display='none'; Codehighlighter1_465_1219_Closed_Text.style.display='none'; Codehighlighter1_465_1219_Open_Image.style.display='inline'; Codehighlighter1_465_1219_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main()</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_465_1219_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_465_1219_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">    Create an Auto Reset Event which automatically reset to <br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">    Non Signalled state after being signalled</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">    HANDLE hEvent </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> CreateEvent(NULL, FALSE, FALSE, __T(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">MyEvent</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">hEvent)<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    DWORD Id;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    HANDLE hThrd </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> CreateThread(NULL, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, (LPTHREAD_START_ROUTINE)Tf, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">Id);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">hThrd)<br><img id=Codehighlighter1_773_813_Open_Image onclick="this.style.display='none'; Codehighlighter1_773_813_Open_Text.style.display='none'; Codehighlighter1_773_813_Closed_Image.style.display='inline'; Codehighlighter1_773_813_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_773_813_Closed_Image onclick="this.style.display='none'; Codehighlighter1_773_813_Closed_Text.style.display='none'; Codehighlighter1_773_813_Open_Image.style.display='inline'; Codehighlighter1_773_813_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif">    </span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_773_813_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_773_813_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">         CloseHandle(hThrd);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">         </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">    }</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Wait for a while before continuing<img src="http://www.shnenglu.com/Images/dot.gif">.</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">    Sleep(</span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> counter </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; counter </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">; counter</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_922_1035_Open_Image onclick="this.style.display='none'; Codehighlighter1_922_1035_Open_Text.style.display='none'; Codehighlighter1_922_1035_Closed_Image.style.display='inline'; Codehighlighter1_922_1035_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_922_1035_Closed_Image onclick="this.style.display='none'; Codehighlighter1_922_1035_Closed_Text.style.display='none'; Codehighlighter1_922_1035_Open_Image.style.display='inline'; Codehighlighter1_922_1035_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif">    </span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_922_1035_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_922_1035_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Signal the event</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">        SetEvent(hEvent);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> wait for some time before giving another signal</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">        Sleep(</span><span style="COLOR: #000000">2000</span><span style="COLOR: #000000">);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">    }</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Wait for the Thread to Die</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">    WaitForSingleObject(hThrd, INFINITE);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    CloseHandle(hThrd);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    CloseHandle(hEvent);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    <br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">End of Main <img src="http://www.shnenglu.com/Images/dot.gif"><img src="http://www.shnenglu.com/Images/dot.gif">..</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div> <br>鎵嬪姩閲嶇疆浜嬩歡鍐呮牳瀵硅薄<br> <div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">DWORD WINAPI Tf()<br><img id=Codehighlighter1_18_465_Open_Image onclick="this.style.display='none'; Codehighlighter1_18_465_Open_Text.style.display='none'; Codehighlighter1_18_465_Closed_Image.style.display='inline'; Codehighlighter1_18_465_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_18_465_Closed_Image onclick="this.style.display='none'; Codehighlighter1_18_465_Closed_Text.style.display='none'; Codehighlighter1_18_465_Open_Image.style.display='inline'; Codehighlighter1_18_465_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_18_465_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_18_465_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">thread instantiated<img src="http://www.shnenglu.com/Images/dot.gif"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    HANDLE hEvent </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> OpenEvent(EVENT_ALL_ACCESS, FALSE, __T(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">MyEvent</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">hEvent)<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> counter </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; counter </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">; counter</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">) </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">char</span><span style="COLOR: #008000"><br><img id=Codehighlighter1_214_390_Open_Image onclick="this.style.display='none'; Codehighlighter1_214_390_Open_Text.style.display='none'; Codehighlighter1_214_390_Closed_Image.style.display='inline'; Codehighlighter1_214_390_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_214_390_Closed_Image onclick="this.style.display='none'; Codehighlighter1_214_390_Closed_Text.style.display='none'; Codehighlighter1_214_390_Open_Image.style.display='inline'; Codehighlighter1_214_390_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="COLOR: #000000">    </span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_214_390_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_214_390_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        WaitForSingleObject(hEvent, INFINITE);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> We need to reset the event since the event is manual reset<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">    event</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">        ResetEvent(hEvent);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Got The Single<img src="http://www.shnenglu.com/Images/dot.gif"></span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">    }</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    CloseHandle(hEvent);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">End of the Thread<img src="http://www.shnenglu.com/Images/dot.gif"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><br><img id=Codehighlighter1_478_1221_Open_Image onclick="this.style.display='none'; Codehighlighter1_478_1221_Open_Text.style.display='none'; Codehighlighter1_478_1221_Closed_Image.style.display='inline'; Codehighlighter1_478_1221_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_478_1221_Closed_Image onclick="this.style.display='none'; Codehighlighter1_478_1221_Closed_Text.style.display='none'; Codehighlighter1_478_1221_Open_Image.style.display='inline'; Codehighlighter1_478_1221_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main()</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_478_1221_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_478_1221_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">    Create an Manual Reset Event where events must be reset <br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">    manually to non signalled state</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">    HANDLE hEvent </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> CreateEvent(NULL, TRUE, FALSE, __T(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">MyEvent</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">hEvent)<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    DWORD Id;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    HANDLE hThrd </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> CreateThread(NULL, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, (LPTHREAD_START_ROUTINE)Tf, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">Id);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">hThrd)<br><img id=Codehighlighter1_775_815_Open_Image onclick="this.style.display='none'; Codehighlighter1_775_815_Open_Text.style.display='none'; Codehighlighter1_775_815_Closed_Image.style.display='inline'; Codehighlighter1_775_815_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_775_815_Closed_Image onclick="this.style.display='none'; Codehighlighter1_775_815_Closed_Text.style.display='none'; Codehighlighter1_775_815_Open_Image.style.display='inline'; Codehighlighter1_775_815_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif">    </span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_775_815_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_775_815_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">         CloseHandle(hThrd);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">         </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">    }</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Wait for a while before continuing<img src="http://www.shnenglu.com/Images/dot.gif">.</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">    Sleep(</span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> counter </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; counter </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">; counter</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_924_1037_Open_Image onclick="this.style.display='none'; Codehighlighter1_924_1037_Open_Text.style.display='none'; Codehighlighter1_924_1037_Closed_Image.style.display='inline'; Codehighlighter1_924_1037_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_924_1037_Closed_Image onclick="this.style.display='none'; Codehighlighter1_924_1037_Closed_Text.style.display='none'; Codehighlighter1_924_1037_Open_Image.style.display='inline'; Codehighlighter1_924_1037_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif">    </span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_924_1037_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_924_1037_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Signal the event</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">        SetEvent(hEvent);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> wait for some time before giving another signal</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">        Sleep(</span><span style="COLOR: #000000">2000</span><span style="COLOR: #000000">);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">    }</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Wait for the Thread to Die</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">    WaitForSingleObject(hThrd, INFINITE);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    CloseHandle(hThrd);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    CloseHandle(hEvent);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    <br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">End of Main <img src="http://www.shnenglu.com/Images/dot.gif"><img src="http://www.shnenglu.com/Images/dot.gif">..</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div> <img src ="http://www.shnenglu.com/huangyi5209/aggbug/141142.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/huangyi5209/" target="_blank">huangyi5209</a> 2011-03-05 08:30 <a href="http://www.shnenglu.com/huangyi5209/articles/141142.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>ASCII 杞?UNICODEhttp://www.shnenglu.com/huangyi5209/articles/141141.htmlhuangyi5209huangyi5209Fri, 04 Mar 2011 23:57:00 GMThttp://www.shnenglu.com/huangyi5209/articles/141141.htmlhttp://www.shnenglu.com/huangyi5209/comments/141141.htmlhttp://www.shnenglu.com/huangyi5209/articles/141141.html#Feedback0http://www.shnenglu.com/huangyi5209/comments/commentRss/141141.htmlhttp://www.shnenglu.com/huangyi5209/services/trackbacks/141141.html 

wchar_t *ascii_to_unicode(const char *resource)
{
    
const char* sp;
    wchar_t
* result, *rp;
     unsigned length 
= strlen(resource) + 1;
     
if (!resource)
         
return NULL;
    
     result 
= (wchar_t*)malloc(length * sizeof(wchar_t));
     
if (result)
     
{
         
for (sp = resource, rp = result; *sp; sp++, rp++)
             
*rp = *sp;
         
*rp = 0;
     }


     
return result;
}


int main(){
    
char ch[] = "C:\\data\\\\test230.ext";
    wchar_t
* wchr = ascii_to_unicode(ch);

    free(wchr);
    system(
"pause");
    
return 0;
}


]]>
鍖哄垎C++鐨勬寚閽堝拰寮曠敤http://www.shnenglu.com/huangyi5209/articles/139240.htmlhuangyi5209huangyi5209Mon, 24 Jan 2011 12:29:00 GMThttp://www.shnenglu.com/huangyi5209/articles/139240.htmlhttp://www.shnenglu.com/huangyi5209/comments/139240.htmlhttp://www.shnenglu.com/huangyi5209/articles/139240.html#Feedback0http://www.shnenglu.com/huangyi5209/comments/commentRss/139240.htmlhttp://www.shnenglu.com/huangyi5209/services/trackbacks/139240.htmlPointers and references look different enough (pointers use the “*” and “->” operators, references use “.“), but they seem to do similar things. Both pointers and references let you refer to other objects indirectly. How, then, do you decide when to use one and not the other?

First, recognize that there is no such thing as a null reference. A reference must always refer to some object. As a result, if you have a variable whose purpose is to refer to another object, but it is possible that there might not be an object to refer to, you should make the variable a pointer, because then you can set it to null. On the other hand, if the variable must alwaysrefer to an object, i.e., if your design does not allow for the possibility that the variable is null, you should probably make the variable a reference.

“But wait,” you wonder, “what about underhandedness like this?”

  1. char *pc = 0;          // set pointer to null   
  2. char& rc = *pc;        // make reference refer to   
  3.                        // dereferenced null pointer  

Well, this is evil, pure and simple. The results are undefined (compilers can generate output to do anything they like), and people who write this kind of code should be shunned until they agree to cease and desist. If you have to worry about things like this in your software, you’re probably best off avoiding references entirely. Either that or finding a better class of programmers to work with. We’ll henceforth ignore the possibility that a reference can be “null.”

Because a reference must refer to an object, C++ requires that references be initialized:

string& rs;             // error! References must   
  1.                         // be initialized   
  2. string s("xyzzy");   
  3. string& rs = s;         // okay, rs refers to s  

Pointers are subject to no such restriction:

  1. string *ps;             // uninitialized pointer:   
  2.                         // valid but risky  

The fact that there is no such thing as a null reference implies that it can be more efficient to use references than to use pointers. That’s because there’s no need to test the validity of a reference before using it:

void printDouble(const double& rd)   
  1. {   
  2.     cout << rd;         // no need to test rd; it   
  3. }                       // must refer to a double  

Pointers, on the other hand, should generally be tested against null:

  1. void printDouble(const double *pd)   
  2. {   
  3. if (pd) {             // check for null pointer   
  4. cout << *pd;   
  5. }   
  6. }  

Another important difference between pointers and references is that pointers may be reassigned to refer to different objects. A reference, however, always refers to the object with which it is initialized:

  1. string s1("Nancy");   
  2. string s2("Clancy");   
  3. string& rs = s1;         // rs refers to s1   
  4.   
  5. string *ps = &s1;        // ps points to s1<A name=31186></A>   
  6.   
  7. rs = s2;                 // rs still refers to s1,   
  8.                          // but s1's value is now   
  9.                          // "Clancy"   
  10.   
  11. ps = &s2;                // ps now points to s2;   
  12.                          // s1 is unchanged  

In general, you should use a pointer whenever you need to take into account the possibility that there’s nothing to refer to (in which case you can set the pointer to null) or whenever you need to be able to refer to different things at different times (in which case you can change where the pointer points). You should use a reference whenever you know there will always be an object to refer to and you also know that once you’re referring to that object, you’ll never want to refer to anything else.

References, then, are the feature of choice when you know you have something to refer to, when you’ll never want to refer to anything else, and when implementing operators whose syntactic requirements make the use of pointers undesirable. In all other cases, stick with pointers.



]]>
Callback Functions Tutorial - C++鐨勫洖璋冨嚱鏁版満鍒?/title><link>http://www.shnenglu.com/huangyi5209/articles/139239.html</link><dc:creator>huangyi5209</dc:creator><author>huangyi5209</author><pubDate>Mon, 24 Jan 2011 12:26:00 GMT</pubDate><guid>http://www.shnenglu.com/huangyi5209/articles/139239.html</guid><wfw:comment>http://www.shnenglu.com/huangyi5209/comments/139239.html</wfw:comment><comments>http://www.shnenglu.com/huangyi5209/articles/139239.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/huangyi5209/comments/commentRss/139239.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/huangyi5209/services/trackbacks/139239.html</trackback:ping><description><![CDATA[<div class="xjzfv9b" id="display_content" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 13px; line-height: 18px; "><h3 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; font-weight: bold; color: rgb(0, 0, 63); ">Introduction</h3><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">If you are reading this article, you probably wonder what callback functions are. This article explains what callback functions are, what are they good for, why you should use them, and so forth. However, before learning what callback functions are, you must be familiar with function pointers. If you aren't, consult a C/C++ book or consider reading the following:</p><ul style="margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><a target="new" style="color: rgb(5, 48, 97); text-decoration: underline !important; ">The Syntax of C and C++ Function Pointers</a></li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><a target="new" style="color: rgb(5, 48, 97); text-decoration: underline !important; ">Pointers to member functions</a></li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><a target="new" style="color: rgb(5, 48, 97); text-decoration: underline !important; ">Declaring, Assigning, and Using Function Pointers</a></li></ul><h3 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; font-weight: bold; color: rgb(0, 0, 63); ">What Is a Callback Function?</h3><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">The simple answer to this first question is that a callback function is a function that is called through a function pointer. If you pass the pointer (address) of a function as an argument to another, when that pointer is used to call the function it points to it is said that a call back is made.</p><h3 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; font-weight: bold; color: rgb(0, 0, 63); ">Why Should You Use Callback Functions?</h3><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">Because they uncouple the caller from the callee. The caller doesn't care who the callee is; all it knows is that there is a callee with a certain prototype and probably some restriction (for instance, the returned value can be int, but certain values have certain meanings).</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">If you are wondering how is that useful in practice, imagine that you want to write a library that provides implementation for sorting algorithms (yes, that is pretty classic), such as bubble sort, shell short, shake sort, quick sort, and others. The catch is that you don't want to embed the sorting logic (which of two elements goes first in an array) into your functions, making your library more general to use. You want the client to be responsible to that kind of logic. Or, you want it to be used for various data types (ints, floats, strings, and so on). So, how do you do it? You use function pointers and make callbacks.</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">A callback can be used for notifications. For instance, you need to set a timer in your application. Each time the timer expires, your application must be notified. But, the implementer of the time'rs mechanism doesn't know anything about your application. It only wants a pointer to a function with a given prototype, and in using that pointer it makes a callback, notifying your application about the event that has occurred. Indeed, the SetTimer() WinAPI uses a callback function to notify that the timer has expired (and, in case there is no callback function provided, it posts a message to the application's queue).</p><div id="1xzp3x1" class="toolbox noBullets colRight" style="margin-top: 15px; margin-right: 0px; margin-bottom: 5px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; float: right; width: 160px; border-left-color: rgb(204, 204, 204); font-size: 11px; clear: both; "><ul style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 5px; "><li id="3fxbj1x" class="comment_linky" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 25px; list-style-type: none; list-style-position: outside; list-style-image: initial; background-image: url(http://www.codeguru.com/newimg/images/icon_comment.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 50%; background-repeat: no-repeat no-repeat; "><a rel="nofollow" style="color: rgb(5, 48, 97); text-decoration: underline !important; font-weight: bold; "><nobr>Post a comment</nobr></a></li><li id="73njnzn" class="email" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 25px; list-style-type: none; list-style-position: outside; list-style-image: initial; background-image: url(http://www.codeguru.com/newimg/images/icon_email.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 50%; background-repeat: no-repeat no-repeat; "><a rel="nofollow" style="color: rgb(5, 48, 97); text-decoration: underline !important; font-weight: bold; ">Email Article</a></li><li id="nbphz9j" class="print" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 25px; list-style-type: none; list-style-position: outside; list-style-image: initial; background-image: url(http://www.codeguru.com/newimg/images/icon_print.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 50%; background-repeat: no-repeat no-repeat; "><a rel="nofollow" style="color: rgb(5, 48, 97); text-decoration: underline !important; font-weight: bold; ">Print Article</a></li><li id="toolBoxShareMenu" class="share" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: -12px; padding-top: 10px; padding-right: 0px; padding-bottom: 10px; padding-left: 12px; list-style-type: none; list-style-position: outside; list-style-image: initial; background-image: url(http://www.codeguru.com/newimg/images/share_article-bg.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; z-index: 100; position: relative; background-position: 0px -1000px; background-repeat: no-repeat no-repeat; "><img height="16" width="16" class="icon" alt="" src="http://www.codeguru.com/newimg/images/icon_share.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: middle; margin-top: 0px; margin-right: 5px; margin-bottom: 2px; margin-left: 0px; "> <a rel="nofollow" style="color: rgb(5, 48, 97); text-decoration: underline !important; font-weight: bold; ">Share Articles</a><img src="http://www.codeguru.com/newimg/images/arrow_down_spblue.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: middle; margin-top: 0px; margin-right: 5px; margin-bottom: 2px; margin-left: 0px; "></li></ul></div><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">Another example from WinAPI functions that use callback mechanism is EnumWindow(), which enumerates all the top-level windows on the screen. EnumWindow() iterates over the top-level windows, calling an application-provided function for each window, passing the handler of the window. If the callee returns a value, the iteration continues; otherwise, it stops. EnumWindows() just doesn't care where the callee is and what it does with the handler it passes over. It is only interested in the return value, because based on that it continues its execution or not.</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">However, callback functions are inherited from C. Thus, in C++, they should be only used for interfacing C code and existing callback interfaces. Except for these situations, you should use virtual methods or functors, not callback functions.</p><h3 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; font-weight: bold; color: rgb(0, 0, 63); ">A Simple Implementation Example</h3><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">Now, follow the example that can be found in the attached files. I have created a dynamic linked library called sort.dll. It exports a type called CompareFunction:</p><pre style="margin-top: 3px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; font-family: 'courier new', monospace; background-image: url(http://www.codeguru.com/newimg/images/paperbg.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(245, 243, 215); border-right-color: rgb(245, 243, 215); border-bottom-color: rgb(245, 243, 215); border-left-color: rgb(245, 243, 215); color: rgb(0, 0, 0); width: 570px; display: block; font-size: 13px; line-height: 16px; overflow-x: auto; min-height: 30px; background-position: 0px 0px; background-repeat: repeat repeat; ">typedef int (__stdcall *CompareFunction)(const byte*, const byte*);</pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">which will be the type of your callback functions. It also exports two methods, called Bubblesort() and Quicksort(), which have the same prototype but provide different behavior by implementing the sorting algorithms with the same name.</p><pre style="margin-top: 3px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; font-family: 'courier new', monospace; background-image: url(http://www.codeguru.com/newimg/images/paperbg.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(245, 243, 215); border-right-color: rgb(245, 243, 215); border-bottom-color: rgb(245, 243, 215); border-left-color: rgb(245, 243, 215); color: rgb(0, 0, 0); width: 570px; display: block; font-size: 13px; line-height: 16px; overflow-x: auto; min-height: 30px; background-position: 0px 0px; background-repeat: repeat repeat; ">void DLLDIR __stdcall Bubblesort(byte* array, int size, int elem_size, CompareFunction cmpFunc); void DLLDIR __stdcall Quicksort(byte* array, int size, int elem_size, CompareFunction cmpFunc); </pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">These two functions take the following parameters:</p><ul style="margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><em>byte* array</em>: a pointer to an array of elements (doesn't matter of which type)</li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><em>int size</em>: the number of elements in the array</li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><em>int elem_size</em>: the size, in bytes, of an element of the array</li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><em>CompareFunction cmpFunc</em>: a pointer to a callback function with the prototype listed above</li></ul><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">The implementation of these two functions performs a sorting of the array. But, each time there is a need to decide which of two elements goes first, a callback is made to the function whose address was passed as an argument. For the library writer, it doesn't matter where that function is implemented, or how it is implemented. All that matters it is that it takes the address of two elements (that are the two be compared) and it returns one of the following values (this is a contract between the library developers and its clients):</p><ul style="margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; ">-1: if the first element is lesser and/or should go before the second element (in a sorted array)</li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; ">0: if the two elements are equal and/or their relative position doesn't matter (each one can go before the other in a sorted array)</li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; ">1: if the first element is greater and/or should go after the second element (in a sorted array)</li></ul><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">With this contract explicitly stated, the implementation of the Bubblesort() function is this (for Quicksort(), which a little bit more complicated, see the attached files).</p><pre style="margin-top: 3px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; font-family: 'courier new', monospace; background-image: url(http://www.codeguru.com/newimg/images/paperbg.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(245, 243, 215); border-right-color: rgb(245, 243, 215); border-bottom-color: rgb(245, 243, 215); border-left-color: rgb(245, 243, 215); color: rgb(0, 0, 0); width: 570px; display: block; font-size: 13px; line-height: 16px; overflow-x: auto; min-height: 30px; background-position: 0px 0px; background-repeat: repeat repeat; ">void DLLDIR __stdcall Bubblesort(byte* array, int size, int elem_size, CompareFunction cmpFunc) { for(int i=0; i < size; i++) { for(int j=0; j < size-1; j++) { // make the callback to the comparison function if(1 == (*cmpFunc)(array+j*elem_size, array+(j+1)*elem_size)) { // the two compared elements must be interchanged byte* temp = new byte[elem_size]; memcpy(temp, array+j*elem_size, elem_size); memcpy(array+j*elem_size, array+(j+1)*elem_size, elem_size); memcpy(array+(j+1)*elem_size, temp, elem_size); delete [] temp; } } } } </pre><blockquote style="margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 1em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><strong>Note</strong>: Because the implementation uses memcpy(), these library functions should not be used for types other than POD (Plain-Old-Data).</blockquote><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">On the client side, there must be a callback function whose address is to be passed to the Bubblesort() function. As a simple example, I have written a function that compares two integer values and one that compares two strings:</p><pre style="margin-top: 3px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; font-family: 'courier new', monospace; background-image: url(http://www.codeguru.com/newimg/images/paperbg.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(245, 243, 215); border-right-color: rgb(245, 243, 215); border-bottom-color: rgb(245, 243, 215); border-left-color: rgb(245, 243, 215); color: rgb(0, 0, 0); width: 570px; display: block; font-size: 13px; line-height: 16px; overflow-x: auto; min-height: 30px; background-position: 0px 0px; background-repeat: repeat repeat; ">int __stdcall CompareInts(const byte* velem1, const byte* velem2) { int elem1 = *(int*)velem1; int elem2 = *(int*)velem2; if(elem1 < elem2) return -1; if(elem1 > elem2) return 1; return 0; } int __stdcall CompareStrings(const byte* velem1, const byte* velem2) { const char* elem1 = (char*)velem1; const char* elem2 = (char*)velem2; return strcmp(elem1, elem2); } </pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">To put all these to a test, I have written this short program. It passes an array with five elements to Bubblesort() or Quicksort() along with the pointer to the callback functions.</p><pre style="margin-top: 3px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; font-family: 'courier new', monospace; background-image: url(http://www.codeguru.com/newimg/images/paperbg.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(245, 243, 215); border-right-color: rgb(245, 243, 215); border-bottom-color: rgb(245, 243, 215); border-left-color: rgb(245, 243, 215); color: rgb(0, 0, 0); width: 570px; display: block; font-size: 13px; line-height: 16px; overflow-x: auto; min-height: 30px; background-position: 0px 0px; background-repeat: repeat repeat; ">int main(int argc, char* argv[]) { int i; int array[] = {5432, 4321, 3210, 2109, 1098}; cout << "Before sorting ints with Bubblesort\n"; for(i=0; i < 5; i++) cout << array[i] << '\n'; Bubblesort((byte*)array, 5, sizeof(array[0]), &CompareInts); cout << "After the sorting\n"; for(i=0; i < 5; i++) cout << array[i] << '\n'; const char str[5][10] = {"estella", "danielle", "crissy", "bo", "angie"}; cout << "Before sorting strings with Quicksort\n"; for(i=0; i < 5; i++) cout << str[i] << '\n'; Quicksort((byte*)str, 5, 10, &CompareStrings); cout << "After the sorting\n"; for(i=0; i < 5; i++) cout << str[i] << '\n'; return 0; } </pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">If I decide that I want the sorting to be done descending (with the biggest element first), all I have to do is to change the callback function code, or provide another that implements the desired logic.</p><h3 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; font-weight: bold; color: rgb(0, 0, 63); ">Calling Conventions</h3><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">In the above code, you can see the word <em>__stdcall</em> in the function's prototype. Because it starts with a double underscore, it is, of course, a compiler-specific extension, more exactly a Microsoft-specific one. Any compiler that supports development of Win32-based applications must support this or an equivalent one. A function that is marked with <em>__stdcall</em> uses the standard calling convention so named because all Win32 API functions (except the few that take variable arguments) use it. Functions that follow the standard calling convention remove the parameters from the stack before they return to the caller. This is the standard convention for Pascal. But in C/C++, the calling convention is that the caller cleans up the stack instead of the called function. To enforce that a function uses the C/C++ calling convention, <em>__cdecl</em>must be used. Variable argument functions use the C/C++ calling convention.</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">Windows adopted the standard calling convention (Pascal convention) because it reduces the size of the code. This was very important in the early days of Windows, when it ran on systems with 640 KB RAM.</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">If you don't like the word <em>__stdcall</em>, you can use the <strong>CALLBACK</strong> macro, defined in windef.h, as</p><pre style="margin-top: 3px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; font-family: 'courier new', monospace; background-image: url(http://www.codeguru.com/newimg/images/paperbg.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(245, 243, 215); border-right-color: rgb(245, 243, 215); border-bottom-color: rgb(245, 243, 215); border-left-color: rgb(245, 243, 215); color: rgb(0, 0, 0); width: 570px; display: block; font-size: 13px; line-height: 16px; overflow-x: auto; min-height: 30px; background-position: 0px 0px; background-repeat: repeat repeat; ">#define CALLBACK __stdcall</pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">or</p><pre style="margin-top: 3px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; font-family: 'courier new', monospace; background-image: url(http://www.codeguru.com/newimg/images/paperbg.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(245, 243, 215); border-right-color: rgb(245, 243, 215); border-bottom-color: rgb(245, 243, 215); border-left-color: rgb(245, 243, 215); color: rgb(0, 0, 0); width: 570px; display: block; font-size: 13px; line-height: 16px; overflow-x: auto; min-height: 30px; background-position: 0px 0px; background-repeat: repeat repeat; ">#define CALLBACK PASCAL</pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">where PASCAL is #defined as __stdcall.</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">You can read more about calling convention here: <a target="new" style="color: rgb(5, 48, 97); text-decoration: underline !important; ">Calling Convetions in Microsoft Visual C++</a>.</p><h3 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; font-weight: bold; color: rgb(0, 0, 63); ">C++ Methods as Callback Functions</h3><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">Because you probably write in C++, you want your callback function a method of a class. But, if you try this:</p><pre style="margin-top: 3px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; font-family: 'courier new', monospace; background-image: url(http://www.codeguru.com/newimg/images/paperbg.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(245, 243, 215); border-right-color: rgb(245, 243, 215); border-bottom-color: rgb(245, 243, 215); border-left-color: rgb(245, 243, 215); color: rgb(0, 0, 0); width: 570px; display: block; font-size: 13px; line-height: 16px; overflow-x: auto; min-height: 30px; background-position: 0px 0px; background-repeat: repeat repeat; ">class CCallbackTester { public: int CALLBACK CompareInts(const byte* velem1, const byte* velem2); }; Bubblesort((byte*)array, 5, sizeof(array[0]), &CCallbackTester::CompareInts); </pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">with a MS compiler, you get this compilation error:</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; "><em>error C2664: 'Bubblesort' : cannot convert parameter 4 from 'int (__stdcall CCallbackTester::*)(const unsigned char *,const unsigned char *)' to 'int (__stdcall *)(const unsigned char *,const unsigned char *)' There is no context in which this conversion is possible</em></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">That happens because non-static member functions have an additional parameter, pointer <em>this</em> (see this<a target="new" style="color: rgb(5, 48, 97); text-decoration: underline !important; ">FAQ</a> for more).</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">That obliges you to make the member function static. If that's not acceptable, you can use several techniques to overcome that. Check the following links to learn more.</p><ul style="margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><a target="new" style="color: rgb(5, 48, 97); text-decoration: underline !important; ">How to Implement Callbacks in C and C++</a></li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><a target="new" style="color: rgb(5, 48, 97); text-decoration: underline !important; ">C++ Callback Demo</a></li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><a target="new" style="color: rgb(5, 48, 97); text-decoration: underline !important; ">Callbacks in C++ Using Template Functors</a></li></ul><h3 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; font-weight: bold; color: rgb(0, 0, 63); ">Notices</h3><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; ">The attached files contain two projects. <em>SortingDLL</em> is a Win32 DLL project. The sort.dll output library exports the two sorting functions, Bubblesort() and Quicksort(). The second project, <em>SortDemo</em>, is a Win32 Console Application that demonstrates how to use the sort.dll library. The output directory for both projects is <em>Shared</em> directory, where the following files can be found: sort.h, sort.dll, sort.lib, and SortDemo.exe.</p><h3 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 13px; font-weight: bold; color: rgb(0, 0, 63); ">Further References</h3><ul style="margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 2em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><a target="new" style="color: rgb(5, 48, 97); text-decoration: underline !important; ">C++ OO Callback Technique</a></li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><a target="new" style="color: rgb(5, 48, 97); text-decoration: underline !important; ">Function Pointers to Non-Static Object Methods</a></li><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: disc; list-style-position: outside; list-style-image: initial; "><a target="new" style="color: rgb(5, 48, 97); text-decoration: underline !important; ">Pointers and References</a></li></ul></div><h2 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 18px; font-weight: bold; color: rgb(0, 0, 63); line-height: 1.5em; font-family: Arial, Helvetica, sans-serif; ">About the Author</h2><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 13px; line-height: 18px; ">Marius Bancila is a Microsoft MVP for VC++. He works as a software developer for a Norwegian-based company. He is mainly focused on building desktop applications with MFC and VC#. He keeps a blog at www.mariusbancila.ro/blog, focused on Windows programming. He is the co-founder of codexpert.ro, a community for Romanian C++/VC++ programmers.<br style="font-size: 12px; line-height: 1em; "><br style="font-size: 12px; line-height: 1em; "></div><h2 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 18px; font-weight: bold; color: rgb(0, 0, 63); line-height: 1.5em; font-family: Arial, Helvetica, sans-serif; ">Downloads</h2><li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; font-family: Arial, Helvetica, sans-serif; font-size: 13px; line-height: 18px; "><a style="color: rgb(5, 48, 97); text-decoration: underline !important; ">callbacks.zip</a></li> <img src ="http://www.shnenglu.com/huangyi5209/aggbug/139239.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/huangyi5209/" target="_blank">huangyi5209</a> 2011-01-24 20:26 <a href="http://www.shnenglu.com/huangyi5209/articles/139239.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>感谢您访问我们的网站,您可能还对以下资源感兴趣:</p> <a href="http://www.shnenglu.com/" title="精品视频久久久久">精品视频久久久久</a> <div class="friend-links"> </div> </div> </footer> <a href="http://www.rc51.cn" target="_blank">久久天天日天天操综合伊人av</a>| <a href="http://www.qtvc.cn" target="_blank">久久亚洲精品国产精品婷婷 </a>| <a href="http://www.123oye.cn" target="_blank">国产成人无码精品久久久久免费 </a>| <a href="http://www.odbeqi.cn" target="_blank">国产精品久久久久久五月尺</a>| <a href="http://www.dachanzui.cn" target="_blank">思思久久好好热精品国产</a>| <a href="http://www.chaikuo.cn" target="_blank">免费精品久久天干天干</a>| <a href="http://www.baicar.cn" target="_blank">亚洲中文精品久久久久久不卡</a>| <a href="http://www.dlwz8.cn" target="_blank">欧美777精品久久久久网</a>| <a href="http://www.akq33.cn" target="_blank">国产精品美女久久久免费</a>| <a href="http://www.lawyer010.cn" target="_blank">久久男人中文字幕资源站</a>| <a href="http://www.job158.cn" target="_blank">久久精品免费全国观看国产</a>| <a href="http://www.onsj.cn" target="_blank">亚洲国产另类久久久精品</a>| <a href="http://www.ejjn.cn" target="_blank">丁香五月网久久综合</a>| <a href="http://www.tjjobs.com.cn" target="_blank">国产日韩久久免费影院</a>| <a href="http://www.logeng.cn" target="_blank">久久精品免费全国观看国产</a>| <a href="http://www.122797929.cn" target="_blank">久久久免费精品re6</a>| <a href="http://www.shaikr.cn" target="_blank">中文字幕成人精品久久不卡</a>| <a href="http://www.wwwh0930com.cn" target="_blank">日日狠狠久久偷偷色综合免费 </a>| <a href="http://www.hhabg.com.cn" target="_blank">色综合久久综合中文综合网</a>| <a href="http://www.21gou.cn" target="_blank">亚洲精品国精品久久99热一</a>| <a href="http://www.uqknet.cn" target="_blank">国产精品久久久天天影视</a>| <a href="http://www.milanworld.cn" target="_blank">激情五月综合综合久久69</a>| <a href="http://www.niutuan.com.cn" target="_blank">久久亚洲AV无码精品色午夜麻豆</a>| <a href="http://www.zjzlgs.cn" target="_blank">久久精品www人人爽人人</a>| <a href="http://www.0756400.cn" target="_blank">91秦先生久久久久久久</a>| <a href="http://www.zhongshengwsl.cn" target="_blank">伊人色综合久久天天人守人婷 </a>| <a href="http://www.nxxdz.cn" target="_blank">欧美亚洲另类久久综合婷婷</a>| <a href="http://www.zfrnhbv.com.cn" target="_blank">亚洲av日韩精品久久久久久a</a>| <a href="http://www.smscx.cn" target="_blank">四虎国产精品免费久久久</a>| <a href="http://www.beauty-queen.cn" target="_blank">色8激情欧美成人久久综合电</a>| <a href="http://www.xsq1.cn" target="_blank">久久久久久久久久久久中文字幕</a>| <a href="http://www.hbswmm.cn" target="_blank">久久国产V一级毛多内射</a>| <a href="http://www.ramada-zhengzhou.cn" target="_blank">亚洲国产成人久久精品99</a>| <a href="http://www.jingxi.jx.cn" target="_blank">久久国产精品无码一区二区三区</a>| <a href="http://www.autothink.com.cn" target="_blank">国产精品日韩欧美久久综合</a>| <a href="http://www.ai7c.cn" target="_blank">亚洲国产精品无码久久</a>| <a href="http://www.qcwxfw.cn" target="_blank">国产精品日韩欧美久久综合</a>| <a href="http://www.zx444.cn" target="_blank">久久婷婷激情综合色综合俺也去</a>| <a href="http://www.ptrjmfv.cn" target="_blank">精品久久久无码中文字幕天天</a>| <a href="http://www.bethesdagroup.cn" target="_blank">老色鬼久久亚洲AV综合</a>| <a href="http://www.lttao.cn" target="_blank">亚洲国产香蕉人人爽成AV片久久 </a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>