主题 : linux2.6.29+qtopia2.2.0 如何实现多线程编程呢? 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 6261
精华: 0
发帖: 13
金钱: 130 两
威望: 130 点
贡献值: 0 点
综合积分: 26 分
注册时间: 2009-05-25
最后登录: 2010-07-27
楼主  发表于: 2009-06-29 15:37

 linux2.6.29+qtopia2.2.0 如何实现多线程编程呢?

管理提醒: 本帖被 arm9home 从 Linux技术交流专区 移动到本区(2009-09-10)
最近在做qt开发,需要用到多线程编程,不知道目前友善之臂提供的linux2.6.29+qtopia2.2.0能不能实现多线程编程?如何实现?
有做过的大侠指教!!!急
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2009-06-30 17:05
引用第6楼l0p0c于2009-06-30 13:41发表的  :
我可以肯定,qtopia是支持多线程的。就看配置成什么样子了!

如果你说的多线程指的是libqte-mt.so的话,qtopia-2.2.0是支持的,但前提是你需要重新编译其中的qte以生成libqte-mt.so, 这可以通过在qte的配置上加上'-thread'参数做到。
这是在qtopia-2.2.0-FriendlyARM/qt2目录下./configure -help的输出

Usage: ./configure [-debug] [-release] [-shared] [-static] [-gif] [-no-gif] \
          [-sm] [-no-sm] [-thread] [-no-thread] [-qt-zlib] [-system-zlib] \
          [-qt-libpng] [-system-libpng] [-no-jpeg] [-system-jpeg] \
          [-no-<module>] [-kde] [-Istring] [-Lstring] [-Rstring] [-lstring]

The defaults (*) are usually acceptable.  Here is a short explanation of
each option:

*  -release ........... Compile and link Qt with debugging turned off.
    -debug ............. Compile and link Qt with debugging turned on.

*  -shared ............ Create and use a shared Qt library (libqt.so)
    -static ............ Create and use a static Qt library (libqt.a)

*  -no-gif ............ Do not compile in GIF reading support.
    -gif ............... Compile in GIF reading support. See src/kernel/qgif.h

    -no-sm ............. Do not support X Session Management.
*  -sm ................ Support X Session Management, links in -lSM -lICE.

*  -no-thread ......... Do not compile with Threading Support
    -thread ............ Compile with Threading Support


*  -qt-zlib ........... Use the zlib bundled with Qt.
    -system-zlib ....... Use a zlib from the operating system
                         http://www.info-zip.org/pub/infozip/zlib

*  -qt-libpng ......... Use the libpng bundled with Qt.
    -system-libpng ..... Use a libpng from the operating system.
                         See http://www.libpng.org/pub/png

*  -no-mng ............ Do not compile in MNG I/O support.
    -system-libmng ..... Use libmng from the operating system.
                         See http://www.libmng.com

*  -no-jpeg ........... Do not compile in JPEG I/O support.
    -system-jpeg ....... Use jpeglib from the operating system.
                         See http://www.ijg.org

*  -no-nas-sound ...... Do not compile in NAS sound support.
    -system-nas-sound .. Use NAS libaudio from the operating system.
                         See http://radscan.com/nas.html

    -no-<module> ....... Disables a module, where module can
                         can be one of: opengl table network canvas

    -kde ............... Builds the Qt Designer with KDE 2 support, so that
                         KDE 2 widgets can be used directly in
                         the Qt Designer. $KDEDIR must be
                         set to point to a KDE 2 installation.
                         See http://www.kde.org

    -tslib ............. Enable TSlib (touchscreen library) mouse handler.
                         See http://arm.linux.org.uk

    -no-g++-exceptions . Disable exceptions on platforms using the GNU C++
                         compiler by using the -fno-exceptions flag.

    -no-xft ............ Disable support for Anti-Aliased fonts through the
                         Xft extension library (XFree86 4.0.2 and newer).
    -xft ............... Enable support for Anti-Aliased fonts.

                         Xft support is auto-detected, but you may use these
                         flags to explicitly enable/disable support.

    -platform target ... The platform you are building on (linux-g++)
    -xplatform target .. The platform when cross-compiling.

                         See the PLATFORMS file for a list of supported
                         operating systems and compilers.

    -Istring ........... Add an explicit include path.
    -Lstring ........... Add an explicit library path.
    -Rstring ........... Add an explicit dynamic library runtime search path.
    -lstring ........... Add an explicit library.


Qt/Embedded only:

    -embedded .......... Enable building of Qt/Embedded.

    -qconfig local ..... Use src/tools/qconfig-local.h rather than the
                         default (qconfig.h).

    -depths list ....... Comma-separated list of supported bit-per-pixel
                         depths, from:  v, 4, 8, 16, 18, 24, and 32.  'v' is VGA16.

    -accel-snap ........ Enable SciTech SNAP Graphics acceleration.
    -accel-voodoo3 ..... Enable Voodoo3 acceleration.
    -accel-mach64 ...... Enable Mach64 acceleration.
    -accel-matrox ...... Enable Matrox MGA acceleration.
    -qvfb .............. Enable X11-based Qt Virtual Frame Buffer.
    -vnc ............... Enable VNC server (requires network module).
    -keypad-mode ....... Enable keypad widget navigation.
    -keypad-input ...... Include keypad keys.
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
2楼  发表于: 2009-07-01 10:08
引用第9楼26672624于2009-06-30 19:02发表的  :
加了也没用,Qt/Embedded 2.0的多线程对Qtopia是无效的

哦是吗?能否为大家解释一下?
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
3楼  发表于: 2009-07-01 15:44
引用第12楼26672624于2009-07-01 13:42发表的  :


Qt之初是为PC Linux的X11, MacOS等系统设计的,后来才发展了嵌入式版本,在配置上依然保留了统一的配置选项,如果你加了多线程库的支持选项,可能无法编译通过,也可能会对整个系统造成影响。
Qtopia自带的视频播放器的多线程使用的是Linux系统本身的,而不是Qt的。Qtopia编程可以基于事件驱动,不清楚很多初学者为何钟情于多线程。对于大多数的程序,如果不是十分擅长编程,特别是Qtopia编程,尽量不要使用多线程,它将到处布满陷阱,到时出了问题也很难debug,因为多线程的错误很多都是没有规律性的。

事实上,QT Embedded中的QThread class在Linux/Unix就是通过pthread(POSIX Thread)实现的,看看qtopia-2.2.0源代码中的qthread_p.h和qthread_unix.cpp就知道了。QThread只是包装了pthread的API更方便用户使用而已。对于熟悉thread编程的开发者来说,QThread不失为QT提供的一个很方便的工具。
至于thread编程容易引起一些比较难debug的问题,这一点我同意,这主要是由多线程调度引起的。要解决这些问题,开发者必须熟悉线程/进程调度(在Linux上,pthread就是一个独立的进程)以及与调度相关的一些基本工具,比如信号量,mutex等的基本概念。
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
4楼  发表于: 2010-02-02 12:18

 Re:回 8楼(kasim) 的帖子

引用第17楼liangruisan于2010-02-01 13:00发表的 回 8楼(kasim) 的帖子 :
高手,怎么“这可以通过在qte的配置上加上'-thread'参数做到”做才能有效果,能够在详细点吗?谢谢了,我这个问题纠结了很久。谢谢了

由于qtopia 2.2.0的makefile里的问题,你现在只能自己手动编译出支持thread的libqte-mt.so. 具体做法如下:

1. 按照手册上的步骤,用“build”或者"build-all"脚本做一次完整的qtopia 2.2.0的编译;

2. 在解压出来的qtopia-2.2.0-FriendlyARM目录下执行
$source setQteEnv

3. 进入qtopia-2.2.0-FriendlyARM/qt2目录,清除之前编译生成的文件然后手动配置qt embedded以支持multi-thread:
$cd qt2
$make clean
$echo yes |./configure -embedded -no-xft -qconfig qpe -depths 16,32 -system-jpeg -gif -thread

注意configure之后的参数与x86-qtopia或者arm-qtopia目录下的“build“脚本里的”echo yes | ./configure -qte“之后的参数保持一致, 只是在最后加上"-thread"

4. 上面的步骤执行后会在当前目录下产生src-mt.mk文件,用编辑器将其内容的最后一行”cd src; $(MAKE) clean“删除或注释掉;

5. 编译libqte-mt
$make src-mt

编译完成后将在qtopia-2.2.0-FriendlyARM/qtopia/lib目录下生成所期待的libqte-mt.so.*文件。用nm命令可以看到现在qt embedded中就已经加入了QThread支持:
alex@aling-xw4400:~/x86-qtopia/qtopia-2.2.0-FriendlyARM/qt2$ nm ../qtopia/lib/libqte-mt.so.2.3.12 |grep -i QThread
0015deb0 t _GLOBAL__I_qthread_unix.cpp
0039d0a0 b _ZL23qthreadposteventprivate
0039d094 b _ZL40QThreadPostEventPrivate_metaObj_unloader
0015ec40 T _ZN23QThreadPostEventPrivate10sendEventsEv
...
0038f1a0 V _ZTV23QThreadPostEventPrivate
0038f240 V _ZTV5QListI14QThreadQtEventE
0038f208 V _ZTV7QThread
0038f280 V _ZTV8QPtrDictI7QThreadE
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
5楼  发表于: 2010-05-07 14:57
g++  -o label_fbli .obj/release-shared/main.o .obj/release-shared/label_fbli_base.o .obj/release-shared/moc_label_fbli_base.o   -lqtopia2 -lqtopia -lqpe -L/opt/FriendlyARM/mini2440/x86-qtopia/qtopia-2.2.0-FriendlyARM/qtopia/lib -L/opt/FriendlyARM/mini2440/x86-qtopia/qtopia-2.2.0-FriendlyARM/qt2/lib -lqte
/opt/FriendlyARM/mini2440/x86-qtopia/qtopia-2.2.0-FriendlyARM/qtopia/lib/libqte.so: undefined reference to `pthread_mutexattr_destroy'

你需要在命令行上加上"-lpthread"来链接libpthread.so
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
6楼  发表于: 2010-05-08 16:38
不管qmake.conf里面定义了什么,只有实际加到gcc, g++命令行上的参数才有意义。为什么不回头再看一眼g++实际调用的命令行呢?
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
7楼  发表于: 2010-05-08 18:51
我的理解是g++带的参数选项命令是在qmake.conf定义好的

如果是这样的话,那qmake.conf里的定义应该会在某个Makefile里调用,去把它找出来,然后看看为什么QMAKE_LIBS_THREAD没有被用到。
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."