小编awm*_*129的帖子

android + pthread + c ++ = SIGSEGV

以下代码在标准linux上编译和运行:

#include <iostream>
#include <pthread.h>

using namespace std;

class Foo
{
    public:
        Foo();
        void go_thread();
        void stop_thread();
    private:
        static void* worker( void* param );
        pthread_t m_pt;
};

Foo::Foo()
{
    m_pt = 0;
}

void Foo::go_thread()
{
    int success = pthread_create( &m_pt, NULL, worker, static_cast<void*>(this) );

    if( success == 0 )
    {
        cout << "thread started" << endl;
    }
}

void Foo::stop_thread()
{
    int success = pthread_join( m_pt, NULL );

    if( success == 0 )
    {
        cout << "thread stopped" << …
Run Code Online (Sandbox Code Playgroud)

c++ android pthreads

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

在构造函数初始值设定项中使用成员的成员函数

我使用初始化列表进行了以下几次尝试,但我从来没有能够很好地解释它.任何人都可以解释为什么以下失败(我没有编译器来捕捉拼写错误,所以请耐心等待):

class Foo
{
 public:
  Foo( int i ) : m_i( i ) {}  //works with no problem

  int getInt() {return m_i;}

  ~Foo() {}
 private:
  int m_i;
};

class Bar
{
 public:
  Bar() : 
   m_foo( 5 ),           //this is ok 
   m_myInt( m_foo.getInt() ) //runtime error, seg 11
  {}

  ~Bar() {}
 private:
        Foo m_foo;
  int m_myInt;


};
Run Code Online (Sandbox Code Playgroud)

当尝试调用初始化列表初始化的成员的成员函数时,我得到seg错误.我似乎记得这是一个已知的问题(或者可能以某种方式设计),但我从未见过它.附加的示例是使用普通的旧数据类型设计的,但Bar::m_myInt用另一个缺少默认(空)构造函数的对象替换,问题更加真实.任何人都可以开导我吗?

c++ constructor ctor-initializer

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

Rust 编译器似乎对 Result 类型感到困惑

我正在尝试编译如下内容:

use anyhow::Result;
use std::sync::{mpsc, Mutex, OnceLock};
use std::cell::Cell;
use std::fmt;

static COMS: OnceLock<Mutex<Coms>> = OnceLock::new();

struct Coms {
    tx: Cell<Option<mpsc::Sender<Msg>>>,
    rx: Cell<Option<mpsc::Receiver<Msg>>>,
}

enum Msg {}

fn bar() -> Result<()> {
    let (tx, rx) = mpsc::channel();
    COMS.set(
        Mutex::new(
            Coms {
                tx: Cell::new(Some(tx)),
                rx: Cell::new(Some(rx)),
            }
        )
    );
    //)?;
    Ok(())
}

fn main() {
    let _ = bar();
}
Run Code Online (Sandbox Code Playgroud)

我收到有关未使用结果的预期警告:

warning: unused `Result` that must be used
  --> src/main.rs:17:5
   |
17 | /     COMS.set(
18 | |         Mutex::new(
19 | …
Run Code Online (Sandbox Code Playgroud)

rust anyhow

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

标签 统计

c++ ×2

android ×1

anyhow ×1

constructor ×1

ctor-initializer ×1

pthreads ×1

rust ×1