我是靠谱客的博主 曾经裙子,最近开发中收集的这篇文章主要介绍glib线程实践:跑一个线程前言一 创建线程测试编写测试代码g_atexit函数结束,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

前言

GThread

typedef struct {
} GThread;
The GThread struct represents a running thread. This struct is returned by g_thread_new() or g_thread_try_new(). You can obtain the GThread struct representing the current thread by calling g_thread_self().

GThread is refcounted, see g_thread_ref() and g_thread_unref(). The thread represented by it holds a reference while it is running, and g_thread_join() consumes the reference that it is given, so it is normally not necessary to manage GThread references explicitly.

The structure is opaque -- none of its fields may be directly accessed.

一 创建线程测试

g_thread_new 用于创建线程

g_thread_new ()

GThread *
g_thread_new (const gchar *name,
              GThreadFunc func,
              gpointer data);
This function creates a new thread. The new thread starts by invoking func with the argument data. The thread will run until func returns or until g_thread_exit() is called from the new thread. The return value of func becomes the return value of the thread, which can be obtained with g_thread_join().

The name can be useful for discriminating threads in a debugger. It is not used for other purposes and does not have to be unique. Some systems restrict the length of name to 16 bytes.

If the thread can not be created the program aborts. See g_thread_try_new() if you want to attempt to deal with failures.

If you are using threads to offload (potentially many) short-lived tasks, GThreadPool may be more appropriate than manually spawning and tracking multiple GThreads.

To free the struct returned by this function, use g_thread_unref(). Note that g_thread_join() implicitly unrefs the GThread as well.

Parameters

name
an (optional) name for the new thread.
[allow-none]
func
a function to execute in the new thread
 
data
an argument to supply to the new thread
 
Returns

the new GThread

Since: 2.32

GThreadFunc 函数指针。

GThreadFunc ()

gpointer
(*GThreadFunc) (gpointer data);
Specifies the type of the func functions passed to g_thread_new() or g_thread_try_new().

Parameters

data
data passed to the thread
 
Returns

the return value of the thread

编写测试代码

创建一个线程,并向线程传递一个字符串参数,然后在线程中每隔1秒打印一个计数值

#include <glib.h>
#include <gtimer.h>
#include <stdlib.h>

gpointer my_thread(gpointer data)
{
        int i = 0;
        g_print("data = %s",(char*)data);
        while(i < 10){
                g_usleep(1000 * 1000);//1 second
                i++;
                g_print("i=%dn",i);
        }
        exit(0);
}
int main(void)
{
        GThread *p;
        gpointer arg = "this is parameter";
        g_print("hello gthreadn");
        p = g_thread_new("mythread",my_thread,arg);

        while(1){
                g_usleep(1000);
        }
        return 0;
}

参考下面的内容修改Makefile文件:

LINK_INCLUDE += -I/big/tool/gcc/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/include/glib-2.0/
LINK_INCLUDE += -I/big/tool/gcc/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/include/glib-2.0/glib/
LINK_INCLUDE += -I/big/tool/gcc/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/lib/glib-2.0/include

CC=/big/tool/gcc/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc

LINK_LIB += -L/big/tool/gcc/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/lib

app:main.c
        $(CC) main.c $(LINK_LIB) -lffi -lglib-2.0 -lgobject-2.0 -lgthread-2.0 -lgmodule-2.0 $(LINK_INCLUDE) -o app
        cp app /big/nfsroot/jiaocheng_rootfs/
.PHONY:clean
clean:
        rm -f *.o app

测试结果:

root@hehe:/# ./app
hello gthread
data = this is parameteri=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
root@hehe:/#

g_atexit函数

g_atexit函数在2.32版本以后,就不要了。

g_atexit ()

void
g_atexit (GVoidFunc func);
g_atexit has been deprecated since version 2.32 and should not be used in newly-written code.
It is best to avoid g_atexit().
Specifies a function to be called at normal program termination.

Since GLib 2.8.2, on Windows g_atexit() actually is a preprocessor macro that maps to a call to the atexit() function in the C library. This means that in case the code that calls g_atexit(), i.e. atexit(), is in a DLL, the function will be called when the DLL is detached from the program. This typically makes more sense than that the function is called when the GLib DLL is detached, which happened earlier when g_atexit() was a function in the GLib DLL.

The behaviour of atexit() in the context of dynamically loaded modules is not formally specified and varies wildly.

On POSIX systems, calling g_atexit() (or atexit()) in a dynamically loaded module which is unloaded before the program terminates might well cause a crash at program exit.

Some POSIX systems implement atexit() like Windows, and have each dynamically loaded module maintain an own atexit chain that is called when the module is unloaded.

On other POSIX systems, before a dynamically loaded module is unloaded, the registered atexit functions (if any) residing in that module are called, regardless where the code that registered them resided. This is presumably the most robust approach.

As can be seen from the above, for portability it's best to avoid calling g_atexit() (or atexit()) except in the main executable of a program.

Parameters

func
the function to call on normal program termination.
[scope async]

GVoidFunc 函数指针

GVoidFunc ()

void
(*GVoidFunc) (void);
GVoidFunc is deprecated and should not be used in newly-written code.
Declares a type of function which takes no arguments and has no return value. It is used to specify the type function passed to g_atexit().

结束

最后

以上就是曾经裙子为你收集整理的glib线程实践:跑一个线程前言一 创建线程测试编写测试代码g_atexit函数结束的全部内容,希望文章能够帮你解决glib线程实践:跑一个线程前言一 创建线程测试编写测试代码g_atexit函数结束所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部