首页 > 编程语言 >C++ thread array and join respectively

C++ thread array and join respectively

时间:2022-10-14 12:45:10浏览次数:57  
标签:10 join string ++ respectively int vec array include

#pragma comment(lib,"rpcrt4.lib")

#include <Windows.h>
#include <rpcdce.h>
#include <iostream>
#include <string>
#include <thread>
#include <vector>

using namespace std;

string getUuid()
{
    RPC_CSTR rpcCstr;
    string uuidValue;
    UUID newUUID;
    UuidCreate(&newUUID);
    UuidToStringA(&newUUID, &rpcCstr);
    uuidValue = (char*)rpcCstr;
    RpcStringFreeA(&rpcCstr);
    return uuidValue;
}

void printVector(const vector<string>& vec)
{
    for (const auto& str : vec)
    {
        cout << str << endl;
    }
    cout << endl << endl;
}

uint32_t num = 0;

void vectorDemo()
{
    vector<vector<string>> vec(10);
    for (int i = 0; i < 10; i++)
    {         
        for (int j = 0; j < 10; j++)
        {
            string tempStr = std::to_string(++num) + ","+getUuid();
            vec[i].push_back(tempStr);
        }
    }

    thread tArr[10];
    for (int i = 0; i < 10; i++)
    {
        tArr[i] = thread(printVector, std::ref(vec.at(i)));
    }

    for (int i = 0; i < 10; i++)
    {
        tArr[i].join();
        cout << "Thread " << i << " finished!" << endl;
    }
}

int main()
{
    vectorDemo();
    cin.get();
}

 

 

 

 

Though it's not run as expected regularlly,but it will aovid access resource conflicts absolutely.

 

标签:10,join,string,++,respectively,int,vec,array,include
From: https://www.cnblogs.com/Fred1987/p/16791255.html

相关文章

  • Java每日一题之Thread的yield和join方法区别
    Java每日一题之Thread的yield和join方法区别​​1.Java线程调度的一点背景​​​​2.理解线程的优先权​​​​3.yield()方法​​​​4.join()方法​​1.Java线程调度的一点......
  • leet Code [34. Find First and Last Position of Element in Sorted Array]
    [34.FindFirstandLastPositionofElementinSortedArray](https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/)二分法......
  • 二维数组面对对象array
    publicclassDemo05{publicstaticvoidmain(String[]args){/*[5][2]面对对象1,2array[0]2,3array[1]3,4array[2]......
  • 数组升序和数组填充array
    importjava.util.Arrays;publicclassDemo06{publicstaticvoidmain(String[]args){int[]a={1,2,5555,674,22};//System.out.println(a);......
  • 稀疏数组array
    publicclassDemo08{publicstaticvoidmain(String[]args){//创建一个二维数组5*5int[][]array1=newint[5][5];array1[1][2]=1;......
  • 冒泡排序array
    importjava.util.Arrays;publicclassDemo07{publicstaticvoidmain(String[]args){int[]a={1,5,8,3,22,13};int[]sort=sort(a);//调用......
  • 数组长度,和,最大元素array
    publicclassDemo03{publicstaticvoidmain(String[]args){int[]arrays={1,2,3,4,5,6};//打印全部的数组长度for(inti=0;i<ar......
  • 反转数组array
    publicclassDemo04{publicstaticvoidmain(String[]args){int[]arrays={1,2,3,4,5,6};/*JDK1.5:没有下标for(intarray:arr......
  • java 数据结构 ArrayList
    importjava.util.ArrayList;importjava.util.Collections;/***java数据结构ArrayList*importjava.util.ArrayList;//引入ArrayList类*ArrayList<E>objectNa......
  • Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
    概要上一章,我们学习了Collection的架构。这一章开始,我们对Collection的具体实现类进行讲解;首先,讲解List,而List中ArrayList又最为常用。因此,本章我们讲解ArrayList。先对Arra......