Linux系统编程——线程
Linux
C语言
函数,指针,结构体
个人日常学习代码存档
#include
#include
#include
#include
int g_data = 0;
pthread_mutex_t mutex;
pthread_cond_t cond;
void *func1(void *arg)
{
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
static int cnt = 0;
while(1)
{
pthread_cond_wait(&cond,&mutex);
printf("t1 run\n");
printf("t1:%d\n",g_data);
g_data = 0;
sleep(1);
if(cnt++ == 10)
exit(1);
}
}
void *func2(void *arg)
{
printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
while(1)
{
printf("t2: %d\n",g_data);
pthread_mutex_lock(&mutex);
g_data++;
if(g_data == 3)
{
pthread_cond_signal(&cond);
}
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond,NULL);
ret = pthread_create(&t1,NULL,func1,(void *)¶m);
if(ret == 0)
{
// printf("main:creat t1 success\n");
}
ret = pthread_create(&t2,NULL,func2,(void *)¶m);
if(ret == 0)
{
// printf("main:creat t2 success\n");
}
// printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
return 0;
}
#include
#include
void *func1(void *arg)
{
static int ret = 10;
static char *p = "t1 is run out";
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
pthread_exit((void *)p);
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
char *pret = NULL;
ret = pthread_create(&t1,NULL,func1,(void *)¶m);
if(ret == 0)
{
printf("main:creat t1 success\n");
}
printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,(void **)&pret);
printf("main: t1 quit:%s\n",pret);
return 0;
}
#include
#include
#include
int g_data = 0;
void *func1(void *arg)
{
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
while(1){
printf("t1:%d\n",g_data++);
sleep(1);
if(g_data == 3){
pthread_exit(NULL);
}
}
}
void *func2(void *arg)
{
printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
while(1){
printf("t2:%d\n",g_data++);
sleep(1);
}
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
ret = pthread_create(&t1,NULL,func1,(void *)¶m);
if(ret == 0)
{
printf("main:creat t1 success\n");
}
ret = pthread_create(&t2,NULL,func2,(void *)¶m);
if(ret == 0)
{
printf("main:creat t2 success\n");
}
printf("main:%ld\n",(unsigned long)pthread_self());
while(1){
printf("main:%d\n",g_data++);
sleep(1);
}
pthread_join(t1,NULL);
pthread_join(t2,NULL);
return 0;
}
#include
#include
#include
int g_data = 0;
pthread_mutex_t mutex;
void *func1(void *arg)
{
pthread_mutex_lock(&mutex);
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
pthread_mutex_unlock(&mutex);
}
void *func2(void *arg)
{
pthread_mutex_lock(&mutex);
printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
pthread_mutex_unlock(&mutex);
}
void *func3(void *arg)
{
pthread_mutex_lock(&mutex);
printf("t3:%ld thread is create\n",(unsigned long)pthread_self());
printf("t3:param is %d\n",*((int *)arg));
pthread_mutex_unlock(&mutex);
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
pthread_t t3;
pthread_mutex_init(&mutex,NULL);
ret = pthread_create(&t1,NULL,func1,(void *)¶m);
if(ret == 0)
{
printf("main:creat t1 success\n");
}
ret = pthread_create(&t2,NULL,func2,(void *)¶m);
if(ret == 0)
{
printf("main:creat t2 success\n");
}
ret = pthread_create(&t3, NULL, func3, (void *)¶m);
if(ret == 0)
{
printf("main:creat t3 success\n");
}
printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_join(t3,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
#include
#include
#include
#include
int g_data = 0;
pthread_mutex_t mutex;
void *func1(void *arg)
{
// pthread_mutex_lock(&mutex);
printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
pthread_mutex_lock(&mutex);
while(1)
{
printf("t1: %d\n",g_data++);
sleep(1);
if(g_data == 3)
{
pthread_mutex_unlock(&mutex);
// pthread_exit(NULL);
exit(0);
}
}
}
void *func2(void *arg)
{
// pthread_mutex_lock(&mutex);
printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
while(1)
{
printf("t2: %d\n",g_data);
pthread_mutex_lock(&mutex);
g_data++;
pthread_mutex_unlock(&mutex);
sleep(1);
}
// pthread_mutex_unlock(&mutex);
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
pthread_mutex_init(&mutex,NULL);
ret = pthread_create(&t1,NULL,func1,(void *)¶m);
if(ret == 0)
{
printf("main:creat t1 success\n");
}
ret = pthread_create(&t2,NULL,func2,(void *)¶m);
if(ret == 0)
{
printf("main:creat t2 success\n");
}
printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
#include
int main(int argc,char **argv)
{
int time = atoi(argv[1]);
int i = 0;
for(i=0;i<time;i++)
{
system("./a.out");
}
}
#include
int main()
{
int i = 0;
for(i=0;i<10;i++)
{
system("./pthread");
}
}
如果文章有帮助就点个赞哦,有问题可留言。