小编Al *_*ndy的帖子

有条件地使用std :: lock_guard

我有一个函数,其中语句foo应该在下面执行,lock_guard但仅当指向mutex对象的指针作为参数提供给函数时.否则foo不必受到保护 lock_guard.

我不能使用lock_guard范围内的if,因为当锁将被立即释放if块结束.

所以,这段代码是胡说八道:

bar( std::mutex * optionalMutex = nullptr )
{
   ...
   if ( nullptr != optionalMutex ) {
      std::lock_guard<std::mutex> lockScope( *optionalMutex );
   }          <- Here the lock ends

   foo...     <- foo is not protected when optionalMutex was provided
}
Run Code Online (Sandbox Code Playgroud)

我试过这样的事情:

bar( std::mutex * optionalMutex = nullptr )
{
   ...
   nullptr == optionalMutex ? 0 : std::lock_guard<std::mutex> lockScope( *optionalMutex );

   // …
Run Code Online (Sandbox Code Playgroud)

c++ conditional scope locking c++11

15
推荐指数
2
解决办法
2481
查看次数

为C printf设置千位分隔符

我有这个C代码:

locale_t myLocale = newlocale(LC_NUMERIC_MASK, "en_US", (locale_t) 0);
uselocale(myLocale);
ptrLocale = localeconv();
ptrLocale->thousands_sep = (char *) "'";

int i1 = snprintf( s1, sizeof(s1), "%'d", 123456789);
Run Code Online (Sandbox Code Playgroud)

输出s1123,456,789.

即使我设置->thousands_sep'它被忽略.有没有办法将任何字符设置为千位分隔符?

c printf locale

9
推荐指数
1
解决办法
9580
查看次数

为什么gcc抱怨"'foo'的声明影响了之前的调用[-Werror = shadow]"

我有这个MCVE:

auto bar() -> double { return 8.0; }

int main()
{
    if ( auto foo = bar() )
    {
        return foo;
    }
    else if ( auto foo = bar() )
    {
        return foo;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用gcc 7.3和编译它们会-c -Werror -Wextra -Wall -Wshadow生成以下错误消息:

test-shadow.cpp: In function ‘int main()’:
test-shadow.cpp:9:17: error: declaration of ‘foo’ shadows a previous local [-Werror=shadow]
  else if ( auto foo = bar() )
                 ^~~
test-shadow.cpp:5:12: note: shadowed declaration is here
  if ( auto foo …
Run Code Online (Sandbox Code Playgroud)

c++ gcc

8
推荐指数
1
解决办法
557
查看次数

访问包含冒号和连字符的JSON密钥

MySQL表格具有以下格式:

CREATE TABLE bar...
   ...
   foo JSON
   ...
Run Code Online (Sandbox Code Playgroud)

该字段foo包含以下内容:

{"#:8": 0.90189, "#:34": 0.90239, "#:55": 0.90238, "#:144": 0.90219, "X:21-34": -1}
Run Code Online (Sandbox Code Playgroud)

此命令失败:

SELECT foo FROM bar WHERE foo->'$.X:21-34' != 0;
Run Code Online (Sandbox Code Playgroud)

错误(4,1):无效的JSON路径表达式.错误在字符位置9附近.

如何访问键名中有一些特殊字符但JSON字符串仍然有效的字段?

mysql json

7
推荐指数
1
解决办法
621
查看次数

带有CUDA 6.0的C++ 11标准

我想在CUDA 6.0项目中使用C++ 11标准来处理我的C++文件.当我将CUDA 6.0 Nsight Eclipse设置中的编译器更改为g ++并添加-std=c++11选项时,我收到很多错误,如下所示:

error: namespace "std::__gnu_cxx" has no member "__normal_iterator"
Run Code Online (Sandbox Code Playgroud)

显然我必须为CUDA选择一次编译器,然后为我的C++文件选择.

我该怎么做?安装CUDA 6.5(支持未记录的C++ 11)不是一种选择.

c++ linux gcc cuda c++11

6
推荐指数
1
解决办法
3549
查看次数

如何使用VideoView Android切换FullScreen

我正在使用视频视频进行实时流媒体播放,我希望将其设置VideoView全屏切换并返回小屏幕,MXPlayer或者YouTube播放器不停止流.

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ad_container"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@android:color/transparent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent" >

        <TextView
            android:id="@+id/scroll_annouc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="@color/red"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:lines="1"
            android:marqueeRepeatLimit="marquee_forever"
            android:padding="@dimen/ten_dp"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="@string/app_name"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/scroll_annouc"
            android:background="@android:color/transparent"
            android:orientation="vertical" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="270dp"
                android:background="@android:color/black" >

                <TextView
                    android:id="@+id/error_overlay"
                    android:layout_width="match_parent"
                    android:layout_height="270dp"
                    android:layout_alignParentTop="true"
                    android:background="@color/red_trans"
                    android:gravity="center"
                    android:text="@string/error_text"
                    android:textColor="@android:color/white"
                    android:textStyle="bold"
                    android:visibility="invisible" />

                <VideoView
                    android:id="@+id/player"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true" />

                <RelativeLayout
                    android:id="@+id/media_controller"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" …
Run Code Online (Sandbox Code Playgroud)

video android android-videoview

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

CUDA:类似于 __device__ 代码的 sprintf 函数

我在互联网上找不到任何东西。由于可以printf__device__函数中使用这一事实,我想知道是否存在sprintf类似的函数,因为它printf“使用”了sprintf要在stdout.

printf cuda

6
推荐指数
1
解决办法
1330
查看次数

curl 在线程调用中崩溃

如果curl是线程安全的,我阅读了 SO 文章。这个非常简单的代码会崩溃 - 并非总是如此,但是当我连续多次调用该程序 [非并行] 时,它会因分段错误或以下错误而崩溃。

到目前为止,我确信我遵循curl文档中所述的关于和线程的规则。

在测试中,我可以发现它在curl_easy_perform().

#include <curl/curl.h>
#include <stdio.h>
#include <thread>

class curlClass
{
private:
    CURL * curl {};
    CURLcode res;
    const char * sUrl;

public:
    auto loadDataFromUrl()     -> void;
    static auto initCurl()     -> void;
    static auto releaseCurl()  -> void;
    static auto callbackSaveData( void * content, size_t size, size_t nmemb, curlClass * classInstance ) -> size_t;
    curlClass( const char * );
    ~curlClass();
};

auto curlClass::initCurl() -> void
{
    curl_global_init(CURL_GLOBAL_SSL);
} …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading curl

6
推荐指数
1
解决办法
4075
查看次数

Memcached:如何在检索所有密钥时打破限制

由于我无法在网络中找到任何可靠的程序,一旦板块中的密钥数量超过500k就会检索所有的memcache密钥,我想用PHP编写自己的程序.

然后我意识到有一个原因,为什么所有的程序都没有真正适用于这么多的密钥.

当我用stats cachedump <slab-id> <limit>它读取键时,只返回一定数量的键 - 在我的情况下 - 结束约30k.

我怎样才能得到至少这500k可能在一块板上?

建议使用的所有"帖子"和"答案" memdump都不起作用.他们也有这个限制.

我在用 memcached 1.4.25

php memcached

5
推荐指数
1
解决办法
1449
查看次数

std :: atomic <double>&的模板特化

我有这个MCVE:

#include <stdio.h>
#include <atomic>

template<typename T> void assertVariableHasBeenSet( T, const char * );
template<>           void assertVariableHasBeenSet<std::atomic<double> &>
                                                        ( std::atomic<double> & myDouble, 
                                                          const char * variableName 
                                                        )
{
    printf( "Double:%s=%f\n", variableName,  myDouble.load() );
};

int main()
{
    std::atomic<double> myDoubleAtomic  {23.45};

    assertVariableHasBeenSet( myDoubleAtomic,   "myDoubleAtomic" );
}
Run Code Online (Sandbox Code Playgroud)

我得到这个编译器错误:

getType.cpp: In function ‘int main()’:
getType.cpp:14:61: error: use of deleted function ‘std::atomic<_Tp>::atomic(const std::atomic<_Tp>&) [with _Tp = double]’
  assertVariableHasBeenSet( myDoubleAtomic, "myDoubleAtomic" );
                                                             ^
In file included from getType.cpp:2:0:
/usr/local/include/c++/4.9.4/atomic:169:7: note: declared here
       atomic(const atomic&) = …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization c++11

5
推荐指数
1
解决办法
188
查看次数