小编anc*_*anc的帖子

std :: string参数的右值

当我传递文本时,以下代码中的LVALUE和RVALUE在实践上有什么区别?我的意思是,在这种特定的字符串情况下(其中字符串是字符串文字),使用RVALUE(&&)有什么好处吗?

void write_Lvalue(const std::string &text) {
    //...
}

void write_Rvalue(const std::string &&text) {
    //...
}

int main() {
    write_Lvalue("writing the Lvalue");
    write_Rvalue("writing the Rvalue");
}
Run Code Online (Sandbox Code Playgroud)

c++ rvalue-reference temporary-objects c++11

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

ResumeThread有时不会唤醒线程

我的C++中有两个函数:一个用于挂起,一个用于恢复线程.在某些情况下,我需要停止所有线程但是当前它没关系,但是当我必须恢复线程时,有时它不起作用,我不是没有原因.看方法:

void CDatabaseMonitor::ResumeAllThreads() 
{
    DWORD dwCurProc = GetCurrentProcessId();
    HANDLE hCurProc = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);

    if (hCurProc != INVALID_HANDLE_VALUE)
    {
        THREADENTRY32 te = {0};
        te.dwSize = sizeof(te);

        if (Thread32First(hCurProc, &te))
        {
            do
            {
                if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID))
                {
                    if (te.th32ThreadID != m_currentThreadId && te.th32OwnerProcessID == dwCurProc)
                    {
                        HANDLE thread = ::OpenThread(THREAD_ALL_ACCESS, FALSE, te.th32ThreadID);

                        if (thread != NULL)
                        {
                            ResumeThread(thread);
                            CloseHandle(thread);
                        }
                    }
                }

                te.dwSize = sizeof(te);
            }
            while (Thread32Next(hCurProc, &te));
        }

        CloseHandle(hCurProc);
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码有什么问题吗?有没有办法强制线程唤醒?提前致谢.

c++ winapi multithreading visual-c++

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

来自Winsock的connect()函数超时

当我从Winsock调用connect()函数时,有没有办法减少超时?我想差不多30秒,我想放5秒钟.

c++ sockets winapi winsock visual-studio

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

为什么我的数据元素被复制而不是移动?

我正在执行一些关于移动语义的测试,我的类行为对我来说似乎很奇怪。

鉴于模拟类VecOfInt

class VecOfInt {
public:
    VecOfInt(size_t num) : m_size(num), m_data(new int[m_size]) {}
    ~VecOfInt() { delete[] m_data; }
    VecOfInt(VecOfInt const& other) : m_size(other.m_size),  m_data(new int[m_size]) {
        std::cout << "copy..." <<std::endl;
        std::copy(other.m_data, other.m_data + m_size, m_data);
    }
    VecOfInt(VecOfInt&& other) : m_size(other.m_size) {
        std::cout << "move..." << std::endl;
        m_data = other.m_data;
        other.m_data = nullptr;
    }
    VecOfInt& operator=(VecOfInt const& other) {
        std::cout << "copy assignment..." << std::endl;
        m_size = other.m_size;
        delete m_data;
        m_data = nullptr;
        m_data = new int[m_size];
        m_data …
Run Code Online (Sandbox Code Playgroud)

c++ stdvector move-constructor move-semantics c++11

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

以下代码是多线程增量计数器和打印的好解决方案吗?

请你给点意见好吗?你将做点什么不同的?我的意思是,你认为如果我用 std::task 或 std::mutex、std::condition_variable 等来做会更好吗?我用 2 个标志来控制线程是一种矫枉过正吗?

std::atomic<int> counter = { 0 };
std::atomic<bool> switchFlag = { false };
std::atomic<bool> finished = { false };
constexpr int MAX_NUM = 10;

void increment(){
    while (!finished.load()){
        if (!switchFlag.load()){
            std::cout << "incremented to =" << ++counter << '\n';
            switchFlag.store(true);
        }
    }
}

void print(){
    while (!finished.load()) {
        if (switchFlag.load()){
            std::cout << "counter=" << counter.load() << '\n';
            if (counter.load() >= MAX_NUM)
                finished.store(true);

            switchFlag.store(false);
        }
    }
}

int main() {
    auto t1 = std::thread(increment);
    auto …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11 stdatomic

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

使用 std::enable::if 和 std::is_base_of 来约束继承

假设有以下场景:

class A {};
class B : public A {};
class C : public A {};
class D : public A {};

template<typename T/*std::enable_if and std::is_base_of here*/> class X {};
Run Code Online (Sandbox Code Playgroud)

当我宣布 X

然后我想约束typename T必须是 A 的子类,否则我会得到一个编译错误。

int main()
{
   X<B> x1 = {}; //should work;
   X<C> x2 = {}; //should work;
   X<D> x2 = {}; //should work;
   X<std::string> = {};  //should generate a compiling error;
   X<int> = {};  //should generate a compiling error;
};
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates c++11

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