#include
#include
#include
/* 声明结构体 */
struct member
{
int num;
char *name;
};
/* 定义线程pthread */
static void * pthread(void *arg)
{
struct member *temp;
/* 线程pthread开始运行 */
printf("pthread start!\n");
/* 令主线程继续执行 */
sleep(2);
/* 打印传入参数 */
temp = (struct member *)arg;
printf("member->num:%d\n",temp->num);
printf("member->name:%s\n",temp->name);
return NULL;
}
/* main函数 */
int main(int agrc,char* argv[])
{
pthread_t tidp;
struct member *b;
/* 为结构体变量b赋值 */
b = (struct member *)malloc(sizeof(struct member));
b->num=1;
b->name="mlq";
/* 创建线程pthread */
if ((pthread_create(&tidp, NULL, pthread, (void*)b)) == -1)
{
printf("create error!\n");
return 1;
}
/* 令线程pthread先运行 */
sleep(1);
/* 线程pthread睡眠2s,此时main可以先执行 */
printf("mian continue!\n");
/* 等待线程pthread释放 */
if (pthread_join(tidp, NULL))
{
printf("thread is not exit...\n");
return -2;
}
return 0;
}
Linux线程可以取消其他线程,被取消的线程会被退出
线程本身可以设置不被其他线程取消
#include
#include
#include
pthread_t tid1, tid2, tid3;
void* th_reader1(void *p)
{
int i = 1;
int oldstate = -1;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
for(; i <= 5; i++)
{
printf("func[%s]: 第 %d 秒\n", __FUNCTION__, i);
sleep(1);
}
pthread_exit( (void *)0 );
}
int main()
{
void *ret1, *ret2, *ret3;
printf("start thread reader 1\n");
pthread_create(&tid2, NULL, th_reader1, NULL); //创建 读 线程1
sleep(2);
pthread_cancel(tid2); //发送取消信号
pthread_join(tid2, &ret2);
return 0;
}
运行结果
# ./a.out
start thread reader 1
func[th_reader1]: 第 1 秒
func[th_reader1]: 第 2 秒
func[th_reader1]: 第 3 秒
func[th_reader1]: 第 4 秒
func[th_reader1]: 第 5 秒
#include
#include
#include
pthread_t tid1, tid2, tid3;
void* th_reader1(void *p)
{
int i = 1;
int oldstate = -1;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
for(; i <= 5; i++)
{
printf("func[%s]: 第 %d 秒\n", __FUNCTION__, i);
sleep(1);
if(4 == i)
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
printf("thread cancel PTHREAD-CANCEL-ENABLE \n");
pthread_testcancel();
}
}
pthread_exit( (void *)0 );
}
int main()
{
void *ret1, *ret2, *ret3;
printf("start thread reader 1\n");
pthread_create(&tid2, NULL, th_reader1, NULL); //创建 读 线程1
sleep(2);
pthread_cancel(tid2);//发送取消信号
pthread_join(tid2, &ret2);
return 0;
}
运行结果:
运行结果:
# ./a.out
start thread reader 1
func[th_reader1]: 第 1 秒
func[th_reader1]: 第 2 秒
func[th_reader1]: 第 3 秒
func[th_reader1]: 第 4 秒
thread cancel PTHREAD-CANCEL-ENABLE
#include
#include
#include
void *func1(void * f1)
{
puts("我是第一个线程!我采用return的方式结束自己。");
return (void*)770880;
}
void *func2(void * f2)
{
puts("我是第二个线程!我采用pthread_exit的方式结束自己。");
pthread_exit((void*)1314);
}
void *func3(void * f3)
{
puts("我是第三个线程!我采用pthread_cancel的方式结束自己。");
while (1)//可能会发生线程执行完但是主线程还没有开始调用pthread_cancel函数。
pthread_testcancel();//主动设置取消点
return (void*)520;
}
int main(void)
{
pthread_t pth[3];
void *i;
pthread_create((pth + 0), NULL, func1, NULL);
pthread_join(pth[0], (void**)&i);
printf("线程一的退出状态:i = %d.\n", (int)i);
pthread_create((pth + 1), NULL, func2, NULL);
pthread_join(pth[1], (void**)&i);
printf("线程二的退出状态:i = %d.\n", (int)i);
pthread_create((pth + 2), NULL, func3, NULL);
pthread_cancel(pth[2]);//杀死
pthread_join(pth[2], (void**)&i);
printf("线程三的退出状态:i = %d.\n", (int)i);
return 0;
}
/*
*主线程创建子线程(当前子线程为stop停止状态),5秒后主线程唤醒子线程,
*10秒后主线程挂起子线程。15秒后主线程再次唤醒子线程,20秒后主线程执行完毕,
*等待子线程退出。
*/
#include
#include
#include
#define RUN 1
#define STOP 0
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int status = STOP;
void *thread_function(void)
{
static int i = 0;
while(1)
{
pthread_mutex_lock(&mut);
while(!status)
{
pthread_cond_wait(&cond, &mut);
}
pthread_mutex_unlock(&mut);
printf("child pthread %d\n", i++);
if (i == 20)
{
break;
}
sleep(1);
}
}
void thread_resume()
{
if (status == STOP)
{
pthread_mutex_lock(&mut);
status = RUN;
pthread_cond_signal(&cond);
printf("pthread run!\n");
pthread_mutex_unlock(&mut);
}
else
{
printf("pthread run already\n");
}
}
void thread_pause()
{
if (status == RUN)
{
pthread_mutex_lock(&mut);
status = STOP;
printf("thread stop!\n");
pthread_mutex_unlock(&mut);
}
else
{
printf("pthread pause already\n");
}
}
int main()
{
int err;
static int i = 0;
pthread_t child_thread;
if (pthread_mutex_init(&mut, NULL) != 0)
{
printf("mutex init error\n");
}
if (pthread_cond_init(&cond, NULL) != 0)
{
printf("cond init error\n");
}
err = pthread_create(&child_thread,NULL,(void *)thread_function,NULL);
if (err != 0)
{
printf("can't create thread:%s\n", strerror(err));
}
while(1)
{
printf("father pthread %d\n", i++);
sleep(1);
if (i == 5)
{
thread_resume();
}
if (i == 10)
{
thread_pause();
}
if (i == 15)
{
thread_resume();
}
if (i == 20)
{
break;
}
}
if (0 == pthread_join(child_thread, NULL))
{
printf("child thread is over\n");
}
return 0;
}
#include
#include
#include
#include
#include
#include
int main()
{
pid_t pid;
int status;
if(!(pid= fork())){
printf("Hi I am child process!\n");
sleep(10);
return 0;
}
else{
printf("send signal to child process (%d) \n",pid);
sleep(1);
kill(pid ,SIGABRT);
wait(&status);
if(WIFSIGNALED(status))
printf("chile process receive signal %d\n",WTERMSIG(status));
}
return 0;
}
运行结果:
send signal to child process(3170)
Hi I am child process!
child process receive signal 6