zoukankan      html  css  js  c++  java
  • Nokia论坛filelogger中不能直接使用的原因

    [转]How_to_use_Thread_Local_Storage

    (2008-07-23 19:20:22)

    转载

    标签:

    log
    线程本地存储
    dll
    exe
    杂谈

    分类:Symbian

    DLL can manage writable static data on a per-thread basis using thread-local storage, commonly known as ”TLS”. This allocates a single machine word of writable static data per thread for every DLL, regardless of whether the DLL uses it. Obviously, the memory overhead is far less significant than allocating a 4 KB chunk for each DLL which uses static data. However, the price of using TLS instead of Direct Memory Access is performance; data is retrieved from TLS about 30 times slower than direct access, because the lookup involves a context switch to the kernel in order to access the data. Thread-local storage is usually initialized when the DLL is attached to a thread within the DLL entry point, E32Dll().Typically, code is added to construct the struct containing the global data and store it in thread-local storage using the static function Dll::SetTLS().To access the data, you should use the static function Dll::Tls(). This will return a TAny* which can be cast and used to access the data. For simplicity, you may wish to provide a utility function, or set of functions, to access the data from a single point.

    TLS is thread and DLL -specific so every DLL in a thread can use it without risk of mixups.

    Usage example

    // Create an object and store the pointer in TLS
    CSomeClass* object = CSomeClass::NewL();
    User::LeaveIfError( Dll::SetTls( object ) );
    // Get the object from TLS
    object = static_cast<CSomeClass*>( Dll::Tls() );
    // When done with the object, delete it and free TLS
    delete object;
    object = NULL;
    Dll::FreeTls();

    Note! The TLS does not take ownership of the object so the user is responsible for deleting it.

    Usage example in an EXE project

    The above code uses the static Dll class to access the TLS which means it only works in a DLL project. TLS can also be used in an EXE project, but through a different static class. UserSvr provides the same API to TLS except that it requires somekind of an id to the pointer being stored. The Dll class actually uses UserSvr to access TLS provinding the DLL module handle as the id. The same code example in an EXE project would look something like this:

    #include <e32svr.h> // For UserSvr
    // Define a handle. This has to be unique within the thread.
    const TInt KMyTlsHandle = 0xC0FFEE;
    // Create an object and store the pointer in TLS
    CSomeObject* object = CSomeObject::NewL();
    User::LeaveIfError( UserSvr::DllSetTls( KMyTlsHandle, object ) );
    // Get the object from TLS
    object = static_cast<CSomeObject*>( UserSvr::DllTls( KMyTlsHandle ) );
    // When done with the object, delete it and free TLS
    delete object;
    
    

    PS:转上面的目的主要是说说今天遇到的编译错误:

    在我使用自己写的Log的时候,出现以下错误,当然,里面用到了线程本地存储,我写的程序是exe的,当然不能使用 Dll::SetTls的形式了。按照以上方法修改就可以了。

    mwldsym2.exe: Undefined symbol: '__E32Dll'
    mwldsym2.exe: referenced from 'int Dll::SetTls(void *) (?SetTls@Dll@@SAHPAX@Z)'
    in up_dll_tls.cpp:19 (edllstub.lib)
    mwldsym2.exe: referenced from 'void * Dll::Tls(void) (?Tls@Dll@@SAPAXXZ)' in up_
    dll_tls.cpp:28 (edllstub.lib)

  • 相关阅读:
    CentOS 7 修改国内yum源
    k8s 安装
    python2 python3同时安装了scrapy如何区分调用
    scrapy log 设置
    hello django
    linux 分割大文件
    scrapy 对不同的Item进行分开存储
    纯C实现的一套low b 贪吃蛇(娱乐版)
    Python之如何实现一行输入多个值
    HDU2571:命运(DP)
  • 原文地址:https://www.cnblogs.com/yaoliang11/p/1788970.html
Copyright © 2011-2022 走看看