Archive

Posts Tagged ‘C/C++’

C++ Standard (C++0x) Thread Library

March 28th, 2010 No comments

The upcoming C++ standard (C++0x) will support multithreading and concurrency both as an inherent part of the memory model, and as part of the C++ Standard Library. “Multithreading and Concurrency” (Anthony Williams):

Also Herb Sutter is a leading authority on software development. He is the best selling author of several books including Exceptional C++ and C++ Coding Standards, as well as hundreds of technical papers and articles, including “The Free Lunch Is Over” which coined the term “concurrency revolution.”

Effective Concurrency: Prefer Futures to Baked-In “Async APIs”

Qt Smart Pointers

February 7th, 2010 No comments

Andrei Alexandrescu “Iterators Must Go!”

February 6th, 2010 No comments

Andrei Alexandrescu gives his keynote presentation, “Iterators Must Go!” at BoostCon 2009. Slides are available here (pdf).

Permalink

Iterators Must Go (Google Docs)

Load a file to a string list

January 22nd, 2010 No comments
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>

int main(int argc, char* argv[])
{
    std::vector<std::string> v;
    std::ifstream f("e:/test.txt");
    if(f.is_open())
    {
        std::copy(std::istream_iterator<std::string>(f),
                  std::istream_iterator<std::string>(),
                  std::back_inserter(v));
    }
    // проверка
    std::copy(v.begin(), v.end(),
              std::ostream_iterator<std::string>(std::cout, "\n"));

    return 0;
}

rsdn.ru

Convert __DATE__ to unsigned int

January 21st, 2010 No comments
#define YEAR ((((__DATE__ [7] - '0') * 10 + (__DATE__ [8] - '0')) * 10 \
              + (__DATE__ [9] - '0')) * 10 + (__DATE__ [10] - '0'))

#define MONTH (__DATE__ [2] == 'n' ? 0 \
               : __DATE__ [2] == 'b' ? 1 \
               : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 2 : 3) \
               : __DATE__ [2] == 'y' ? 4 \
               : __DATE__ [2] == 'n' ? 5 \
               : __DATE__ [2] == 'l' ? 6 \
               : __DATE__ [2] == 'g' ? 7 \
               : __DATE__ [2] == 'p' ? 8 \
               : __DATE__ [2] == 't' ? 9 \
               : __DATE__ [2] == 'v' ? 10 : 11)

#define DAY ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 + (__DATE__ [5] - '0'))

#define DATE_AS_INT (((YEAR - 2000) * 12 + MONTH) * 31 + DAY)

int main (void)
{
  printf ("%d-%02d-%02d = %d\n", YEAR, MONTH + 1, DAY, DATE_AS_INT);
  return 0;
}

Boost compilation with Visual Studio

November 17th, 2009 No comments

build bjam

  • go to %BOOST%/tools/jam/src folder
  • run build.bat file
  • copy bjam.exe file to %BOOST%/bin folder from %BOOST%/tools/jam/src/bin.ntx86 folder

build boost

  • go to %BOOST% folder
  • compile Boost using bjam.exe:

bin\bjam.exe –build-dir=”C:\Dev\Libs\boost\build-boost” –toolset=msvc –build-type=complete stage

Create a dmp file from the application

October 25th, 2009 No comments
static LPTOP_LEVEL_EXCEPTION_FILTER m_previousFilter = NULL;


typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType,
                                         CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
                                         CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
                                         CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);

static LONG WINAPI MyUnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
{
    HMODULE hDll = ::LoadLibrary(_T("DBGHELP.DLL"));
    MINIDUMPWRITEDUMP pDump = (MINIDUMPWRITEDUMP)::GetProcAddress(hDll, "MiniDumpWriteDump");

    _MINIDUMP_EXCEPTION_INFORMATION ExInfo;
    ExInfo.ThreadId = ::GetCurrentThreadId();
    ExInfo.ExceptionPointers = pExceptionInfo;
    ExInfo.ClientPointers = NULL;

    MINIDUMP_CALLBACK_INFORMATION mci;

    // HANDLE hFile - minidamp file name(for example, "test.dmp")

    BOOL bOK = pDump(::GetCurrentProcess(), ::GetCurrentProcessId(),
                     hFile, 1, &ExInfo, NULL, &mci);
}


void main()
{
    // setup our own ExceptionHandler
    m_previousFilter = SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);

    // actial work

    // befoe exit
    if (m_previousFilter)
    {
        SetUnhandledExceptionFilter(m_previousFilter);
    }
}

Parallel Programming

October 22nd, 2009 No comments

count valuable bits in unsigned int

October 14th, 2009 No comments
uint8_t num_of_bits32(uint32_t _arg)
{
    _arg = (_arg & 0x55555555L) + ((_arg >> 1) & 0x55555555L);
    _arg = (_arg & 0x33333333L) + ((_arg >> 2) & 0x33333333L);
    _arg = (_arg + (_arg >> 4)) & 0x0F0F0F0FL;
    _arg = _arg + (_arg >> 8);

    return (uint8_t)(_arg + (_arg >> 16)) & 0x3F;
}

or

int count1(int t)
{
 __asm
 {
        mov edx,t
        mov eax, 0
  cycle : bsf ecx, edx
        jz finish
        inc eax
        inc ecx
        shr edx, cl
        jmp cycle
  finish:
 }
}

Intel Threading Building Blocks (TBB)

October 6th, 2009 No comments

Intel® Threading Building Blocks (Intel® TBB) is an award-winning C++ template library that abstracts threads to tasks to create reliable, portable, and scalable parallel applications. Just as the C++ Standard Template Library (STL) extends the core language, Intel TBB offers C++ users a higher level abstraction for parallelism. To implement Intel TBB, developers use familiar C++ templates and coding style, leaving low-level threading details to the library. It is also portable between architectures and operating systems. With Intel TBB, developers get the benefits of faster programming, scalable performance, and easier to maintain code.

Intel homepage

Intel® Threading Building Blocks 2.2 for Open Source