c语言线程终止练习示例
代码如下:
#include
#include
#include
void *t1(void *args)
{
return (void *) 0;
}
void *t2(void *args)
{
printf("thread 2 param[args] = %dn", args);
pthread_exit((void *) 3);
}
void *t3(void *args) {
while(1)
{
printf("thread 3 is workingn");
sleep(1);
}
}
int main(int argc, char *argv[])
{
pthread_t thread;
int err;
void *status;
printf("creating thread 1n");
err = pthread_create(&thread, NULL, t1, NULL);
if(err)
{
printf("Can not created thread 1n");
exit(-1);
}
pthread_join(thread, &status);
printf("thread 1 exit return code %dnn", status);
printf("creating thread 2n");
err = pthread_create(&thread, NULL, t2, (void *) 9);
if(err)
{
printf("Can not created thread 2n");
exit(-2);
}
pthread_join(thread, &status);
printf("thread 2 exit return code %dnn", status);
printf("creating thread 3n");
err = pthread_create(&thread, NULL, t3, NULL);
if(err)
{
printf("Can not created thread 3n");
exit(-3);
}
sleep(10);
pthread_cancel(thread);
pthread_join(thread, &status);
printf("thread 3 exit return code %dn", status);
return 1;
}
-
直接插入排序(C语言实现)
直接插入排序方法:仅有一个记录的表总是有序的,因此,对于有n个记录的表,可以从第二个记录开始直到第n个记录逐个有序的向有序表中进行插入操作,从而得到n个记录按关键字有序的表。下面一起跟着小编学习一下吧!实现一个直接插入排序的`C语言函数,要求对要排序的元素按...
-
C语言入门知识:常量
导语:在程序运行中,其值不能被改变的量称为常量。下面是常量相关知识,欢迎阅读:常量有5种类型:整型常量、实型常量、字符常量、字符串常量和符号常量。(一)数值转换数字的四种表现形式:①:二进制:所有数字由0,1构成,逢二进一,二进制数中不会出现2.。例:110101②:八进制:以数...
-
C语言中文件操作详解及实例代码
在文件操作中除了打开操作以及读写操作,还有几种比较常见的操作。以下是本站小编搜索整理的关于这些操作中涉及到的函数,,需要的朋友可以参考本文!想了解更多相关信息请持续关注我们应届毕业生考试网!一.移动位置指针的函数rewind函数和fseek函数,这两个函数的原...
-
C语言自增(++)和自减(--)
C语言源自KenThompson发明的B语言,那么大家知道C语言自增(++)和自减(--)是什么呢?下面一起来看看!一个整数自身加一可以这样写:a+=1;它等价于a=a+1;。但是在C语言中还有一种更简单的写法,就是a++;或者++a;。这种写法叫做自加或自增;意思很明确,就是自身加一。相应...