GMutex / GCond

os posted @ 2013年9月23日 14:59 in Glib , 5040 阅读

下面两个函数:
void g_cond_signal (GCond *cond);
void g_cond_wait (GCond *cond, GMutex *mutex);
 

用于进行线程同步。
mutex 是“mutual exclusion”(互斥)的英文缩写,用来保证线程对于共享数据的独占访问。

 

GMutex *mutex; // 互斥变量

GCond *cond; //等待条件

 

CRITICAL_SECTION 属于轻量级的线程同步对象,相对于mutex来说,它的效率会高很多。

mutex可以用于进程之间的同步,CRITICAL_SECTION只在同一个进程有效。

 

同一进程可以包括多个线程,这些线程共享相同的内存空间,而进程都有各自独立的内存空间,进程之间通信需要专门的机制,这无疑增加了内核的开销,降低了系 统性能。线程带来的开销很小,内核无需单独复制进程的内存空间或文件描述符等,这就大量地节省了CPU时间,使得创建线程比进程的速度快数十倍。另外,多 线程程序作为一种多任务、并发的工作方式,还有以下的优点:1)提高应用程序响应时间;2)使多CPU系统更加有效;3)改善程序结构。
首先我们理清一下Pthread和Gthread的区别。Pthread即POSIX thread,Posix线程是一个POSIX标准线程,该标准定义内部API创建和操纵线程。Gthread调用的是Glib库中的线程部分;GLib是GTK+和GNOME工程的基础底层核心程序库,是一个综合用途的实用的轻量级的C程序库。 本软件是带有界面的,并且是GTK+界面,因此,Gthread是最好的选择。
引入Gthread线程的文件在编译时要加入参数`pkg-config --cflags --libs gthread-2.0`。
(1)gboolean g_thread_supported();/*测试是否支持多线程*/
(2)void g_thread_init (GThreadFunctions *vtable);/*初始化多线程支持*/
(3)void gdk_threads_init (void);/*初始化GDK多线程支持*/
(4)void gdk_threads_enter (void);/*进入多线程互斥区域*/
(5)void gdk_threads_leave (void);/*退出多线程互斥区域*/
(6)GThread * g_thread_create (GThreadFunc func, gpointer data, gboolean joinable, GError **error);
这是创建线程函数,func是线程执行的外部函数,data是传给该外部函数的参数,joinable标志线程是否可分离,error是出错代码返回地址。
(7)void g_thread_exit (gpointer retval);/*线程退出,retval为返回状态值*/
(8)GMutex *g_mutex_new ();/*返回一个新的互斥锁*/
(9) void g_mutex_lock(GMutex *mutex);/*上锁*/
(10)void g_mutex_unlock (GMutex *mutex);/*解锁*/
(11)GCond* g_cond_new ();/*返回一个新的信号量*/
(12)void g_cond_signal (GCond *cond);/*释放信号量cond*/
(13)void g_cond_wait(GCond *cond, GMutex *mutex);/*等待信号量cond*/

 

#define g_mutex_new()            G_THREAD_UF (mutex_new,      ())
#define G_THREAD_UF(op, arglist)					\
    (*g_thread_functions_for_glib_use . op) arglist

#define g_mutex_new()            (*g_thread_functions_for_glib_use.mutex_new())

 

#define g_cond_new()             G_THREAD_UF (cond_new,       ())

#define g_cond_new()             (*g_thread_functions_for_glib_use.cond_new())

 

typedef gpointer (*GThreadFunc) (gpointer data);

typedef enum
{
  G_THREAD_PRIORITY_LOW,
  G_THREAD_PRIORITY_NORMAL,
  G_THREAD_PRIORITY_HIGH,
  G_THREAD_PRIORITY_URGENT
} GThreadPriority;

typedef struct _GThread         GThread;
struct  _GThread
{
  /*< private >*/
  GThreadFunc func;
  gpointer data;
  gboolean joinable;
  GThreadPriority priority;
};

typedef struct _GMutex          GMutex;
typedef struct _GCond           GCond;
typedef struct _GPrivate        GPrivate;
typedef struct _GStaticPrivate  GStaticPrivate;

typedef struct _GThreadFunctions GThreadFunctions;
struct _GThreadFunctions
{
  GMutex*  (*mutex_new)           (void);
  void     (*mutex_lock)          (GMutex               *mutex);
  gboolean (*mutex_trylock)       (GMutex               *mutex);
  void     (*mutex_unlock)        (GMutex               *mutex);
  void     (*mutex_free)          (GMutex               *mutex);
  GCond*   (*cond_new)            (void);
  void     (*cond_signal)         (GCond                *cond);
  void     (*cond_broadcast)      (GCond                *cond);
  void     (*cond_wait)           (GCond                *cond,
                                   GMutex               *mutex);
  gboolean (*cond_timed_wait)     (GCond                *cond,
                                   GMutex               *mutex,
                                   GTimeVal             *end_time);
  void      (*cond_free)          (GCond                *cond);
  GPrivate* (*private_new)        (GDestroyNotify        destructor);
  gpointer  (*private_get)        (GPrivate             *private_key);
  void      (*private_set)        (GPrivate             *private_key,
                                   gpointer              data);
  void      (*thread_create)      (GThreadFunc           func,
                                   gpointer              data,
                                   gulong                stack_size,
                                   gboolean              joinable,
                                   gboolean              bound,
                                   GThreadPriority       priority,
                                   gpointer              thread,
                                   GError              **error);
  void      (*thread_yield)       (void);
  void      (*thread_join)        (gpointer              thread);
  void      (*thread_exit)        (void);
  void      (*thread_set_priority)(gpointer              thread,
                                   GThreadPriority       priority);
  void      (*thread_self)        (gpointer              thread);
  gboolean  (*thread_equal)       (gpointer              thread1,
				   gpointer              thread2);
};

 

定义一些宏来访问这些成员变量:

/* shorthands for conditional and unconditional function calls */

#define G_THREAD_UF(op, arglist)					\
    (*g_thread_functions_for_glib_use . op) arglist
#define G_THREAD_CF(op, fail, arg)					\
    (g_thread_supported () ? G_THREAD_UF (op, arg) : (fail))
#define G_THREAD_ECF(op, fail, mutex, type)				\
    (g_thread_supported () ? 						\
      ((type(*)(GMutex*, const gulong, gchar const*))			\
      (*g_thread_functions_for_glib_use . op))				\
     (mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (fail))

#ifndef G_ERRORCHECK_MUTEXES
# define g_mutex_lock(mutex)						\
    G_THREAD_CF (mutex_lock,     (void)0, (mutex))
# define g_mutex_trylock(mutex)						\
    G_THREAD_CF (mutex_trylock,  TRUE,    (mutex))
# define g_mutex_unlock(mutex)						\
    G_THREAD_CF (mutex_unlock,   (void)0, (mutex))
# define g_mutex_free(mutex)						\
    G_THREAD_CF (mutex_free,     (void)0, (mutex))
# define g_cond_wait(cond, mutex)					\
    G_THREAD_CF (cond_wait,      (void)0, (cond, mutex))
# define g_cond_timed_wait(cond, mutex, abs_time)			\
    G_THREAD_CF (cond_timed_wait, TRUE,   (cond, mutex, abs_time))
#else /* G_ERRORCHECK_MUTEXES */
# define g_mutex_lock(mutex)						\
    G_THREAD_ECF (mutex_lock,    (void)0, (mutex), void)
# define g_mutex_trylock(mutex)						\
    G_THREAD_ECF (mutex_trylock, TRUE,    (mutex), gboolean)
# define g_mutex_unlock(mutex)						\
    G_THREAD_ECF (mutex_unlock,  (void)0, (mutex), void)
# define g_mutex_free(mutex)						\
    G_THREAD_ECF (mutex_free,    (void)0, (mutex), void)
# define g_cond_wait(cond, mutex)					\
    (g_thread_supported () ? ((void(*)(GCond*, GMutex*, gulong, gchar*))\
      g_thread_functions_for_glib_use.cond_wait)			\
        (cond, mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (void) 0)
# define g_cond_timed_wait(cond, mutex, abs_time)			\
    (g_thread_supported () ?						\
      ((gboolean(*)(GCond*, GMutex*, GTimeVal*, gulong, gchar*))	\
        g_thread_functions_for_glib_use.cond_timed_wait)		\
          (cond, mutex, abs_time, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : TRUE)
#endif /* G_ERRORCHECK_MUTEXES */

#if defined(G_THREADS_ENABLED) && defined(G_THREADS_MANDATORY)
#define g_thread_supported()     1
#else
#define g_thread_supported()    (g_threads_got_initialized)
#endif
#define g_mutex_new()            G_THREAD_UF (mutex_new,      ())
#define g_cond_new()             G_THREAD_UF (cond_new,       ())
#define g_cond_signal(cond)      G_THREAD_CF (cond_signal,    (void)0, (cond))
#define g_cond_broadcast(cond)   G_THREAD_CF (cond_broadcast, (void)0, (cond))
#define g_cond_free(cond)        G_THREAD_CF (cond_free,      (void)0, (cond))
#define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
#define g_private_get(private_key) G_THREAD_CF (private_get, \
                                                ((gpointer)private_key), \
                                                (private_key))
#define g_private_set(private_key, value) G_THREAD_CF (private_set, \
                                                       (void) (private_key = \
                                                        (GPrivate*) (value)), \
                                                       (private_key, value))
#define g_thread_yield()              G_THREAD_CF (thread_yield, (void)0, ())

#define g_thread_create(func, data, joinable, error)			\
  (g_thread_create_full (func, data, 0, joinable, FALSE, 		\
                         G_THREAD_PRIORITY_NORMAL, error))

 

 

Avatar_small
full part time maids 说:
2021年9月08日 16:39

Settees are huge and heavy. To clear them properly you must pull out there the couch cushions and also vacuum each inch and also brush apart any crumbs as well as other bits who have fallen powering and collected inside the corners and over the edges.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter