c++使用mutex为函数上锁

本文深入探讨了Horovod并行训练框架的内部实现细节,包括数据类型定义、字节交换宏定义、字符串解析函数及多线程页面保存机制。通过具体代码示例,展示了如何使用Horovod进行高效的深度学习模型训练。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifndef HOROVOD_COMMON_H
#define HOROVOD_COMMON_H

#include<string.h>
#include <string>
#include<iostream>
#include<memory>
//#inlcude<cstring>
using namespace std;

typedef unsigned int byte; 
void getdefine(int a){
#if defined(_MSC_VER)
#define FLATBUFFERS_BYTESWAP16 int
#else
     #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408
     #define FLATBUFFERS_BYTESWAP17 int
    #endif

#endif

cout<<"this is getdefine";
FLATBUFFERS_BYTESWAP16 k=9;

}

#if defined HOROVOD_COMMON_H
#define FLATBUFFERS_BYTESWAP32 string

#endif
enum MPIDataType:byte {
    HOROVOD_UINT8 = 0,
    HOROVOD_INT8 = 1,
    HOROVOD_UINT16 = 2,
    HOROVOD_INT16 = 3,
    HOROVOD_INT32 = 4,
    HOROVOD_INT64 = 5,
    HOROVOD_FLOAT32 = 6,
    HOROVOD_FLOAT64 = 7,
    HOROVOD_BOOL = 8
};
typedef unsigned __int32 uint32_t;
string & ParseFromString(std::string& input){

    cout<<"ParseFrom String:";
    cout<<input<<endl;
    *(&input)="xq";
    return *&input;
}
class From2{
public:
 const void getnum() {
    //F="qw";
    //const string x="horovod";
    F="wq";
     cout<<"getnum"<<endl;
}
private:
    string F;
};
void foo(int * d)  
{  
       cout<<"1234"<<endl;  
} 


class EnumTest{

public:
    enum EnumType
    {
        ALL=0,REDUCE=1
    };

};

#include<map>
#include<string>
#include<chrono>
#include<thread>
#include<mutex>
std::map<std::string,std::string> g_pages;
std::mutex g_pages_mutex;

using namespace std;
int number=0;
void save_page(const std::string & url){
    g_pages_mutex.lock();
    std::this_thread::sleep_for(std::chrono::seconds(2));   
    cout<<"sleep time is end! thread is["<<++number<<"]"<<endl;
    string result="fake content";
    g_pages[url]=result+url;
    g_pages_mutex.unlock();

}
int main(){

    thread t1(save_page,"first1");
    thread t2(save_page,"first2");
    if(t1.joinable()){
        t1.join();
        t2.join();
        cout<<"T1 join the main !"<<endl;

    }

    g_pages_mutex.lock();

    for(const auto &x:g_pages){
        cout<<x.first<<" "<<x.second<<endl;
    }

    g_pages_mutex.unlock();
return 0;
}


#endif

输出结果:

sleep time is end! thread is[1]
sleep time is end! thread is[2]
T1 join the main !
first1 fake contentfi
first2 fake contentfi
请按任意键继续. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝鲸123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值