c++在线程内 调用外部库里面的一个耗时长的函数时,主界面会出现暂时的卡死。

七步777 2016-08-22 02:35:09
c++在线程内 调用外部库里面的一个耗时长的函数时,主界面会出现暂时的卡死。请问什么原因呢?有什么解决方法。

使用的clock_t start_test=clock();
clock_t end_test=clock();
double time_test=(double)(end_test-start_test);


是一个保存文件的方法。 耗时大概在5-6秒
...全文
489 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
七步777 2016-08-22
  • 打赏
  • 举报
回复
引用 10 楼 u010370871 的回复:
其实我想问问什么UI库????????
问题找到了,谢谢各位。 库里面加了线程锁。 当调用保存文件的时候,再调用该库里面其他方法的时候就阻塞了。 而界面有个timer在间隔刷新卡采集到的数据。 于是保存的时候就造成了阻塞卡死了。
张小飞Official 2016-08-22
  • 打赏
  • 举报
回复
其实我想问问什么UI库????????
pengzhixi 2016-08-22
  • 打赏
  • 举报
回复
那这个你就应该和这个库的作者反馈下这个问题。由他来提供解决方案。不过很好奇,他怎么来防止被中断的。
赵4老师 2016-08-22
  • 打赏
  • 举报
回复
SetThreadAffinityMask The SetThreadAffinityMask function sets a processor affinity mask for a specified thread. A thread affinity mask is a bit vector in which each bit represents the processors that a thread is allowed to run on. A thread affinity mask must be a proper subset of the process affinity mask for the containing process of a thread. A thread is only allowed to run on the processors its process is allowed to run on. DWORD SetThreadAffinityMask ( HANDLE hThread, // handle to the thread of interest DWORD dwThreadAffinityMask // a thread affinity mask ); Parameters hThread A handle to the thread whose affinity mask the function sets. Windows NT: This handle must have the THREAD_SET_INFORMATION access right associated with it. dwThreadAffinityMask Windows NT: Specifies an affinity mask for the thread. Windows 95 and Windows 98: This value must be 1. Return Values If the function succeeds, the return value is nonzero. Windows NT: The return value is the thread's previous affinity mask. Windows 95 and Windows 98: The return value is 1. To succeed, hThread must be valid and dwThreadAffinityMask must be 1. If the function fails, the return value is zero. To get extended error information, call GetLastError. QuickInfo Windows NT: Requires version 3.5 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. See Also Processes and Threads Overview, Process and Thread Functions, GetProcessAffinityMask, SetThreadIdealProcessor
七步777 2016-08-22
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
Multiple Threads in the User Interface http://msdn.microsoft.com/zh-cn/library/ms810439.aspx
感谢回复,但是问题不在这里, 调用的库是在新开的线程里面,该线程主要用于读取卡(插在主板上了)里面的数据,并保存。 调用提供的外部库(里面有锁),有一个保存的方法耗时间长,会出现界面卡死。 我在该线程里面故意写一个while的死循环i++;界面不会卡死, 应该改循环是可被中断的。 现在怀疑是不是新开的线程和UI线程被分配到同一个核上面。所以出现上面的情况。
赵4老师 2016-08-22
  • 打赏
  • 举报
回复
Multiple Threads in the User Interface http://msdn.microsoft.com/zh-cn/library/ms810439.aspx
七步777 2016-08-22
  • 打赏
  • 举报
回复
引用 4 楼 adlay 的回复:
[quote=引用 2 楼 u013421223 的回复:] [quote=引用 1 楼 adlay 的回复:] 你说的线程内是当前处理 UI 消息的线程内还是新开的一个专门调用这个函数的线程? 如果是在 UI 线程里调用当然会卡住了.
新开的[/quote] 主线程里有没有等待那个新开的线程结束,或是等待其它同步对象的动作?[/quote] UI不会等待线程结束,也没有同步。 问了下提供库的人,说是该函数有内部锁,防止被中断。 可能是该应用程序的UI线程和开的线程被分配到一个核上, 所以造成了卡死。但是while循环是可以被中断的。所以界面不会卡死
www_adintr_com 2016-08-22
  • 打赏
  • 举报
回复
引用 2 楼 u013421223 的回复:
[quote=引用 1 楼 adlay 的回复:] 你说的线程内是当前处理 UI 消息的线程内还是新开的一个专门调用这个函数的线程? 如果是在 UI 线程里调用当然会卡住了.
新开的[/quote] 主线程里有没有等待那个新开的线程结束,或是等待其它同步对象的动作?
七步777 2016-08-22
  • 打赏
  • 举报
回复
引用 1 楼 adlay 的回复:
你说的线程内是当前处理 UI 消息的线程内还是新开的一个专门调用这个函数的线程? 如果是在 UI 线程里调用当然会卡住了.
我在新开的线程里面 写了一个while 死循环都不会卡死,只是CPU占用到50%而已,但是在线程里面调用外部库就会出现界面卡几秒
七步777 2016-08-22
  • 打赏
  • 举报
回复
引用 1 楼 adlay 的回复:
你说的线程内是当前处理 UI 消息的线程内还是新开的一个专门调用这个函数的线程? 如果是在 UI 线程里调用当然会卡住了.
新开的
www_adintr_com 2016-08-22
  • 打赏
  • 举报
回复
你说的线程内是当前处理 UI 消息的线程内还是新开的一个专门调用这个函数的线程? 如果是在 UI 线程里调用当然会卡住了.

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧