小编vim*_*ist的帖子

类成员函数的静态互斥锁:C++ 11

参考RAII

我可以使用static mutexcritical section为:

#include <string>
#include <mutex>
#include <iostream>
#include <fstream>
#include <stdexcept>

void write_to_file (const std::string & message) {
    // mutex to protect file access
    static std::mutex mutex;

    // lock mutex before accessing file
    std::lock_guard<std::mutex> lock(mutex);

    // try to open file
    std::ofstream file("example.txt");
    if (!file.is_open())
        throw std::runtime_error("unable to open file");

    // write message to file
    file << message << std::endl;

    // file will be closed 1st when leaving scope (regardless of exception)
    // …
Run Code Online (Sandbox Code Playgroud)

multithreading c++11

3
推荐指数
1
解决办法
1万
查看次数

JSON到PHP关联数组

你们中的任何人都知道把它放到一个关联数组中的好方法.我试过json_decode但发现它没有多大帮助.

这是我需要放入关联数组的数据:

   {
       "data": [
          {
             "name": "Joe Bloggs",
             "id": "203403465"
          },
          {
             "name": "Fred Bloggs",
             "id": "254706567"
          },
          {
             "name": "Barny Rubble",
             "id": "453363843"
          },
          {
             "name": "Homer Simpson",
             "id": "263508546"
          }
       ]
    }
Run Code Online (Sandbox Code Playgroud)

编辑:

在我接受了答案之后,我记得为什么我认为json_decode不起作用.

而不是像这样的关联数组:

[0] => Array
(
    [name] => Joe Bloggs
    [id] => 203403465
)
Run Code Online (Sandbox Code Playgroud)

我想要一个这样的:

Array
(
    [Joe Bloggs] => 45203340465
    [Fred Bloggs] => 65034033446
)
Run Code Online (Sandbox Code Playgroud)

不幸的是,我当时已经忘记了这一点......但无论如何我现在已经解决了我的问题.

感谢您所有的帮助!

php json facebook associative-array

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

标签 统计

associative-array ×1

c++11 ×1

facebook ×1

json ×1

multithreading ×1

php ×1