V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  v2qwsdcv  ›  全部回复第 2 页 / 共 2 页
回复总数  30
1  2  
2019 年 4 月 11 日
回复了 mart1nN 创建的主题 Java Java 多线程资源共享的问题
源码给您找好啦,求别鄙视我们做 C++的了 都不容易。


https://github.com/unofficial-openjdk/openjdk/blob/5ec14c8bb2533c20eca3564258c4dc66bf3bb9c3/src/java.base/share/classes/java/lang/Thread.java
public synchronized void start() {
...
try {
start0();
started = true;
} ...
}

private native void start0();

https://github.com/unofficial-openjdk/openjdk/blob/531ef5d0ede6d733b00c9bc1b6b3c14a0b2b3e81/src/java.base/share/native/libjava/Thread.c
{"start0", "()V", (void *)&JVM_StartThread},

https://github.com/unofficial-openjdk/openjdk/blob/e19d12112815026f04a9df075e56eb26622b9d8d/src/hotspot/share/prims/jvm.cpp

JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
JVMWrapper("JVM_StartThread");
JavaThread *native_thread = NULL;
...
native_thread = new JavaThread(&thread_entry, sz);

if (native_thread->osthread() != NULL) {
// Note: the current thread is not being used within "prepare".
native_thread->prepare(jthread);
}
}
}
...
Thread::start(native_thread);

JVM_END

static void thread_entry(JavaThread* thread, TRAPS) {
HandleMark hm(THREAD);
Handle obj(THREAD, thread->threadObj());
JavaValue result(T_VOID);
JavaCalls::call_virtual(&result,
obj,
SystemDictionary::Thread_klass(),
vmSymbols::run_method_name(),
vmSymbols::void_method_signature(),
THREAD);
}

https://github.com/unofficial-openjdk/openjdk/blob/294d2d319b870ac68ca10a5b03006a70e26bcaba/src/hotspot/share/runtime/thread.cpp
void JavaThread::run() {
...
thread_main_inner();
}


void JavaThread::thread_main_inner() {
...
if (!this->has_pending_exception() &&
!java_lang_Thread::is_stillborn(this->threadObj())) {
{
ResourceMark rm(this);
this->set_native_thread_name(this->get_thread_name());
}
HandleMark hm(this);
this->entry_point()(this, this);
}
...
}

https://github.com/unofficial-openjdk/openjdk/blob/d148cecb01572f077179c94cb59117af89eb59b8/src/hotspot/share/runtime/javaCalls.cpp

https://github.com/unofficial-openjdk/openjdk/blob/e836a10c8c6ed2ef2f3219c46ca1906a2d9d6493/src/hotspot/share/classfile/vmSymbols.hpp
template(run_method_name, "run")
2019 年 4 月 4 日
回复了 ZenFX 创建的主题 汽车 卡罗拉和雷凌怎么选
建议买 10 万以下的国产车。如非必然,可以考虑不买车。
2018 年 12 月 20 日
回复了 sky2017 创建的主题 C 关于 C++ std::thread 的疑问
@ZouZhiZhang 依然不能导出自定义类型的 全局变量。
2018 年 12 月 20 日
回复了 sky2017 创建的主题 C 关于 C++ std::thread 的疑问
@ZouZhiZhang 貌似不行啊 是不是我用错了
g++ --std=c++11 -fPIC -shared -o libdy.so -Wl,--whole-archive dynamic.o -Wl,--no-whole-archive
2018 年 12 月 19 日
回复了 sky2017 创建的主题 C 关于 C++ std::thread 的疑问
动态链接库不是 C++标准,是不同操作系统的实现。
我测试了一下,在 Linux 下不能导出自定义的类型为全局变量。没有你说的阻塞的情况,应该就是没有生成这个变量导致。

楼上说的对, 反对使用全局变量。

```

#include <thread>
#include <cstdio>
extern "C"
{
class TestClass
{
public:
TestClass()
{
printf("construct TestClass\n");
m_thread = std::thread([] {});
}
~TestClass()
{
printf("destruct TestClass\n");
if (m_thread.joinable())
{
m_thread.join();
}
}

protected:
private:
std::thread m_thread;
};

extern TestClass tc;

extern int go =1002;
extern struct my m;

struct my{
int a;
int b;
};
}

//g++ --std=c++11 -fPIC -shared dynamic.cpp -o libdy.so
```

从符号表上看只有 int go 被导出了
```
nm -D libdy.so
0000000000201024 B __bss_start
w __cxa_finalize
0000000000201024 D _edata
0000000000201028 B _end
00000000000005c0 T _fini
w __gmon_start__
0000000000201020 D go
0000000000000480 T _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
w _Jv_RegisterClasses

```

调用的代码

```
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int main()
{
void *handle = dlopen("./libdy.so", RTLD_NOW);
if (!handle)
{
printf("%s\n", dlerror());
exit(-1);
}
{
void *ptr = nullptr;
ptr = dlsym(handle, "tc");
if (ptr == nullptr)
{
printf("tc is null\n");
printf("%s\n", dlerror());
}
else
{
printf("find tc\n");
}
}

{
void *gp = nullptr;
gp = dlsym(handle, "go");
if (!gp)
{
printf("tc is null\n");
printf("%s\n", dlerror());
}
else
{
printf("go is %d\n", *((int *)gp));
}
}

{
void *ptr = nullptr;
ptr = dlsym(handle, "m");
if (ptr == nullptr)
{
printf("m is null\n");
printf("%s\n", dlerror());
}
else
{
printf("find m\n");
}
}
dlclose(handle);

return 0;
}

//g++ -std=c++11 -rdynamic call_dynamic.cpp -o call_dynamic -ldl
```
2018 年 12 月 4 日
回复了 zhiqiang 创建的主题 C C++什么情况下会出现类 static 成员析构错误?
如果不是 STL 的 bug,那么可能和我以前碰到的一个问题一样:由于源文件编码导致。你的源文件有的用 gbk 有的用 utf8. C++编译单元的编码和该编译单元的源文件相同。
2018 年 10 月 29 日
回复了 xcai 创建的主题 Linux IBM 喜提红帽
Debian 或成最大赢家。
2018 年 10 月 8 日
回复了 mrchi 创建的主题 程序员 入职提交材料中需要户口本复印件正常吗?
上海 部分公司需要 貌似跟社保相关
2018 年 8 月 17 日
回复了 jokerL 创建的主题 问与答 有些事不吐不快
所以要么只生一个好,要么别生孩子。
2018 年 8 月 9 日
回复了 wsds 创建的主题 程序员 c++不用 boost 库,怎么切割字符串?
1  2  
关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   4451 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 27ms · UTC 05:46 · PVG 13:46 · LAX 21:46 · JFK 00:46
♥ Do have faith in what you're doing.