FLTK 中的线程
Threading in FLTK
首先,我复习了示例test/threads.cxx
;但这并不是我正在寻找的用例,因为示例线程函数可以访问 FLTK 函数。
我有一个 libssh2
库函数(我写的,但希望它不依赖于 FLTK),具有以下函数头:
int sshSendFile(const char * loclfile, const char * scppath, char *
bytesuploaded)
我想在线程中 运行,在线程中,FLTK 旋转并读取值 bytesupload
并更新 Fl_Progress
上的标签,其中sshSendFile 课程在上传时正在更新。
实际上,这就是我到目前为止所拥有的;一旦 sshSendFile 完成,我的程序就会在调试中退出!
Fl::lock();
char * string_target;
string_target = (char *)malloc(128);
void * next_message;
(Fl_Thread)_beginthread((void(__cdecl *)(void *))sshSendFile("C:/randomfile.txt", "/tmp/testing, string_target), 0,NULL);
while (Fl::wait() > 0) {
if ((next_message = Fl::thread_message()) != NULL) {
this->progress_bar->label(string_target);
this->redraw();
Fl::check();
}
}
Fl::unlock();
在 Fl:wait()
处设置断点永远不会命中。我在调试这个时遇到了一些麻烦,发现文档不是很清楚。感谢您的帮助!
您在主线程中调用 sshSendFile
,然后尝试使用此函数的结果启动线程。
请注意 _beginthread
接受指向函数的指针,并且从 int
到 (void(__cdecl *)(void *))
.
这种丑陋的转换必须 "silence" 错误
换句话说,您必须将函数指针作为第一个参数传递给 _beginthread
,最简单的方法是像这样创建 "thread main":
struct Task {
std::string file;
...
}
void sendFiles(void* arg) {
Task* task = (task*)arg;
sshSendFiles(task.file.c_str(), ...);
delete task;
}
和你启动线程的代码,应该传递这个sendFiles
和一个任务指针:
task* task = new task();
task->file = "something";
... initialize also buffer
(Fl_Thread)_beginthread(&sendFiles, 0, task);
// this will call startThread(task) in main thread
另一方面,使用 C++11 中的现代线程 api 会容易得多,因为您现在所做的是普通的旧系统级 C,它很复杂、不方便并且充其量已被弃用。
首先,我复习了示例test/threads.cxx
;但这并不是我正在寻找的用例,因为示例线程函数可以访问 FLTK 函数。
我有一个 libssh2
库函数(我写的,但希望它不依赖于 FLTK),具有以下函数头:
int sshSendFile(const char * loclfile, const char * scppath, char * bytesuploaded)
我想在线程中 运行,在线程中,FLTK 旋转并读取值 bytesupload
并更新 Fl_Progress
上的标签,其中sshSendFile 课程在上传时正在更新。
实际上,这就是我到目前为止所拥有的;一旦 sshSendFile 完成,我的程序就会在调试中退出!
Fl::lock();
char * string_target;
string_target = (char *)malloc(128);
void * next_message;
(Fl_Thread)_beginthread((void(__cdecl *)(void *))sshSendFile("C:/randomfile.txt", "/tmp/testing, string_target), 0,NULL);
while (Fl::wait() > 0) {
if ((next_message = Fl::thread_message()) != NULL) {
this->progress_bar->label(string_target);
this->redraw();
Fl::check();
}
}
Fl::unlock();
在 Fl:wait()
处设置断点永远不会命中。我在调试这个时遇到了一些麻烦,发现文档不是很清楚。感谢您的帮助!
您在主线程中调用 sshSendFile
,然后尝试使用此函数的结果启动线程。
请注意 _beginthread
接受指向函数的指针,并且从 int
到 (void(__cdecl *)(void *))
.
换句话说,您必须将函数指针作为第一个参数传递给 _beginthread
,最简单的方法是像这样创建 "thread main":
struct Task {
std::string file;
...
}
void sendFiles(void* arg) {
Task* task = (task*)arg;
sshSendFiles(task.file.c_str(), ...);
delete task;
}
和你启动线程的代码,应该传递这个sendFiles
和一个任务指针:
task* task = new task();
task->file = "something";
... initialize also buffer
(Fl_Thread)_beginthread(&sendFiles, 0, task);
// this will call startThread(task) in main thread
另一方面,使用 C++11 中的现代线程 api 会容易得多,因为您现在所做的是普通的旧系统级 C,它很复杂、不方便并且充其量已被弃用。