Solved

InterlockedExchange for string

Posted on 2007-03-21
6
644 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Execute multiple curl cmds with sleep and send output to file 10 114
operator overload: incompatible type and too many params 5 90
Making an alias 7 114
boost::uuid crashes 17 40
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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