Linux-线程

pthread

    pthread_create创建一个线程并马上开始执行线程函数;pthread_cancel取消线程,可在线程函数中设置“可取消性”的状态和类型;pthread_join以阻塞方式等待指定线程结束,线程可用pthread_cancel取消,也可在线程函数中用pthread_exit((void *)val)和return (void *)val结束线程,线程正常结束时pthread_join(tid, retval)函数retval能收到线程结束返回值val,用pthread_cancel取消的返回值是PTHREAD_CANCELED=-1。

#include <stdio.h>
#include <pthread.h>

void *rountine(void *arg)
{
	char *str = (char *)arg;

	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

	int i;
	for(i = 0; i < 5; i++)
	{
		printf("NO.%d: %s \n", i, str);
		sleep(1);
	}

	//pthread_exit((void *)123);
	return (void *)123;
}

int main()
{
	pthread_t tid;
	int ret = pthread_create(&tid, NULL, rountine, "hello world");
	if(ret < 0)
	{
		printf("pthread_create: %d\n", strerror(ret));
		return -1;
	} 

	sleep(3);
	pthread_cancel(tid);

	int i;
	for(i = 0 ; i < 3; i++)
	{
		printf("Main: NO.%d----\n", i);
		sleep(1);
	}

	void *val;
	void **retval=&val;
	ret = pthread_join(tid, retval);
	if(ret < 0)
	{
		printf("pthread_join: %d\n", strerror(ret));
		return -1;
	} 

	printf("val=%d\n", (int)val);
	return 0;
}

    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);用来设置当前线程的“可取消性”状态,并且将先前的状态返回到oldstate引用中;“可取消性”状态的合法值分别是:PTHREAD_CANCEL_ENABLE 和 PTHREAD_CANCEL_DISABLE。
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);用来设置当前线程的“可取消类型”,并且将先前的类型返回到oldtype引用中;“可取消类型”的合法值分别是:PTHREAD_CANCEL_DEFERRED :线程接收到取消操作后,直到运行到“可取消点”后取消;PTHREAD_CANCEL_ASYNCHRONOUS :线程接收到取消操作后,立即取消;pthread_testcancel()函数用来在当前线程创建一个“可取消点”,如果当前线程是不能取消的,则这个函数无效。

sem

    sem信号量同步

    Compile and link with -std=gnu99 -pthread

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>

#define MAX 9
char array[MAX];
sem_t sem;

void fun1(int val)
{
	sem_wait(&sem);	//P
	for(int i = 0; i < MAX; i++)
	{
		array[i] = val+i;
		sleep(1);
		printf("fun1: NO.%d: %d \n", i, array[i]);
	}
	sem_post(&sem); //v
}

void fun2(int val)
{
	sem_wait(&sem);	//P
	for(int i = 0; i < MAX; i++)
	{
		array[i] = val+i;
		sleep(1);
		printf("fun2: NO.%d: %d \n", i, array[i]);
	}
	sem_post(&sem); //v
}

void *roun1(void *arg)
{
	fun1(1);
	return (void *)1;
}

void *roun2(void *arg)
{
	fun2(10);
	return (void *)2;
}

int main()
{
	int ret = sem_init(&sem, 0, 1);
	if(0 > ret)
	{
		perror("sem_init");
		return -1;
	}

	pthread_t tid[2];
	ret = pthread_create(&tid[0], NULL, roun1, NULL);
	if(ret < 0)
	{
		printf("pthread_create: %s\n", strerror(ret));
		return -1;
	} 

	sleep(3);
	
	ret = pthread_create(&tid[1], NULL, roun2, NULL);
	if(ret < 0)
	{
		printf("pthread_create: %s\n", strerror(ret));
		return -1;
	} 

	ret = pthread_join(tid[0], NULL);
	if(ret < 0)
	{
		printf("pthread_join: %s\n", strerror(ret));
		return -1;
	}
	ret = pthread_join(tid[1], NULL);
	if(ret < 0)
	{
		printf("pthread_join: %s\n", strerror(ret));
		return -1;
	} 
}

mutex

    mutex互斥锁互斥

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#define MAX 9
char array[MAX];
pthread_mutex_t mutex;

void fun(int val)
{
	pthread_mutex_lock(&mutex);
	for(int i = 0; i < MAX; i++)
	{
		array[i] = val+i;
		sleep(1);
		printf("1: NO.%d: %d \n", i, array[i]);
	}
	pthread_mutex_unlock(&mutex);
}

void *roun1(void *arg)
{
	fun(1);
	return (void *)1;
}

void *roun2(void *arg)
{
	fun(10);
	return (void *)2;
}

int main()
{
	int ret = pthread_mutex_init(&mutex, NULL);
	if(0 > ret)
	{
		perror("pthread_mutex_init");
		return -1;
	}

	pthread_t tid[2];
	ret = pthread_create(&tid[0], NULL, roun1, NULL);
	if(ret < 0)
	{
		printf("pthread_create: %s\n", strerror(ret));
		return -1;
	} 

	sleep(3);
	
	ret = pthread_create(&tid[1], NULL, roun2, NULL);
	if(ret < 0)
	{
		printf("pthread_create: %s\n", strerror(ret));
		return -1;
	} 

	ret = pthread_join(tid[0], NULL);
	if(ret < 0)
	{
		printf("pthread_join: %s\n", strerror(ret));
		return -1;
	}
	ret = pthread_join(tid[1], NULL);
	if(ret < 0)
	{
		printf("pthread_join: %s\n", strerror(ret));
		return -1;
	} 
}

条件变量

    与互斥锁不同,条件变量是用来等待而不是用来上锁的;条件变量用来自动阻塞一个线程,直到某特殊情况发生为止;条件变量使我们可以睡眠等待某种条件出现;条件变量是利用线程间共享的全局变量进行同步的一种机制,
    主要包括两个动作:
一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号);条件的检测是在互斥锁的保护下进行的;如果条件为假,一个线程自动阻塞,并释放等待状态改变的互斥锁。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

//条件变量
pthread_cond_t cont = PTHREAD_COND_INITIALIZER; 
//同步锁
pthread_mutex_t mutex;
//运行标志
int bIsrun = 1;

//工作线程
void *work(void *arg)
{
	while(1)
	{
		if(0 > pthread_mutex_lock(&mutex))
		{
			exit(-1);
		} 

		if(bIsrun)
		{
			if(0 != pthread_cond_wait (&cont, &mutex))
			{
				exit(-1);
			} 
		}

		if(0 > pthread_mutex_unlock(&mutex))
		{
			perror("work:pthread_mutex_unlock");
			exit(-1);
		}

		static unsigned long counter=0;

		printf("Running - %lu ... \r\n", counter++);
		sleep(1);
	}

	return (void *)0; 
} 

int main(int argv, char **argc)
{  
	// 初始化
	pthread_t thr; 
	pthread_attr_t threadAttr; 
	pthread_attr_init(&threadAttr); 

	pthread_mutex_init(&mutex, NULL);

	pthread_create(&thr, &threadAttr, work, (void *)0);

	//调度线程
	while(1)
	{
		char ch = getchar();

		switch(ch)
		{
			/*suspend*/
			case 's':
			case 'S':
				pthread_mutex_lock(&mutex); 
				bIsrun = 1;
				printf("bIsrun = 1\r\n");
				pthread_mutex_unlock (&mutex);
				break;

			/*resume*/
			case 'r':
			case 'R':
				pthread_mutex_lock(&mutex); 
				if(bIsrun)
				{
					bIsrun = 0;
					pthread_cond_signal(&cont);
				} 
				pthread_mutex_unlock (&mutex);
				break;

			/*exit*/
			case 'q':
			case 'Q':
				exit(1);

			default:
				printf("Input something...\r\n");
				break;
		};
 	}

	return 0; 
}

猜你喜欢

转载自blog.csdn.net/TSZ0000/article/details/83106859