body
{
font-family: 微软雅黑,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif;
font-size: 10.5pt;
line-height: 1.5;
}
html, body
{
}
h1 {
font-size:1.5em;
font-weight:bold;
}
h2 {
font-size:1.4em;
font-weight:bold;
}
h3 {
font-size:1.3em;
font-weight:bold;
}
h4 {
font-size:1.2em;
font-weight:bold;
}
h5 {
font-size:1.1em;
font-weight:bold;
}
h6 {
font-size:1.0em;
font-weight:bold;
}
img {
border:0;
max-width: 100%;
height: auto !important;
}
blockquote {
margin-top:0px;
margin-bottom:0px;
}
table {
border-collapse:collapse;
border:1px solid #bbbbbb;
}
td {
border-collapse:collapse;
border:1px solid #bbbbbb;
}
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>
#include<string.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//初始化了一个MUTEX锁
void *func1(void *arg)
{
pthread_mutex_lock(&mutex);//给mutex加锁,这是一条原子操作,不可能出现两个线程同时执行这个代码
int *a = (int *) arg;
printf("thread%d start\n", *a);
int i;
for (i = 0; i < 10; i++)
{
printf("thread%d is running\n", *a);
sleep(1);
}
printf("thread%d end\n", *a);
pthread_mutex_unlock(&mutex);//给mutex解锁
pthread_exit(NULL);
}
void *func2(void* arg)
{
pthread_t th = *(pthread_t*)arg;
sleep(2);
pthread_cancel(th);
}
int main(int arg, char * args[])
{
printf("process start\n");
pthread_t thr_d1, thr_d2;
int i[2];
i[0] = 1;
i[1] = 2;
pthread_create(&thr_d1, NULL, func1, &i[0]);
sleep(1);
pthread_create(&thr_d2, NULL, func1, &i[1]);
//设置死锁产生
pthread_t thr_d3;
pthread_create(&thr_d3, NULL, func2, &thr_d1);
pthread_join(thr_d1, NULL);
pthread_join(thr_d2, NULL);
printf("process end\n");
return 0;
}