对原帖稍微改了一些小地方。
- /*********
-
多线程编程
-
**********/
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <pthread.h>
-
-
void *mythread2()
-
{
-
int i;
-
for(i=0;i<3;i++)
-
printf("This is a pthread 2:\n");
-
pthread_exit("pthread 2 Thank you for the CPU time\n");
-
}
-
void *mythread1()
-
{
-
int i;
-
for(i=0;i<3;i++)
-
printf("This is a pthread 1:\n");
-
pthread_exit("pthread 1 Thank you for the CPU time\n");
-
}
-
int main(int argc,char **argv)
-
{
-
pthread_t id1,id2;
-
int i, ret;
-
void *thread_result;
-
ret = pthread_create(&id2,NULL,mythread2,NULL);
-
if(ret!=0)
-
{
- printf("Create pthread 2 failed\n");
-
exit(0);
-
}
-
ret = pthread_create(&id1,NULL,mythread1,NULL);
-
if(ret!=0)
-
{
- printf("Create pthread 1 failed\n");
-
exit(0);
-
}
-
-
for(i=0;i<3;i++)
-
printf("This is the main process:\n");
-
pthread_join(id1,&thread_result);
-
printf("Thread1 joined,it returned:%s",(char *)thread_result);
-
pthread_join(id2,&thread_result);
-
printf("Thread2 joined,it returned:%s",(char *)thread_result);
-
return 0;
-
}
-
/* 打印输出结果 */
-
/* This is the main process: */
-
/* This is the main process: */
-
/* This is the main process: */
-
/* This is a pthread 1: */
-
/* This is a pthread 1: */
-
/* This is a pthread 1: */
-
/* Thread1 joined,it returned:pthread 1 Thank you for the CPU time */
-
/* This is a pthread 2: */
-
/* This is a pthread 2: */
-
/* This is a pthread 2: */
- /* Thread2 joined,it returned:pthread 2 Thank you for the CPU time */
- #include <stdio.h>
-
#include <stdlib.h>
-
#include <pthread.h>
-
#include <unistd.h>
-
-
void *mythread2()
-
{
-
int i;
-
for(i=0;i<3;i++)
-
{
-
printf("This is a pthread 2:\n");
-
sleep(1);
-
}
-
pthread_exit("pthread 2 Thank you for the CPU time\n");
-
}
-
void *mythread1()
-
{
-
int i;
-
for(i=0;i<3;i++)
-
{
-
printf("This is a pthread 1:\n");
-
sleep(1);
-
}
-
pthread_exit("pthread 1 Thank you for the CPU time\n");
-
}
-
int main()
-
{
-
pthread_t id1,id2;
-
int i, ret;
-
void *thread_result;
-
ret = pthread_create(&id2,NULL,mythread2,NULL);
-
if(ret!=0)
-
{
-
printf("Create pthread 2 failed\n");
-
exit(0);
-
}
-
ret = pthread_create(&id1,NULL,mythread1,NULL);
-
if(ret!=0)
-
{
-
printf("Create pthread 1 failed");
-
exit(0);
-
}
-
for(i=0;i<3;i++)
-
{
-
printf("This is the main process:\n");
-
sleep(1);
-
}
-
pthread_join(id1,&thread_result);
-
printf("Thread1 joined,it returned:%s",(char *)thread_result);
-
pthread_join(id2,&thread_result);
-
printf("Thread2 joined,it returned:%s",(char *)thread_result);
-
return 0;
-
}
-
/* 打印输出结果 */
-
/* This is the main process: */
-
/* This is a pthread 1: */
-
/* This is a pthread 2: */
-
/* This is the main process: */
-
/* This is a pthread 1: */
-
/* This is a pthread 2: */
-
/* This is the main process: */
-
/* This is a pthread 1: */
-
/* This is a pthread 2: */
-
/* Thread1 joined,it returned:pthread 1 Thank you for the CPU time */
- /* Thread2 joined,it returned:pthread 2 Thank you for the CPU time */
- /*********
-
取消线程
-
**********/
-
-
#include <pthread.h>
-
#include <stdio.h>
-
#include <errno.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
-
#define handle_error_en(en, msg) \
-
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
-
-
static void *
-
thread_func(void *ignored_argument)
-
{
-
int s;
-
-
/* Disable cancellation for a while, so that we don't
-
immediately react to a cancellation request */
-
-
s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
-
if (s != 0)
-
handle_error_en(s, "pthread_setcancelstate");
-
-
printf("thread_func(): started; cancellation disabled\n");
-
sleep(5);
-
printf("thread_func(): about to enable cancellation\n");
-
-
s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
-
if (s != 0)
-
handle_error_en(s, "pthread_setcancelstate");
-
-
/* sleep() is a cancellation point */
-
-
sleep(1000); /* Should get canceled while we sleep */
-
-
/* Should never get here */
-
-
printf("thread_func(): not canceled!\n");
-
return NULL;
-
}
-
-
int
-
main(void)
-
{
-
pthread_t thr;
-
void *res;
-
int s;
-
-
/* Start a thread and then send it a cancellation request */
-
-
s = pthread_create(&thr, NULL, &thread_func, NULL);
-
if (s != 0)
-
handle_error_en(s, "pthread_create");
-
-
sleep(2); /* Give thread a chance to get started */
-
-
printf("main(): sending cancellation request\n");
-
s = pthread_cancel(thr);
-
if (s != 0)
-
handle_error_en(s, "pthread_cancel");
-
-
/* Join with thread to see what its exit status was */
-
-
s = pthread_join(thr, &res);
-
if (s != 0)
-
handle_error_en(s, "pthread_join");
-
- if (res == PTHREAD_CANCELED) //目标线程被取消,PTHREAD_CANCELED会被放置在*res中
-
printf("main(): thread was canceled\n");
-
else
-
printf("main(): thread wasn't canceled (shouldn't happen!)\n");
-
exit(EXIT_SUCCESS);
- }
- /*********
- 线程属性
- **********/
- #define _GNU_SOURCE /* 引入 pthread_getattr_np() 声明 */
-
#include <pthread.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <errno.h>
-
-
#define handle_error_en(en, msg) \
-
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
-
-
static void
-
display_pthread_attr(pthread_attr_t *attr, char *prefix)
-
{
-
int s, i;
-
size_t v;
-
void *stkaddr;
-
struct sched_param sp;
-
-
s = pthread_attr_getdetachstate(attr, &i);//获取线程属性对象的分离状态属性
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_getdetachstate");
-
printf("%sDetach state = %s\n", prefix,
-
(i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
-
(i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
-
"???");
-
-
s = pthread_attr_getscope(attr, &i);//获取线程属性对象里的竞争作用域属性
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_getscope");
-
printf("%sScope = %s\n", prefix,
-
(i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" :
-
(i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
-
"???");
-
-
s = pthread_attr_getinheritschedw(attr, &i);//获取 线程属性对象里的继承调度属性
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_getinheritsched");
-
printf("%sInherit scheduler = %s\n", prefix,
-
(i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" :
-
(i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
-
"???");
-
-
s = pthread_attr_getschedpolicy(attr, &i);//获取线程属性对象的调度策略属性
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_getschedpolicy");
-
printf("%sScheduling policy = %s\n", prefix,
-
(i == SCHED_OTHER) ? "SCHED_OTHER" :
-
(i == SCHED_FIFO) ? "SCHED_FIFO" :
-
(i == SCHED_RR) ? "SCHED_RR" :
-
"???");
-
-
s = pthread_attr_getschedparam(attr, &sp);//获取在线程属性对象里的调度参数属性
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_getschedparam");
-
printf("%sScheduling priority = %d\n", prefix, sp.sched_priority);
-
-
s = pthread_attr_getguardsize(attr, &v);//获取 线程属性对象里预留空间尺寸
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_getguardsize");
-
printf("%sGuard size = %d bytes\n", prefix, v);
-
-
s = pthread_attr_getstack(attr, &stkaddr, &v);//获取线程属性对象里的栈属性
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_getstack");
-
printf("%sStack address = %p\n", prefix, stkaddr);
-
printf("%sStack size = 0x%x bytes\n", prefix, v);
-
}
-
-
static void *
-
thread_start(void *arg)
-
{
-
int s;
-
pthread_attr_t gattr;
-
-
/* pthread_getattr_np() 一个非标准的 GNU 扩展,
-
它用于取得其第一个参数引用的线程的属性。*/
-
-
s = pthread_getattr_np(pthread_self(), &gattr);//pthread_self()获得calling线程的线程id
-
if (s != 0)
-
handle_error_en(s, "pthread_getattr_np");
-
-
printf("Thread attributes:\n");
-
display_pthread_attr(&gattr, "\t");
-
-
exit(EXIT_SUCCESS); /* 终止所有线程 */
-
}
-
-
int
-
main(int argc, char *argv[])
-
{
-
pthread_t thr;
-
pthread_attr_t attr;
-
pthread_attr_t *attrp; /* NULL or &attr */
-
int s;
-
-
attrp = NULL;
-
-
/* 如果命令行参数提供了,使用它来设置线尺寸属性,
-
以及一些其它属性,并设置 attrp 指向这个线程属性对象 */
-
-
printf("argc=%d\n",argc);
-
-
if (argc > 1) {
-
int stack_size;
-
void *sp;
-
-
attrp = &attr;
-
-
s = pthread_attr_init(&attr);
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_init");
-
-
s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_setdetachstate");
-
-
s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_setinheritsched");
-
-
stack_size = strtoul(argv[1], NULL, 0);//convert a string to an unsigned long integer
-
-
s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);//对齐内存分配
-
if (s != 0)
-
handle_error_en(s, "posix_memalign");
-
-
printf("posix_memalign() allocated at %p\n", sp);
-
-
s = pthread_attr_setstack(&attr, sp, stack_size);//设置线程属性对象里的栈属性(地址/尺寸)
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_setstack");
-
}
-
-
s = pthread_create(&thr, attrp, &thread_start, NULL);
-
if (s != 0)
-
handle_error_en(s, "pthread_create");
-
-
if (attrp != NULL) {
-
s = pthread_attr_destroy(attrp);
-
if (s != 0)
-
handle_error_en(s, "pthread_attr_destroy");
-
}
-
-
pause(); /* 当其它线程调用 exit() 时终止 */
-
return 0;
- }
//pthread_attr_init()的man手册实例
//$ ./a.out
或
//$ ./a.out 0x3000000
- /*********
-
线程互斥量
-
**********/
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <pthread.h>
-
-
int x;
-
pthread_mutex_t mutex; // 声明一个互斥量
-
-
void thread1(void)
-
{
-
while(x>0)
-
{
-
pthread_mutex_lock(&mutex); //上锁
-
printf("Thread 1 is running x=%d\n",x);
-
x--;
-
pthread_mutex_unlock(&mutex); //解锁
-
sleep(1);
-
}
-
pthread_exit(NULL);
-
}
-
-
void thread2(void)
-
{
-
while(x>0)
-
{
-
pthread_mutex_lock(&mutex); //上锁 //注释本行看区别
-
sleep(3);
-
printf("Thread 2 is running x=%d\n",x);
-
x--;
-
pthread_mutex_unlock(&mutex); //解锁
-
sleep(1);
-
}
-
pthread_exit(NULL);
-
}
-
-
int main()
-
{
-
pthread_t id1,id2;
-
int ret;
-
ret = pthread_mutex_init(&mutex,NULL); //初始化互斥量
-
if(ret!=0)
-
{
-
printf("pthread_mutex_init error\n");
-
exit(0);
-
}
-
x=10; //初始值
-
ret = pthread_create(&id1,NULL,(void *)thread1,NULL); //创建线程1
-
if(ret!=0)
-
{
-
printf("Create pthread 1 error\n");
-
exit(0);
-
}
-
ret = pthread_create(&id2,NULL,(void *)thread2,NULL); //创建线程2
-
if(ret!=0)
-
{
-
printf("Create pthread 2 error\n");
-
exit(0);
-
}
-
pthread_join(id1,NULL); //线程结束聚合线程
-
pthread_join(id2,NULL);
-
printf("Done\n");
-
return 0;
- }
- /*********
-
线程条件
-
**********/
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <pthread.h>
-
#include <unistd.h>
-
int x;
-
pthread_mutex_t mutex;
-
pthread_cond_t cond;
-
-
void producer(void)
-
{
-
while(1)
-
{
-
pthread_mutex_lock(&mutex);
-
int i;
-
for(i=0;i<3-x;i++)
-
{
-
x++;
-
printf("Producing:i=%d x=%d \n",i,x);
-
sleep(1);
-
}
-
if(x>=3)
-
{
-
pthread_cond_signal(&cond); /*条件改变,发送信号,通知consumer线程,激活等待该条件的线程*/
-
/*pthread_cond_broadcast()激活所有等待线程*/
-
printf("Producing complete %d\n",x);
-
}
-
pthread_mutex_unlock(&mutex);
-
printf("producer Unlock Mutex\n");
-
sleep(1);
-
}
-
pthread_exit(NULL);
-
}
-
-
void consumer(void)
-
{
-
while(1)
-
{
-
pthread_mutex_lock(&mutex);
-
while(x<3)
-
{
-
pthread_cond_wait(&cond,&mutex);//等待一个条件 //等待
-
printf("start consuming %d\n",x);
-
}
-
if(x>0)
-
{
-
x--;
-
printf(" consuming %d\n",x);
-
sleep(1);
-
}
-
pthread_mutex_unlock(&mutex);
-
printf("consumer Unlock Mutex\n");
-
}
-
pthread_exit(NULL);
-
}
-
-
int main()
-
{
-
pthread_t id1,id2;
-
int ret;
-
ret = pthread_mutex_init(&mutex,NULL); //初始化互斥量
-
if(ret!=0)
-
{
-
printf("pthread_mutex_init error\n");
-
exit(0);
-
}
-
ret=pthread_cond_init(&cond,NULL); //初始化条件变量
-
if(ret!=0)
-
{
-
printf("pthread_cond_init error\n");
-
exit(0);
-
}
-
printf("x=[%d]\n",x);
-
ret = pthread_create(&id1,NULL,(void *)producer,NULL); //创建线程1 发生器
-
if(ret!=0)
-
{
-
printf("Create pthread producer error\n");
-
exit(0);
-
}
-
ret = pthread_create(&id2,NULL,(void *)consumer,NULL); //创建线程2 消耗器
-
if(ret!=0)
-
{
-
printf("Create pthread consumer, error\n");
-
exit(0);
-
}
-
pthread_join(id1,NULL);
-
pthread_join(id2,NULL);
-
printf("Done\n");
-
return 0;
- }
- /*********
-
线程条件 信号量
-
**********/
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <pthread.h>
-
#include <semaphore.h>
-
-
sem_t sem;
-
char buffer[256];
-
-
void mythread(void)
-
{
-
sem_wait(&sem); //递减锁定一个信号量
-
while(strncmp("exit",buffer,4))
-
{
-
printf("You input %d characters\n",strlen(buffer)-1);
-
sem_wait(&sem);
-
}
-
pthread_exit(NULL);
-
}
-
int main()
-
{
-
pthread_t id;
-
int ret;
-
ret=sem_init(&sem,0,0); //初始化一个匿名信号量,进程内线程共享,
-
if(ret!=0)
-
{
-
printf("Semaphare initialization failed\n");
-
exit(1);
-
}
-
ret=pthread_create(&id,NULL,(void *)&mythread,NULL); //创建线程
-
if(ret!=0)
-
{
-
printf("pthread_create failed\n");
-
exit(1);
-
}
-
printf("Input some text,Enter'.'to finish\n");
-
while(strncmp("exit",buffer,4)!=0)
-
{
-
fgets(buffer,256,stdin); //FILE *stdin standard I/O streams
-
sem_post(&sem); //增加解锁一个信号量
-
}
-
pthread_join(id,NULL);
-
printf("Thtead joined,\n");
-
sem_destroy(&sem);
-
return 0;
- }