libevent提取里面的多线程实现

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#define EVTHREAD_WRITE 0x04
/** A flag passed to a locking callback when the lock was allocated as a
 * read-write lock, and we want to acquire or release the lock for reading. */
#define EVTHREAD_READ 0x08
/** A flag passed to a locking callback when we don't want to block waiting
 * for the lock; if we can't get the lock immediately, we will instead
 * return nonzero from the locking callback. */
#define EVTHREAD_TRY    0x10




#define EVTHREAD_LOCK_API_VERSION 1


/**
   @name Types of locks


   @{*/
/** A recursive lock is one that can be acquired multiple times at once by the
 * same thread.  No other process can allocate the lock until the thread that
 * has been holding it has unlocked it as many times as it locked it. */
#define EVTHREAD_LOCKTYPE_RECURSIVE 1
/* A read-write lock is one that allows multiple simultaneous readers, but
 * where any one writer excludes all other writers and readers. */
#define EVTHREAD_LOCKTYPE_READWRITE 2
/**@}*/


/** This structure describes the interface a threading library uses for
 * locking.   It's used to tell evthread_set_lock_callbacks() how to use
 * locking on this platform.
 */
 
#define mm_malloc(sz) malloc(sz)
#define mm_calloc(n, sz) calloc((n), (sz))
#define mm_strdup(s) strdup(s)
#define mm_realloc(p, sz) realloc((p), (sz))
#define mm_free(p) free(p)
 
struct evthread_lock_callbacks {
/** The current version of the locking API.  Set this to
* EVTHREAD_LOCK_API_VERSION */
int lock_api_version;
/** Which kinds of locks does this version of the locking API
* support?  A bitfield of EVTHREAD_LOCKTYPE_RECURSIVE and
* EVTHREAD_LOCKTYPE_READWRITE.
*
* (Note that RECURSIVE locks are currently mandatory, and
* READWRITE locks are not currently used.)
**/
unsigned supported_locktypes;
/** Function to allocate and initialize new lock of type 'locktype'.
* Returns NULL on failure. */
void *(*alloc)(unsigned locktype);
/** Funtion to release all storage held in 'lock', which was created
* with type 'locktype'. */
void (*free)(void *lock, unsigned locktype);
/** Acquire an already-allocated lock at 'lock' with mode 'mode'.
* Returns 0 on success, and nonzero on failure. */
int (*lock)(unsigned mode, void *lock);
/** Release a lock at 'lock' using mode 'mode'.  Returns 0 on success,
* and nonzero on failure. */
int (*unlock)(unsigned mode, void *lock);
};


static pthread_mutexattr_t attr_recursive;


static void *
evthread_posix_lock_alloc(unsigned locktype)
{
printf("evthread_posix_lock_alloc\n");
pthread_mutexattr_t *attr = NULL;
pthread_mutex_t *lock = (pthread_mutex_t *)mm_malloc(sizeof(pthread_mutex_t));
if (!lock)
return NULL;
if (locktype & EVTHREAD_LOCKTYPE_RECURSIVE)
attr = &attr_recursive;
if (pthread_mutex_init(lock, attr)) {
mm_free(lock);
return NULL;
}
printf("evthread_posix_lock_alloc end\n");
return lock;
}


static void
evthread_posix_lock_free(void *lock_, unsigned locktype)
{
pthread_mutex_t *lock = (pthread_mutex_t *)lock_;
pthread_mutex_destroy(lock);
mm_free(lock);
}


static int
evthread_posix_lock(unsigned mode, void *lock_)
{
pthread_mutex_t *lock = (pthread_mutex_t *)lock_;
if (mode & EVTHREAD_TRY)
return pthread_mutex_trylock(lock);
else
return pthread_mutex_lock(lock);
}


static int
evthread_posix_unlock(unsigned mode, void *lock_)
{
pthread_mutex_t *lock = (pthread_mutex_t *)lock_;
return pthread_mutex_unlock(lock);
}


struct evthread_lock_callbacks evthread_lock_fns_ = {
1,
1,
evthread_posix_lock_alloc,
evthread_posix_lock_free,
evthread_posix_lock,
evthread_posix_unlock
};
#define EVTHREAD_ALLOC_LOCK(lockvar, locktype) \

((lockvar) = evthread_lock_fns_.alloc ?\

   evthread_lock_fns_.alloc(locktype) : NULL)

#define EVLOCK_LOCK(lockvar,mode) \
do { \
if (lockvar) \
evthread_lock_fns_.lock(mode, lockvar);\
} while (0)
#define EVLOCK_UNLOCK(lockvar,mode) \
do { \
if (lockvar) \
evthread_lock_fns_.unlock(mode, lockvar);\
} while (0)



typedef struct test
{
void *lock;
int k;

}test_t;
void* my_thread(void *t)
{
printf("my_thread\n");
int i = 100;
test_t *test = (test_t*)t;

while(i--)
{
EVLOCK_LOCK(test->lock, 0);
printf("my_thread pthread_self :%d k: %d\n", pthread_self(), test->k++);
EVLOCK_UNLOCK(test->lock, 0);

srand(((unsigned)time(NULL))%1000);
int k = rand()%100000;
usleep(k);
}

}
int main()
{
test_t base;
base.k = 0;
pthread_t tid;
int ret;
EVTHREAD_ALLOC_LOCK(base.lock, 1);


if(base.lock != NULL)
{
for(int i = 0; i < 10; i++)
{
ret = pthread_create(&tid, NULL, my_thread, (void*)&base);
}
}
//pthread_join(tid, NULL);
while(1)
{
sleep(100);
}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值