0

EDIT: The question has nothing to do with the one that was marked as duplicate. The errors are completely different and it surprises me that people who actually have knowledge on this stuff, marked it as duplicate, as if all they wanted was to earn points...

EDIT2: The question is clear and is even in the title. I repeat it here, for even more convenience...

Are there any flags should you use for the functions of the program mentioned below? 
(You only need to check the headers) For example thread needs -pthread flag.

I am a beginner in Linux C++ programming and also a beginner in multithreading. I am trying to find out what mutex exactly does and to practice on it, thus I went here and I want to compile the program over there. The problem is, I do not know which flags should I use in the compile command. I am compiling it with the command:

gcc <project_name> -o <executable_name> -std=c++14 -pthread

but I receive a bunch of errors (the compiler does not recognize chrono, mutex etc). Which flags should I use in the compilation command?

I am trying to compile it on Ubuntu 16.04 LTS (Virtual Machine from Windows 10) with gcc 6.2.0. Here is the code, for your ease:

#include <iostream>
#include <map>
#include <string>
#include <chrono>
#include <thread>
#include <mutex>

std::map<std::string, std::string> g_pages;
std::mutex g_pages_mutex;

void save_page(const std::string &url)
{
    // simulate a long page fetch
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::string result = "fake content";

    std::lock_guard<std::mutex> guard(g_pages_mutex);
    g_pages[url] = result;
}

int main() 
{
    std::thread t1(save_page, "http://foo");
    std::thread t2(save_page, "http://bar");
    t1.join();
    t2.join();

    // safe to access g_pages without lock now, as the threads are joined
    for (const auto &pair : g_pages) {
        std::cout << pair.first << " => " << pair.second << '\n';
    }
}
  • Tell me how is this question an "exact duplicate"... Are you doing this for points? – Manolis Grifoman Feb 27 '17 at 01:01
  • *"... but I receive a bunch of errors (the compiler does not recognize chrono, mutex etc)..."* - You have to provide the exact error message. Otherwise, the community has to guess at what's wrong and the flags required to fix it. In the absence of specific information, the close reason seems appropriate to me. – jww Feb 27 '17 at 11:52
  • There are no points for closing as duplicate. So, the **real** point here is that **you** step back for a second and spend time at the [help] to better understand the policies of this site. For example, when asking for "code not working", you show the **compiler** errors. Dont expect others to spend their time to fetch your code, to then try to run into the same errors as you did; without knowing what they are. And then: re-visit your attitude. So, yes, maybe it was wrong to close of your question as dup here; but still your question should have to be closed as it looks right now. – GhostCat Feb 27 '17 at 12:28
  • Now either I am not making my question clear, or something's happening here. IS there ANY flag I have to use for the above functions? Check the headers to see which functions. Stop focusing on irrelevant things. If there is not a flag I have to use, THEN focus on the errors... – Manolis Grifoman Feb 27 '17 at 12:28
  • And beyond that: you are always free to delete the question, and write up a **better** one, for example including more of your setup and the error messages you get. – GhostCat Feb 27 '17 at 12:29
  • My question wasn't even "Why it's not working". I am asking a simple question. I even put it on the title... And noone is focusing there... – Manolis Grifoman Feb 27 '17 at 12:29
  • What better question than having it in the title? But noone is bothering asnwering the question (which by the way, doesn't depend on the compiler errors). Everyone is trying to find a detail to say "HA! you did not ask right" and noone is bothering to actually answer the question... – Manolis Grifoman Feb 27 '17 at 12:32

0 Answers0