⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cnewtls.cpp

📁 window下的多线程编程参考书。值得一读
💻 CPP
字号:
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "CMcl.h"

#define NUM_THREADS         10
#define THREAD_LIFETIME     1000

// C++ TLS wrapper class...
class CTls {
private:
    DWORD m_dwTlsIndex;
    DWORD m_dwStatus;

public:
    CTls() {
        m_dwStatus = NO_ERROR;
        m_dwTlsIndex = ::TlsAlloc();
        if (m_dwTlsIndex == 0xFFFFFFFF) {
            m_dwStatus = ::GetLastError();
            CMclThrowError(m_dwStatus);
        }
    };

    ~CTls() {
        if (m_dwStatus == NO_ERROR) {
            ::TlsFree(m_dwTlsIndex);
        }
    };

    DWORD Status(void) {
        return m_dwStatus;
    };

    BOOL SetValue( LPVOID lpTlsValue) {
        return ::TlsSetValue( m_dwTlsIndex, lpTlsValue);
    };

    LPVOID GetValue(void) {
        return ::TlsGetValue( m_dwTlsIndex);
    };
};

// global C++ object for new dynamically allocated TLS...
CTls g_cFooCounter;

void foo(void) {
    // read the current dynamic TLS value...
    DWORD dwValue = (DWORD) g_cFooCounter.GetValue();

    // add one to the count...
    dwValue++;

    // save the new value back to dynamic TLS...
    if (!g_cFooCounter.SetValue((LPVOID) dwValue)) {
        printf( "Error! Unable to increment TLS value, error = %ld.\n", GetLastError());
    }
}

DWORD get_dynamic_TLS_count(void) {
    return (DWORD) g_cFooCounter.GetValue();
}

class CFooThreadHandler : public CMclThreadHandler {
    unsigned ThreadHandlerProc(void) {
        // run the thread for a short time...
        clock_t endtime = clock() + THREAD_LIFETIME;
        while (clock() < endtime) {
            foo();
        }

        // print out the count and thread info...
        printf( "Thread ID=0x%08x foo() call count: %ld (new-dynamic TLS).\n", 
            GetCurrentThreadId(), 
            get_dynamic_TLS_count());
        return 0;
    };
};

int main( int argc, char *argv[]) {
    // no need to allocate the TLS index
    // since it was done in the constructor for the
    // global CTls object g_cFooCounter...
    
    // create some threads that use counted_function()...
    // notice that all the threads use the same instance
    // of the thread handler object...
    int i;
    CMclWaitableCollection cCollection;
    CFooThreadHandler cFooHandler;
    CMclThreadAutoPtr apThread[NUM_THREADS];
    for (i = 0; i < NUM_THREADS; i++) {
        apThread[i] = new CMclThread(&cFooHandler);
        cCollection.AddObject(*apThread[i]);
    }

    // allow the threads to run...
    cCollection.Wait( TRUE, INFINITE);

    // no need to free the TLS index since it will be done when
    // the g_cFooCounter goes out of scope. since this binary is an 
    // EXE this will happen when the program terminates, but if this 
    // was a DLL it would happen when the DLL was unloaded...

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -