1. 单例模式 (多线程下) a. 饿汉式 1 template<class T> 2 class Singleton { 3 public: 4 static T* get_instance() { 5 if( _instance == NULL ) { 6 pthread_mutex_lock( ...
分类:
其他好文 时间:
2016-04-21 15:08:06
阅读次数:
134
1#include<stdio.h>
2#include<pthread.h>
3
4staticintcount=0;
5staticpthread_mutex_tlock=PTHREAD_MUTEX_INITIALIZER;
6void*pthread(void*arg)
7{
8inttmp=0;
9inti=5000;
10while(i--)
11{
12pthread_mutex_lock(&lock);
13tmp=count;
14printf("thisi..
分类:
编程语言 时间:
2016-04-17 18:01:42
阅读次数:
250
#include<pthread.h>
#include<fstream>
#include<iostream>
#include<string>
#include<vector>
usingnamespacestd;
#defineLINE_PER_THREAD1024
pthread_mutex_tcount_mutex=PTHREAD_MUTEX_INITIALIZER;
structparam{
vector<string>..
分类:
其他好文 时间:
2016-04-17 09:08:11
阅读次数:
138
#include<pthread.h>
#include<iostream>
usingnamespacestd;
intglobal=0;
#defineNUMTHREADS2
pthread_mutex_tmutexnum;
structthread_data{
intidx;
};
structthread_datathread_data_array[NUMTHREADS];
void*assign_value(void*param){
structthread_da..
分类:
编程语言 时间:
2016-04-16 11:02:51
阅读次数:
121
#include
#include "errors.h"
typedef struct stage_tag{ //流水线的工作单元结构体
pthread_mutex_t mutex; //保护当前工作单元数据的互斥锁
pthread_cond_t avail; //等待当前工作单元存储数据可用的条件变量
pthread_cond_t ready; //等待当前工作单元可处理...
分类:
编程语言 时间:
2016-03-26 07:33:59
阅读次数:
255
pthread_cond_t my_condition = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁和条件等待 解决生产者和消费者模型 条件等待模型//socket连接池。。
分类:
系统相关 时间:
2016-03-17 14:39:39
阅读次数:
175
pthread_mutex_lock() pthread_mutex_unlock() 当该函数返回时,该互斥锁已经被锁定。线程调用该函数让互斥锁上锁,如果该互斥锁已被另一个线程锁定和拥有,则调用该线程将阻塞,直到该互斥锁为可用为止; 头文件为: #include <pthread.h> pthre
分类:
其他好文 时间:
2016-03-11 11:47:47
阅读次数:
145
三种线程同步机制 ?互斥锁 ?信号量 ?条件变量 pthread_t thread_id; 主要函数 pthread_create(),pthread_exit(),pthread_join(),pthread_cancel() pthread_mutex_t mutex; 互斥锁主要包括下面的基本
分类:
编程语言 时间:
2016-03-03 01:21:09
阅读次数:
177
1.定义一个任务结构体,和一个线程池结构体 struct task{ void *(*p)(void*);//需要实现的函数; void *arg;//函数所带的参数 struct task *next;};struct pthread_pool{ pthread_mutex_t mutex;//线
分类:
编程语言 时间:
2016-03-01 22:15:26
阅读次数:
230
编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。使用条件变量来实现:#include #include #include static pthread_mutex_t mtx=PT...
分类:
编程语言 时间:
2016-01-24 18:14:30
阅读次数:
144