Search code examples
c++for-loopspinlock

for with one parameter


Reading that spinlock and other multitasking stuff, I faced to this code:

#include <boost/range/algorithm.hpp>
#include <boost/atomic.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <vector>

class SpinLock
{
    boost::atomic_flag flag;
public:
    void lock()
    {
        while( flag.test_and_set(boost::memory_order_acquire) )
            ;
    }
    bool try_lock()
    {
        return !flag.test_and_set(boost::memory_order_acquire);
    }
    void unlock()
    {
        flag.clear(boost::memory_order_release);
    }
};

int main()
{
    using namespace std; using namespace boost;

    SpinLock lock;
    vector<thread> v;
    for(auto i = 0; i!=4; ++i)
        v.emplace_back([&lock, i]
        {
            for(auto j = 0; j!=16; ++j)
            {
                this_thread::yield();
                lock_guard<SpinLock> x(lock);
                cout << "Hello from " << i << flush << "\tj = " << j << endl;
            }
        });
    for(auto &t: v)
        t.join();
}

Could you explain why for has only one parameter?

And what that colon operator does?

And what is that t object?


Solution

  • It's range-based for. t has type std::thread&.