The following code in the constructor generates a warning

mutex_map[key] = PTHREAD_MUTEX_INITIALIZER; 

mutex_map has the following type:

  static std::map<std::string, pthread_mutex_t> mutex_map; 

in the same file in the global area

I get the following warning for simple C ++

  warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x 

I can not exactly understand this warning and how to solve this problem.

+4
source share
2 answers

Your PTHREAD_MUTEX_INITIALIZER is somewhat equivalent to a list of initializers.

In C ++ 03, you can initialize an array as follows:

 int arr[5] = {1,2,3,4,5}; 

Pay attention to the list of initializers. However, there is no support for classes.

In C ++ 11, they added std::initializer_list<T> , so this syntax is possible. Consider the following:

 std::vector<int> v = {1,2,3,4,5}; 

Or, even easier:

 std::vector<int> v{1,2,3,4,5}; 

This will work in C ++ 11, but not in C ++ 03, because in C ++ 11 a vector has a constructor that takes an argument from an initializer list. You can include this in your own classes. Note that the latter is equivalent to the following, as usual:

 std::vector<int> v ({1,2,3,4,5}); 

This helps illustrate that the list of initializers is now actual.

If you want to see the Stroustrup publication, I point you to the C ++ 11 FAQ .

+4
source

For the next solution: you need to call

 pthread_mutex_init(&mutex_map[key], NULL); 

instead of PTHREAD_MUTEX_INITIALIZER .

+2
source

Source: https://habr.com/ru/post/1410792/


All Articles