许多互斥对象如果放置了过多的互斥对象,代码就没有什么并发性可言,运行起来也比单线程解决方案慢。如果放置了过少的互斥对象,代码将出现奇怪和令人尴尬的错误。幸运的是,有一个中间立场。首先,互斥对象是用于串行化存取*共享数据*。不要对非共享数据使用互斥对象,并且,如果程序逻辑确保任何时候都只有一个线程能存...
分类:
其他好文 时间:
2014-07-26 00:27:46
阅读次数:
201
http://www.blogjava.net/fhtdy2004/archive/2009/07/05/285519.html线程同步:何时互斥锁不够,还需要条件变量?很显然,pthread中的条件变量与Java中的wait,notify类似假设有共享的资源sum,与之相关联的mutex 是loc...
分类:
编程语言 时间:
2014-07-26 00:21:16
阅读次数:
249
1 #include 2 #include 3 #include 4 #include 5 6 7 void *thread_foo_func(void *); 8 void *thread_bar_func(void *); 9 10 11 int global = 4;12 13 ...
分类:
其他好文 时间:
2014-07-25 14:00:51
阅读次数:
252
#include class CTestLock { public: CTestLock() { pthread_mutex_init(&mutex_t_, NULL); pthread_cond_init(&cond_t_, NULL); ...
分类:
其他好文 时间:
2014-07-18 12:01:59
阅读次数:
202
#includepthread_t pthread_self(void)功能:获取调用线程的thread identifer例如:thread_id.c运行结果:清除:线程终止有两种情况:正常终止和非正常终止当某段代码可能出现不可预料的终止时,可以用pthread_cleanup_push和pthr...
分类:
编程语言 时间:
2014-07-18 09:19:15
阅读次数:
269
每个线程都有自己的信号屏蔽字,但是信号的处理是进程中所有线程共享的。进程中的信号是递送到单个线程的。线程中pthread_sigmask函数类似与进程的sigprocmask函数,可以用来阻塞信号。#include int pthread_sigmask(int how,const sigset_t...
分类:
编程语言 时间:
2014-07-18 08:34:25
阅读次数:
325
1.pthread_create function creates thread that share the same memory with the process.2.pthread_join function wait for threads until they stop3.The pth...
分类:
系统相关 时间:
2014-07-18 00:19:54
阅读次数:
317
pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t*mutex)函数
传入的参数mutex用于保护条件,因为我们在调用pthread_cond_wait时,如果条件不成立我们就进入阻塞,但是进入阻塞这个期间,如果条件变量改变了的话,那我们就漏掉了这个条件。因为这个线程还没有放到等待队列上,所以调用pthread_cond_wait前要先...
分类:
编程语言 时间:
2014-07-17 20:24:12
阅读次数:
236
#include
#include
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化条件变量*/
void *thread1(void *);
void *thread2(void *)...
分类:
其他好文 时间:
2014-07-17 20:10:40
阅读次数:
218
在线程实际运行过程中,我们经常需要多个线程保持同步。这时可以用互斥锁来完成任务;互斥锁的使用过程中,主要有pthread_mutex_init,pthread_mutex_destory,pthread_mutex_lock,pthread_mutex_unlock这几个函数以完成锁的初始化,锁的销毁,上锁和释放锁操作。
一,锁的创建
锁可以被动态或静态创建,可以用宏PTHRE...
分类:
编程语言 时间:
2014-07-17 16:38:59
阅读次数:
303