?
Solved

InterlockedExchange for string

Posted on 2007-03-21
6
Medium Priority
?
675 Views
Last Modified: 2013-11-20
When write to variables that are used in multiple threads, I read that I need to use InterlockedExchange() to lock the variable.  This function works great for integers but I have a variable that is a string.  Is there a function to do the same thing for strings.
0
Comment
Question by:ITSpgrmr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 18767543
Not really, but you can use a CRITICAL_SECTION for that, e.g.

// clobal variable
CRITICAL_SECTION g_cs;

// before launching the threads
InitializeCriticalSection(&g_cs);

// when accessing the string, enclose the action like
EnterCriticalSection(&g_cs);

// access the string, you have exclusive access to that section

LeaveCriticalSection(&g_cs); // after that, any other thread can enter the section
0
 
LVL 86

Expert Comment

by:jkr
ID: 18767550
Oh, and when you no longer need the CRITICAL_SECTION, clean up using

DeleteCriticalSection(&g_cs);
0
 
LVL 4

Expert Comment

by:chip3d
ID: 18767596
a nicer way would be to encapsulate the whole CRITICAL_SECTION stuff in a RAII object like:

class Lock
{
    public:
        Lock() {InitializeCriticalSection(&lock_);}
        ~Lock() {DeleteCriticalSection(&lock_);}

        void lock () const
        {
            EnterCriticalSection (&lock_);
        }

     bool unlock () const
        {
            LeaveCriticalSection (&lock_);
            return true;
        }

    private:
        mutable CRITICAL_SECTION lock_;

        // no copy and assignment
        Lock (const Lock &src);
        Lock& operator= (const Lock &src);
};
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 4

Expert Comment

by:chip3d
ID: 18767609
sorry

       bool unlock () const
        {
            LeaveCriticalSection (&lock_);
            return true;
        }

should be

       void unlock () const
        {
            LeaveCriticalSection (&lock_);
        }

regards
0
 
LVL 4

Expert Comment

by:chip3d
ID: 18767652
than all you need is a other class like

class Locker
{
    public:
        explicit Locker (Lock &lock) : lock_(lock)  {lock_.lock();}
        ~Locker() {lock_.unlock();}

    private:
        Lock& lock_;

        // no copy and assignment
        Locker (const Locker &src);
        Locker& operator= (const Locker &src);
};


so you could use is it like

....

Lock lock;
lock.lock();

your string code...

lock.unlock;

or as RAII

Lock lock; // as member of an object or in function....

Locker lockthis(lock);
your string code...

// the lock will be release as soon as lockthis will be out of scope

regards

0
 
LVL 86

Expert Comment

by:jkr
ID: 18767677
Even though a wrapper class is nice, I'd rather start out with the basics when learning a new API. Just my 2 Cents here, though.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question