Solved

InterlockedExchange for string

Posted on 2007-03-21
6
622 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
  • 3
  • 3
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now