小编Ste*_*sop的帖子

关键部分中的C++异常处理(pthreads)

[编辑:(从评论中复制)事实证明,问题出在其他地方,但谢谢大家的意见.]

我有一个共享容器类,它使用一个互斥锁来锁定push()和pop()函数,因为我不想同时修改head和tail.这是代码:

int Queue::push( WorkUnit unit )
{
    pthread_mutex_lock( &_writeMutex );
    int errorCode = 0;

    try
    {
        _queue.push_back( unit );
    }
    catch( std::bad_alloc )
    {
        errorCode = 1;
    }

    pthread_mutex_unlock( &_writeMutex );

    return errorCode;
}
Run Code Online (Sandbox Code Playgroud)

当我在调试模式下运行它时,一切都很好.当我在发布模式下运行时,我大致在驱动程序开始推送和"同时"弹出时崩溃.如果try/catch块捕获到std :: bad_alloc异常,它会立即强制退出吗?如果是这样,我应该将函数的其余部分包含在finally块中吗?

此外,较慢的调试模式是否可能成功,因为我的push()和pop()调用从未实际发生过?

c++ mutex exception pthreads

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

有没有办法检查外部数据是否通过管道传输到程序或程序是否独立运行?

即数据已由其他程序提供,或者用户必须手动输入.我指的是这两种状态:

dir /b /s *.* | myprogram
Run Code Online (Sandbox Code Playgroud)

myprogram
Run Code Online (Sandbox Code Playgroud)

在第二种情况下,程序将等待用户输入.有办法防止这种情况吗?

c++ windows console

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

我是线程的新手,这个编译错误意味着什么?

使用C++.

pthread_t threads[STORAGE]; // 0-99

...

void run()

Error>>>    int status = pthread_create(&threads[0], NULL, updateMessages, (void *) NULL);
if (status != 0)
{
    printf("pthread_create returned error code %d\n", status);
    exit(-1);
}
Run Code Online (Sandbox Code Playgroud)

...

void ClientHandler::updateMessages(void *)
{
    string reqUpdate = "91"; // Request for update
    string recvMSG;
    while (true)
    {
        sleep(5);
        sending(sock,reqUpdate); // send
        recvMSG = receiving(sock); // receive
        QString output(recvMSG);
        emit signal_chat(output, 0);    // Print message to text box
    }
}
Run Code Online (Sandbox Code Playgroud)

...

编译错误: TCPClient.cpp:109: error: argument of type ‘void (ClientHandler::)(void*)’ …

c++ multithreading

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

C++中两个指针向量的减法和相交

我有两个向量,指向我的自定义类对象.
这两个向量中的指针不指向同一个对象,但存储在对象中的值是相同的.

我的自定义类结构是:


Class Item
{
   string ItemId;
   string ItemDescription;
   float ItemPrice;
}
第一矢量(V1)具有n个条目,第二矢量(V2)具有m个条目(n> m).

我要执行两个操作:

  • 获取一个在V1V2中都有共同对象的向量.通常,我的意思是说元素的ItemId是相同的.(可以称为V1和V2的交点).

  • 获取具有V2中不存在的元素的向量.(可以称为V1-V2).

    如何以有效的方式做到这一点?

  • c++ stdvector

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

    标签 统计

    c++ ×4

    console ×1

    exception ×1

    multithreading ×1

    mutex ×1

    pthreads ×1

    stdvector ×1

    windows ×1