ps - Lf? pid
pid : 进程号
简述
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int main()
{
pthread_t pthid = pthread_self();
printf("pthid=%ld\n",pthid);
return 0;
}
函数
#include <stdio.h>
#include <pthread.h>
// 注意线程调用的函数返回值为任意指针类型
void *myfunc01()
{
printf("线程%ld正在执行\n", pthread_self());
return NULL;
}
void *myfunc02(void *arg)
{
printf("线程%ld正在执行,参数为:%s\n", pthread_self(), (char *)arg);
return NULL;
}
int main(int argc, char const *argv[])
{
pthread_t p1, p2, p3;
pthread_create(&p1, NULL, myfunc01, NULL);
pthread_create(&p2, NULL, myfunc02, "Thread2");
pthread_create(&p3, NULL, myfunc02, "Thread3");
getchar();
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *myfunc01(void *argv)
{
int *time = (int *)argv;
printf("线程A%ld将于%d秒后销毁\n", pthread_self(), *time);
sleep(*time);
return "线程A";
}
void *myfunc02(void *argv)
{
int *time = (int *)argv;
printf("线程B%ld将于%d秒后销毁\n", pthread_self(), *time);
sleep(*time);
return "线程B";
}
int main(int argc, char const *argv[])
{
int n01 = 3, n02 = 5;
pthread_t p1, p2;
pthread_create(&p1, NULL, myfunc01, &n01);
pthread_create(&p2, NULL, myfunc02, &n02);
void *argv01, *argv02;
pthread_join(p1, &argv01);
pthread_join(p2, &argv02);
printf("%s被回收\n", (char *)argv01);
printf("%s被回收\n", (char *)argv02);
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *fun1(void *arg)
{
printf("线程1开始执行\n");
sleep(3);
return NULL;
}
void *fun2(void *arg)
{
printf("线程2开始执行\n");
sleep(1);
return NULL;
}
int main(int argc, char const *argv[])
{
pthread_t tid1;
pthread_t tid2;
pthread_create(&tid1,NULL,fun1,NULL);
pthread_create(&tid2,NULL,fun2,NULL);
pthread_detach(tid1);
pthread_detach(tid2);
getchar();
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
void *fun(void *arg)
{
printf("len = %ld\n",strlen(arg));
char *txt = (char *)arg;
char buf[32] = {0};
strcpy(buf,txt);
printf("线程%ld开始执行\n",pthread_self);
for(int i = 0;i < strlen(buf);i++)
{
printf("%c\n",buf[i]);
sleep(1);
}
printf("线程%ld执行结束\n");
}
int main(int argc, char const *argv[])
{
char buf[32] = {0};
while(1)
{
// memset(buf,0,32);
printf("请输入要打印的字符\n");
fgets(buf,32,stdin);
buf[strlen(buf)-1] = 0;
if(strcmp(buf,"886")==0)
{
break;
}
pthread_t t;
pthread_create(&t,NULL,fun,buf);
}
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *fun(void *arg)
{
for(int i = 0;i < 10;i++)
{
printf("%d\n",i);
sleep(1);
if(i == 3)
{
pthread_exit(NULL);
}
}
return NULL;
}
int main(int argc, char const *argv[])
{
pthread_t tid;
pthread_create(&tid,NULL,fun,NULL);
pthread_detach(tid);
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *fun(void *arg)
{
for(int i = 0;i < 10;i++)
{
printf("%d\n",i);
sleep(1);
}
return NULL;
}
int main(int argc, char const *argv[])
{
pthread_t tid;
pthread_create(&tid,NULL,fun,NULL);
sleep(3);
pthread_detach(tid);
pthread_join(tid,NULL);
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *fun(void *arg)
{
int i = 0;
while(1)
{
printf("%lu正在运行i=%d\n",pthread_self(),i++);
sleep(1);
}
}
int main(int argc, char const *argv[])
{
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);//设置分离属性
pthread_t tid;
pthread_create(&tid,NULL,fun,NULL);
printf("5秒后将结束线程\n");
sleep(5);
pthread_cancel(tid);
sleep(1);
printf("销毁线程\n");
pthread_attr_destroy(&attr);
return 0;
}
概述:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <
void *fun(void *arg)
{
int i = 0;
while(1)
{
printf("%lu线程正在执行i=%d\n",pthread_self(),i++);
sleep(1);
}
return NULL;
}
int main(int argc, char const *argv[])
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
void *stackaddr = calloc(1,128);
if(stackaddr == NULL)
{
perror("null");
return 0;
}
pthread_attr_setstack(&attr,&stackaddr,128);
pthread_t tid;
printf("线程在5秒后结束\n");
pthread_create(&tid,&attr,fun,NULL);
sleep(5);
pthread_cancel(tid);
sleep(1);
pthread_attr_destroy(&attr);
free(stackaddr);
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
int ticket = 100;
void *sale(void *name)
{
char buf[32] = {0};
strcpy(buf,(char*) name);
while(ticket > 0)
{
ticket--;
sleep(0.5);
printf("%s售卖了一张船票,还剩%d张船票\n",buf,ticket);
}
}
int main(int argc, char const *argv[])
{
pthread_t tid1,tid2,tid3,tid4;
pthread_create(&tid1,NULL,sale,"一号窗口");
pthread_create(&tid2,NULL,sale,"二号窗口");
pthread_create(&tid3,NULL,sale,"三号窗口");
pthread_create(&tid3,NULL,sale,"四号窗口");
pthread_detach(tid1);
pthread_detach(tid2);
pthread_detach(tid3);
pthread_detach(tid4);
getchar();
return 0;
}