惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

博客园 - 一字千金

linux C++崩溃堆栈信息打印 看懂成交密度,秒辨股市资金真假动向 安装android studio时出现下面报错source-36_r01.zip安装失败 异动拉升横盘突破筛选股票 sh看门狗脚本 QLabel设置QToolTip显示的样式 自动化http请求脚本 批量遍历文件夹内得文件生成md5值 python使用pg数据库 批量获取git所有分支命令 大模型之智能体 深⼊了解 GPT-4和ChatGPT的API 初识GPT-4和ChatGTP 什么是大模型以及需要掌握哪些基础知识 linux下检测程序内存泄漏方法步骤 pycharm2024下载安装一键激活2099年 linux根据进程名查找进程并杀死进程脚本 linux根据进程名称定时获取进程内存脚本 QGridLayout删除旧窗口还有清干净 linux编译安装ccache3.2.4
g_JavaVM->AttachCurrentThread((void **) &pEnv, NULL))返回-1
一字千金 · 2025-07-21 · via 博客园 - 一字千金

g_JavaVM->AttachCurrentThread((void **) &pEnv, NULL))返回-1,流式回调问题解决了,因为研究院流式回调线程从pthread改成了brpc的bthread,jvm和bthread不兼容,导致无法绑定线程;解决方法是将回调结果缓存到任务队列,起个pthread线程消费队列,往上层发;

特性

pthread (POSIX Threads)

bthread (BRPC)

线程模型

1:1(内核线程)

M:N(用户态线程,映射到少量pthread)

创建开销

较高(微秒级,需内核参与)

极低(纳秒级,纯用户态操作)

调度方式

由操作系统内核调度

用户态协作式调度(Work-Stealing算法)

阻塞影响

阻塞整个内核线程

仅阻塞当前bthread(自动切换其他任务)

内存占用

每个线程独立栈(默认MB级)

共享栈池(默认KB级)

多核利用率

依赖OS调度

自动负载均衡

//线程
std::queue<SourceResult> queue;
std::atomic<bool> stop_flag;
pthread_mutex_t pmutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t pcond = PTHREAD_COND_INITIALIZER;
pthread_t consumer;
// 消费者成员函数
void CtaT4Impl::consume() {
    while (stop_flag==false) {
        
        pthread_mutex_lock(&pmutex);
        // 无任务时等待
        while (queue.size() == 0 && stop_flag==false)
        {
            pthread_cond_wait(&pcond, &pmutex);//等待并释放锁
        }
        // 处理所有待执行任务
        while (queue.size() > 0&& stop_flag==false) {
            SourceResult task = queue.front();
            queue.pop();
            pthread_mutex_unlock(&pmutex);  // 执行任务时释放锁
            if (pTaskEvent_)
            {
                pTaskEvent_->OnFinished(task);
                VCAENGINE_INFO("consume callback task%s", task.result.c_str());
                //std::this_thread::sleep_for(std::chrono::microseconds(10));//sleep试下,太快会出现错误JavaVM can't attach current thread
            }
            pthread_mutex_lock(&pmutex);
        }
        pthread_mutex_unlock(&pmutex);
    }
}
//线程函数
HPR_VOIDPTR CALLBACK ComsumeThread(HPR_VOIDPTR ppoint)
{
    if (ppoint == NULL)
    {
        VCAENGINE_ERROR("ppoint is NULL");
        return NULL;
    }
    CtaT4Impl* piplm = (CtaT4Impl*)ppoint;
    if (piplm == NULL)
    {
        VCAENGINE_ERROR("piplm is NULL");
        return NULL;
    }
    piplm->consume();
}
构造函数中启动消费者线程
// 启动消费者线程 (使用lambda)
stop_flag = false;
pthread_attr_t attr;
// 初始化线程属性
pthread_attr_init(&attr);
// 设置堆栈大小(单位:字节)
if (pthread_attr_setstacksize(&attr,20*1024*1024) != 0) {
    VCAENGINE_ERROR("Failed to set stack size");
    return;
}

// 创建线程
if (pthread_create(&consumer, &attr, ComsumeThread, this) != 0) {
    VCAENGINE_ERROR("Failed to create thread");
    pthread_attr_destroy(&attr);
    return;
}
// 销毁属性对象,不用等待线程结束
pthread_attr_destroy(&attr);