Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

InterlockedExchange for string

Posted on 2007-03-21
6
Medium Priority
?
688 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…
Suggested Courses

610 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