摘要: 因为类似的问题至少碰到3次以上,虽然很简单但是每次都花了不少时间来定位,所以决定写个demo来演示一下:)程序逻辑比较简单,主线程读入一个整数,分别有两个线程对这个整数求1~n的累加和。代码如下:#include<iostream>#include<pthread.h>#include<string>#include<unistd.h>usingnamespacestd;intNumber=0;pthread_mutex_tNMutex;pthread_cond_tNCond;void*thread1(void*arg){pthread_deta 阅读全文
posted @ 2011-07-28 13:55 wangkangluo1 阅读(1321) 评论(1) 推荐(0) 编辑
摘要: 参考地址:http://blog.csdn.net/jiazhen/article/details/4293754#include <stdio.h> #include <stdlib.h> #include <semaphore.h> #include <pthread.h> sem_t smt; void DisplayData() { printf ("Display a frame/n"); } void *ThreadFuncDisplay(void *arg) { while (1){ printf (" 阅读全文
posted @ 2011-07-28 11:55 wangkangluo1 阅读(701) 评论(0) 推荐(0) 编辑
摘要: 参考地址:http://hi.baidu.com/luxey/blog/item/41c802085918a1980a7b8292.html#include<unistd.h>#include<signal.h>void handler() { printf("hello\n");}main(){ int i; signal(SIGALRM,handler); alarm(5); for(i=1;i<7;i++) { printf("sleep %d ...\n",i); sleep(1); }} 1. alarm(设置信号 阅读全文
posted @ 2011-07-28 11:21 wangkangluo1 阅读(1203) 评论(0) 推荐(0) 编辑
摘要: 原文地址:下午实验编写了使用pthread_kill函数检测一个线程是否还活着的程序,在linux环境下gcc编译通过,现将代码贴在下面:/*******************************pthread_kill.c*******************************//******************************* pthread_kill.c *******************************/#include <stdio.h>#include <stdlib.h>#include <pthread.h&g 阅读全文
posted @ 2011-07-28 11:16 wangkangluo1 阅读(4597) 评论(0) 推荐(0) 编辑