小编Min*_*ham的帖子

如何绘制 UML 来说明在临界区工作的 2 个线程

我有一个小程序,它在同一个临界区运行 2 个线程并使用互斥锁。该程序运行良好。但我想画一个 UML,最好是活动或状态图,说明 2 个线程运行相同的临界区,而不是代码的不同部分。请帮我修改我的 UML。

下面是源代码:

#include <iostream>
#include <thread>
#include <mutex>
#include <stdio.h>
/* Global variables where both threads have access to*/
std::mutex myMutex;
int globalVariable = 1;
/* CRITICAL SECTION */
void hello()
{
    myMutex.lock();
    for (int counter =0 ; counter< 100; counter++){
        //std::lock_guard<std::mutex> lock(myMutex);
        printf("%d ",std::this_thread::get_id() );      //print the id of the thread that executes the for loop
        globalVariable++;
    }
    printf("Thread with id = %d runs. Counter is %d \n", std::this_thread::get_id(), msg) ; //print the …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading uml mutex

8
推荐指数
1
解决办法
165
查看次数

在向量中查找结构体

我想找到一个所有成员数据都与某些值匹配的结构。

我做了一个小程序如下:

#include <iostream>
#include <vector>
using namespace std;
struct vlan {
    int vlanId;
    bool status;
};

vector<vlan> vlanTable; 

int main(){
    vlan tmp;
    tmp.status = true;
    tmp.vlanId = 1;
    vector <vlan>::iterator flag = find(vlanTable.begin(), vlanTable.end(), tmp);
    if ( flag != vlanTable.end()){
        cout<<"found"<<endl;
    }
    else cout<<"not found"<<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它返回错误为:模板参数推导/替换在查找函数处失败。

有人可以帮助我吗?

c++ vector

5
推荐指数
2
解决办法
116
查看次数

函数声明后的 throw() 有什么用?

C++ expressionThrow在https://en.cppreference.com/w/cpp/language/throw中定义为 a 。从语法上讲,它后面跟着一个异常类名。例如:

int a = 1, b = 0; 
if (b==0){
    string m ="Divided by zero";
    throw MyException(m);       //MyException is a class that inherit std::exception class
}
Run Code Online (Sandbox Code Playgroud)

但是,我见过其他一些我不太理解的 throw 语法:

void MyFunction(int i) throw();     // how can we have an expression following a function definition? 
Run Code Online (Sandbox Code Playgroud)

或者在自定义异常类中,我们还有:

class MyException : public std::exception
{
public:
  MyException( const std::string m)
    : m_( m )
  {}

  virtual ~MyException() throw(){};               // what is throw() in this case? 
  const char* what() …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
670
查看次数

标签 统计

c++ ×3

multithreading ×1

mutex ×1

uml ×1

vector ×1