pthread_mutex_lock
2011-10-23 00:20
337 查看
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <iostream>
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
struct node {
int n_number;
struct node *n_next;
} *head = NULL;
/*[thread_func]*/
static void cleanup_handler(void *arg)
{
printf("Cleanup handler of second thread./n");
free(arg);
(void)pthread_mutex_unlock(&mtx);
}
static void *thread_func(void *arg)
{
struct node *p = NULL;
pthread_cleanup_push(cleanup_handler, p);
while (1) {
pthread_mutex_lock(&mtx);
while (head == NULL) {
pthread_cond_wait(&cond, &mtx);
}
p = head;
head = head->n_next;
printf("Got %d from front of queue/n", p->n_number);
free(p);
pthread_mutex_unlock(&mtx);
}
pthread_cleanup_pop(0);
return 0;
}
int main(void)
{
pthread_t tid;
int i;
struct node *p;
pthread_create(&tid, NULL, thread_func, NULL);
for (i = 0; i < 10; i++) {
p = (node*)malloc(sizeof(struct node));
p->n_number = i;
pthread_mutex_lock(&mtx);
p->n_next = head;
head = p;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
sleep(1);
}
printf("thread 1 wanna end the line.So cancel thread 2./n");
pthread_cancel(tid);
pthread_join(tid, NULL);
printf("All done -- exiting/n");
return 0;
}
#include <unistd.h>
#include <string.h>
#include <iostream>
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
struct node {
int n_number;
struct node *n_next;
} *head = NULL;
/*[thread_func]*/
static void cleanup_handler(void *arg)
{
printf("Cleanup handler of second thread./n");
free(arg);
(void)pthread_mutex_unlock(&mtx);
}
static void *thread_func(void *arg)
{
struct node *p = NULL;
pthread_cleanup_push(cleanup_handler, p);
while (1) {
pthread_mutex_lock(&mtx);
while (head == NULL) {
pthread_cond_wait(&cond, &mtx);
}
p = head;
head = head->n_next;
printf("Got %d from front of queue/n", p->n_number);
free(p);
pthread_mutex_unlock(&mtx);
}
pthread_cleanup_pop(0);
return 0;
}
int main(void)
{
pthread_t tid;
int i;
struct node *p;
pthread_create(&tid, NULL, thread_func, NULL);
for (i = 0; i < 10; i++) {
p = (node*)malloc(sizeof(struct node));
p->n_number = i;
pthread_mutex_lock(&mtx);
p->n_next = head;
head = p;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
sleep(1);
}
printf("thread 1 wanna end the line.So cancel thread 2./n");
pthread_cancel(tid);
pthread_join(tid, NULL);
printf("All done -- exiting/n");
return 0;
}
相关文章推荐
- pthread_mutex_lock
- pthread_mutex_lock的作用
- ubuntu环境无pthread_mutex_lock 函数 man 手册
- pthread_mutex_lock 用法
- man pthread_mutex_init 或 man pthread_mutex_lock 没有结果的解决的方法
- pthread_mutex_xx and pthread_spin_lock
- 用gdb脚本解决死锁的调试方法(由pthread_mutex_lock引起的死锁)
- pthread_mutex_lock的实现!!
- android4.0 编译undefined reference to `pthread_mutex_trylock'
- pthread 线程库编程 一:线程与互斥锁 mutex_lock
- pthread mutex vs spinlock
- 线程相关函数(4)-pthread_mutex_lock(), pthread_mutex_unlock() 互斥锁
- pthread_mutex_lock引起的core
- pthread_mutex_lock
- POSIX详解pthread_mutex_lock
- pthread_mutex_xx 与 pthread_spin_lock的区别和联系
- linux基础编程 多线程中的互斥锁 pthread_mutex_lock
- pthread_mutex_lock
- POSIX详解pthread_mutex_lock
- 多线程编程:用互斥锁同步资源(pthread_mutex_lock测试加锁)