Concurrency mutex.try_lock()允许多个线程同时持有锁

Concurrency mutex.try_lock()允许多个线程同时持有锁,concurrency,c++11,Concurrency,C++11,几个小时后,我被c++11的unique\u lock的意外行为狠狠地打了一顿。我一定是严重误解了什么: #include <iostream> #include <vector> #include <thread> #include <mutex> #define N_THREADS 4 #define N_ITERATIONS 10000 #define N_LOOPS 1000000 class ThingMaJigger { publi

几个小时后,我被c++11的
unique\u lock
的意外行为狠狠地打了一顿。我一定是严重误解了什么:

#include <iostream>
#include <vector>
#include <thread>
#include <mutex>

#define N_THREADS 4
#define N_ITERATIONS 10000
#define N_LOOPS 1000000

class ThingMaJigger
{
public:
    void fight()    {
        if(m.try_lock()) {
            // more then one thread ends up here??!?!?
            printf("I'm the winner!\n" );
            m.unlock();
        } else {
            printf("Sleeping \n" );
        }
    }
private:
    std::mutex m;
};

void worker(ThingMaJigger* ar,int tIdx)
{
    ar->fight();
}

int main(int argc, char const *argv[])
{
    for (int _loop = 0; _loop < N_LOOPS; ++_loop) {
        std::vector<std::thread> ts;
        ThingMaJigger t;
        for (int i = 0; i < N_THREADS; ++i)
        {
            ts.emplace_back(worker,&t,i);
        }

        for (int i = 0; i < N_THREADS; ++i)
        {
            ts[i].join();
        }

        printf("\n");

    }
        return 0;
}
有点像多个工人拿着同一把锁 同时,不是吗

说:

返回值 如果成功获取锁,则为true,否则为false

注意:
try\u lock
允许返回false,即使没有其他人
我有锁。这不是问题所在。

concurency令人困惑。我们去购物吧

#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <unistd.h>

#define N_THREADS 4
#define N_ITERATIONS 10000
#define N_LOOPS 1000000

class ThingMaJigger
{
public:
    void fight()    {
        if(m.try_lock()) {
            // more then one thread ends up here??!?!?
            printf("I'm the winner!\n");
            usleep(1000000); // <<<< this. 
                             // or can unlock() before other's try_lock()
            m.unlock();
        } else {
            printf("Sleeping \n" );
        }
    }
private:
    std::mutex m;
};

void worker(ThingMaJigger* ar,int tIdx)
{
    ar->fight();
}

int main(int argc, char const *argv[])
{
    for (int _loop = 0; _loop < N_LOOPS; ++_loop) {
        std::vector<std::thread> ts;
        ThingMaJigger t;
        for (int i = 0; i < N_THREADS; ++i)
        {
            ts.emplace_back(worker,&t,i);
        }

        for (int i = 0; i < N_THREADS; ++i)
        {
            ts[i].join();
        }

        printf("\n");

    }
        return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义N_线程4
#定义N_迭代10000次
#定义N_循环1000000
阶级思想
{
公众:
无效战斗(){
如果(m.try_lock()){
//不止一根线在这里结束??!?!?
printf(“我是赢家!\n”);

usleep(1000000);//concurency令人困惑。我们去购物吧

#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <unistd.h>

#define N_THREADS 4
#define N_ITERATIONS 10000
#define N_LOOPS 1000000

class ThingMaJigger
{
public:
    void fight()    {
        if(m.try_lock()) {
            // more then one thread ends up here??!?!?
            printf("I'm the winner!\n");
            usleep(1000000); // <<<< this. 
                             // or can unlock() before other's try_lock()
            m.unlock();
        } else {
            printf("Sleeping \n" );
        }
    }
private:
    std::mutex m;
};

void worker(ThingMaJigger* ar,int tIdx)
{
    ar->fight();
}

int main(int argc, char const *argv[])
{
    for (int _loop = 0; _loop < N_LOOPS; ++_loop) {
        std::vector<std::thread> ts;
        ThingMaJigger t;
        for (int i = 0; i < N_THREADS; ++i)
        {
            ts.emplace_back(worker,&t,i);
        }

        for (int i = 0; i < N_THREADS; ++i)
        {
            ts[i].join();
        }

        printf("\n");

    }
        return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义N_线程4
#定义N_迭代10000次
#定义N_循环1000000
阶级思想
{
公众:
无效战斗(){
如果(m.try_lock()){
//不止一根线在这里结束??!?!?
printf(“我是赢家!\n”);

usleep(1000000);//concurency令人困惑。我们去购物吧

#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <unistd.h>

#define N_THREADS 4
#define N_ITERATIONS 10000
#define N_LOOPS 1000000

class ThingMaJigger
{
public:
    void fight()    {
        if(m.try_lock()) {
            // more then one thread ends up here??!?!?
            printf("I'm the winner!\n");
            usleep(1000000); // <<<< this. 
                             // or can unlock() before other's try_lock()
            m.unlock();
        } else {
            printf("Sleeping \n" );
        }
    }
private:
    std::mutex m;
};

void worker(ThingMaJigger* ar,int tIdx)
{
    ar->fight();
}

int main(int argc, char const *argv[])
{
    for (int _loop = 0; _loop < N_LOOPS; ++_loop) {
        std::vector<std::thread> ts;
        ThingMaJigger t;
        for (int i = 0; i < N_THREADS; ++i)
        {
            ts.emplace_back(worker,&t,i);
        }

        for (int i = 0; i < N_THREADS; ++i)
        {
            ts[i].join();
        }

        printf("\n");

    }
        return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义N_线程4
#定义N_迭代10000次
#定义N_循环1000000
阶级思想
{
公众:
无效战斗(){
如果(m.try_lock()){
//不止一根线在这里结束??!?!?
printf(“我是赢家!\n”);

usleep(1000000);//concurency令人困惑。我们去购物吧

#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <unistd.h>

#define N_THREADS 4
#define N_ITERATIONS 10000
#define N_LOOPS 1000000

class ThingMaJigger
{
public:
    void fight()    {
        if(m.try_lock()) {
            // more then one thread ends up here??!?!?
            printf("I'm the winner!\n");
            usleep(1000000); // <<<< this. 
                             // or can unlock() before other's try_lock()
            m.unlock();
        } else {
            printf("Sleeping \n" );
        }
    }
private:
    std::mutex m;
};

void worker(ThingMaJigger* ar,int tIdx)
{
    ar->fight();
}

int main(int argc, char const *argv[])
{
    for (int _loop = 0; _loop < N_LOOPS; ++_loop) {
        std::vector<std::thread> ts;
        ThingMaJigger t;
        for (int i = 0; i < N_THREADS; ++i)
        {
            ts.emplace_back(worker,&t,i);
        }

        for (int i = 0; i < N_THREADS; ++i)
        {
            ts[i].join();
        }

        printf("\n");

    }
        return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义N_线程4
#定义N_迭代10000次
#定义N_循环1000000
阶级思想
{
公众:
无效战斗(){
如果(m.try_lock()){
//不止一根线在这里结束??!?!?
printf(“我是赢家!\n”);

usleep(1000000);//这正按预期工作

在打印“我是赢家”后,您立即解锁了锁。这也给了其他线程获得它的机会

如果只希望有一个线程“赢”,还应该有一个变量指示是否有人赢了。在创建线程之前,将其设置为false。成功获取锁的任何线程都会检查该变量,以查看是否有其他线程赢了

bool somebodyWon = false; // make sure this is set to false before
                          // threads get created

    if(m.try_lock()) {
        if (somebodyWon) {
            printf("Darn, someone beat me to it!\n");
        } else {
            printf("I'm the winner!\n");
            somebodyWon = true;
        }
        m.unlock();
    } else {
        printf("I didn't even get a chance! \n" );
    }

另一种方法也是合法的,即从使用互斥变为使用信号量,这样一个线程就可以锁定对象,但让父线程在所有线程都加入后释放对象。

这是按预期工作的

在打印“我是赢家”后,您立即解锁了锁。这也给了其他线程获得它的机会

如果只希望有一个线程“赢”,还应该有一个变量指示是否有人赢了。在创建线程之前,将其设置为false。成功获取锁的任何线程都会检查该变量,以查看是否有其他线程赢了

bool somebodyWon = false; // make sure this is set to false before
                          // threads get created

    if(m.try_lock()) {
        if (somebodyWon) {
            printf("Darn, someone beat me to it!\n");
        } else {
            printf("I'm the winner!\n");
            somebodyWon = true;
        }
        m.unlock();
    } else {
        printf("I didn't even get a chance! \n" );
    }

另一种方法也是合法的,即从使用互斥变为使用信号量,这样一个线程就可以锁定对象,但让父线程在所有线程都加入后释放对象。

这是按预期工作的

在打印“我是赢家”后,您立即解锁了锁。这也给了其他线程获得它的机会

如果只希望有一个线程“赢”,还应该有一个变量指示是否有人赢了。在创建线程之前,将其设置为false。成功获取锁的任何线程都会检查该变量,以查看是否有其他线程赢了

bool somebodyWon = false; // make sure this is set to false before
                          // threads get created

    if(m.try_lock()) {
        if (somebodyWon) {
            printf("Darn, someone beat me to it!\n");
        } else {
            printf("I'm the winner!\n");
            somebodyWon = true;
        }
        m.unlock();
    } else {
        printf("I didn't even get a chance! \n" );
    }

另一种方法也是合法的,即从使用互斥变为使用信号量,这样一个线程就可以锁定对象,但让父线程在所有线程都加入后释放对象。

这是按预期工作的

在打印“我是赢家”后,您立即解锁了锁。这也给了其他线程获得它的机会

如果只希望有一个线程“赢”,还应该有一个变量指示是否有人赢了。在创建线程之前,将其设置为false。成功获取锁的任何线程都会检查该变量,以查看是否有其他线程赢了

bool somebodyWon = false; // make sure this is set to false before
                          // threads get created

    if(m.try_lock()) {
        if (somebodyWon) {
            printf("Darn, someone beat me to it!\n");
        } else {
            printf("I'm the winner!\n");
            somebodyWon = true;
        }
        m.unlock();
    } else {
        printf("I didn't even get a chance! \n" );
    }

另一种方法也是合法的,即从使用互斥变为使用信号量,这样一个线程可以锁定对象,但让父线程在所有线程都加入后释放对象。

您似乎有一个问题是纯代码(和(自身)用纯代码回答)。这样的问题最好在堆栈溢出问题上提问。问题中似乎没有任何软件设计或体系结构问题,答案中也没有关于设计或体系结构的信息。您似乎有一个纯代码问题(和(自我)用纯代码回答)。这样的问题最好在堆栈溢出问题上提问。问题中似乎没有任何软件设计或体系结构问题,答案中也没有关于设计或体系结构的信息。您似乎有一个纯代码问题(和(自我)用纯代码回答)。这样的问题最好在堆栈溢出问题上提问。问题中似乎没有任何软件设计或体系结构问题,答案中也没有关于设计或体系结构的信息。您似乎有一个纯代码问题(和(自我)用纯代码回答)。这样的问题最好在堆栈溢出问题上提问。问题中似乎没有任何软件设计或体系结构的问题,答案中似乎没有关于设计或体系结构的信息。我在发布后几分钟回答了我自己的问题,但无论如何,谢谢。我回答了我自己的问题