Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ FLTK中的线程_C++_Fltk - Fatal编程技术网

C++ FLTK中的线程

C++ FLTK中的线程,c++,fltk,C++,Fltk,首先,我回顾了示例test/threads.cxx;但这并不完全是我想要的用例,因为示例线程函数可以访问FLTK函数 我有一个libssh2库函数(我编写了这个函数,但希望它不依赖于FLTK),函数头如下: int sshSendFile(const char*loclfile,const char*scppath,char* 字节(已上载) 我希望它在线程中运行,而在线程中,FLTK旋转并读取值bytesupload,并更新Fl\u Progress上的标签,当然sshSendFile在上载时

首先,我回顾了示例
test/threads.cxx
;但这并不完全是我想要的用例,因为示例线程函数可以访问FLTK函数

我有一个
libssh2
库函数(我编写了这个函数,但希望它不依赖于FLTK),函数头如下:

int sshSendFile(const char*loclfile,const char*scppath,char* 字节(已上载)

我希望它在线程中运行,而在线程中,FLTK旋转并读取值
bytesupload
,并更新
Fl\u 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
,然后尝试使用此函数的结果启动线程

请注意,
\u beginthread
接受指向函数的指针,您必须使用从
int
(void(\u cdecl*)(void*))的难看转换来“消除”错误

换句话说,您必须将函数指针作为第一个参数传递给
\u 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,它很复杂,不方便,最好是不推荐的。

您这样做是非常错误的<代码>\u beginthread
需要函数的地址,但您正在将一些不相关的值转换为指向函数的指针。这将导致访问冲突错误,之后将无法工作。C风格的强制转换是引入错误的好方法。