线程、创建线程、终止线程、线程同步
2014-11-21 10:24
369 查看
一、线程:1.线程是比进程粒度更细的运行单位,线程可以再次创建线程,所有的线程都在同一个地址空间运行,多个线程共享一个内存的空间、同样的文件描述符及其它资源。
2.编译一个与线程有关的文件时,使用GCC 编译器时,必须使用-lpthread选项,例如:gcc foot.c -o foot-lpthread -std-gnu99
二、线程标识:1.每个进程都有唯一的标识,每个线程也有唯一的标识,进程在整个系统中是唯一的,而线程仅在属于进程的上下文中才具有唯一性。
2.线程的类型为:pthread_t
3.比较两个线程是否相等:#include <pthread.h>
int pthread_equal(pthread_t t1,thread_t t2);
返回值:非0标示t1和t2相等,0表示不等。
4.获得自身的标示符:#include<pthread.h> pthread_t pthread_self(void);
三、创建线程:1.创建线程函数:
#include<pthread.h>
int pthread_create()pthread_t *restrict thread,const pthread_attr_t *restrict attr, void *(start_routine)(void),void *restrcit arg);
返回值:0表水程功,错误号表示失败!
参数解释:
1.指向pthread_t类型的变量thread,代表新创建的线程标识。
2.指向pthread_attr_t线程属性类型的变量attr,该变量控制线程与程序其它部分进行交互的方式,该参数为NULL代表使用默认的属性。
3.指向线程函数的指针start_routine,该变量为函数指针,代表要执行的代码。
4.指向线程参数的指针arg,通过此参数可以向线程函数start_routine提供所需要的运行参数。如果需要传递多个参数,则需要传递多个参数给线程,则需要将 多 个参数组织委一个结构体,并将结构体指针传递到线程。
2.code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>
pthread_t ntid;
typedef unsigned long u32;//重新定义nusigned long 32,32bit是内核调用的类型。有什么作用?
void printids(const char *);
void *run(void *);
int main()
{
int err=pthread_create(&ntid,NULL,run,NULL);
if(err!=0)
{
printf("can't create thread:%s!\n",strerror(err));//strerror(err))返回错误原因的描述字符符
exit(1);
}
printids("main thread:");
sleep(1);
return 0;
}
void printids(const char *s)
{pid_t pid;
pthread_t tid;
pid=getpid();
tid=pthread_self();
printf("%s pid %u tid %u (0x%x)\n",s,(u32)pid,(u32)tid,(u32)tid);
}
void *run(void *arg)
{
printids("new threads:");
return((void *)0);//把0强制转换为void类型指针。
}
3.运行结果:
结果显示错误:这应该跟数据类型有关,如何解决呢?
四、线程同步: 1.线程的互斥量的数据类型为pthread_mutex_t,使用之前必须初始化,静态初始化为:
prhread_mutex_t mutex =PTHREAD_MUTEX_INITIALIZER:
2.通过调用pthread_mutex_lock函数对互斥量进行加锁,若该互斥量被加锁,则调用该互斥量的函数则被阻塞。.通过调用pthread_mutex_unlock函数进 行解锁,[b]通过调用pthread_mutex_trylock函数尝试进行解锁。这三个函数若成功则锁住互斥量,否则返回错误EBUSY,而不会阻塞调用线程。[/b]
#include<pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_tryloc(pthread_mutex_t *mutex);
返回值:若成功则返回0,否则返回失败代码。
3.用pthread_cond_t数据类型的条件变量可以通过静态赋值:
pthread_cond_t cond=PTHREAD_COND_INITIALIZER:
4.已下函数可以通知线程已经条件发生变化:
#include<pthread.h>
int pthread_cond_signal(pthread_cond_t *cond);
返回值:若成功则返回0,否则返回失败代码。
5.已下函数来等待条件为真后继续执行。
#include<pthread.h>
nt pthread_cond_wait (pthread_cond_t *restrict cond,pthread_cond_t *restrict mutex);
返回值:若成功则返回0,否则返回失败代码。
6.code:基于分组的存取代码片段!
/*定义临界资源互斥量/
prhread_mutex_t mqlock =PTHREAD_MUTEX_INITIALIZER:
/*定义条件变量*/
pthread_cond_t mqlock_ready=[b]PTHREAD_COND_INITIALIZER:[/b]
[b] ...[/b]
/*线程2/
void *thread2_run(void *arg)
{
...
for(;;)
{
[b] ....[/b]
[b] pthread_mutex_loc(&mqlock);/*加锁互斥量*/[/b]
[b] while(list_empty(&mqlock));[/b]
[b] pthread_cond_wait (&mqlock_readym,&mqlock);/若分组队列为空,则休眠等待条件的改变*/[/b]
[b] pkt=getnextpkt(&pkt_queue);/*若分组队列不为空,则取下一个分组*/[/b]
[b] int pthread_mutex_unlock(&mqlock);/*解锁互斥量*/[/b]
[b] handle_packet(pkt);/*处理分组*/[/b]
}
}
/*线程1*/
void *thread1_run(void *arg)
{
...
for(;;)
{
.......
pthread_mutex_loc(&mqlock);/*加锁互斥量*/
list_add_tail(pkt,&pkt_queue);/*分组进入队列尾*/
int pthread_mutex_unlock(&mqlock);/*解锁互斥量*/
int pthread_cond_signal(&mqlock_ready);/*通知条件发生变化*/
......
}
}
2.编译一个与线程有关的文件时,使用GCC 编译器时,必须使用-lpthread选项,例如:gcc foot.c -o foot-lpthread -std-gnu99
二、线程标识:1.每个进程都有唯一的标识,每个线程也有唯一的标识,进程在整个系统中是唯一的,而线程仅在属于进程的上下文中才具有唯一性。
2.线程的类型为:pthread_t
3.比较两个线程是否相等:#include <pthread.h>
int pthread_equal(pthread_t t1,thread_t t2);
返回值:非0标示t1和t2相等,0表示不等。
4.获得自身的标示符:#include<pthread.h> pthread_t pthread_self(void);
三、创建线程:1.创建线程函数:
#include<pthread.h>
int pthread_create()pthread_t *restrict thread,const pthread_attr_t *restrict attr, void *(start_routine)(void),void *restrcit arg);
返回值:0表水程功,错误号表示失败!
参数解释:
1.指向pthread_t类型的变量thread,代表新创建的线程标识。
2.指向pthread_attr_t线程属性类型的变量attr,该变量控制线程与程序其它部分进行交互的方式,该参数为NULL代表使用默认的属性。
3.指向线程函数的指针start_routine,该变量为函数指针,代表要执行的代码。
4.指向线程参数的指针arg,通过此参数可以向线程函数start_routine提供所需要的运行参数。如果需要传递多个参数,则需要传递多个参数给线程,则需要将 多 个参数组织委一个结构体,并将结构体指针传递到线程。
2.code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>
pthread_t ntid;
typedef unsigned long u32;//重新定义nusigned long 32,32bit是内核调用的类型。有什么作用?
void printids(const char *);
void *run(void *);
int main()
{
int err=pthread_create(&ntid,NULL,run,NULL);
if(err!=0)
{
printf("can't create thread:%s!\n",strerror(err));//strerror(err))返回错误原因的描述字符符
exit(1);
}
printids("main thread:");
sleep(1);
return 0;
}
void printids(const char *s)
{pid_t pid;
pthread_t tid;
pid=getpid();
tid=pthread_self();
printf("%s pid %u tid %u (0x%x)\n",s,(u32)pid,(u32)tid,(u32)tid);
}
void *run(void *arg)
{
printids("new threads:");
return((void *)0);//把0强制转换为void类型指针。
}
3.运行结果:
结果显示错误:这应该跟数据类型有关,如何解决呢?
四、线程同步: 1.线程的互斥量的数据类型为pthread_mutex_t,使用之前必须初始化,静态初始化为:
prhread_mutex_t mutex =PTHREAD_MUTEX_INITIALIZER:
2.通过调用pthread_mutex_lock函数对互斥量进行加锁,若该互斥量被加锁,则调用该互斥量的函数则被阻塞。.通过调用pthread_mutex_unlock函数进 行解锁,[b]通过调用pthread_mutex_trylock函数尝试进行解锁。这三个函数若成功则锁住互斥量,否则返回错误EBUSY,而不会阻塞调用线程。[/b]
#include<pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_tryloc(pthread_mutex_t *mutex);
返回值:若成功则返回0,否则返回失败代码。
3.用pthread_cond_t数据类型的条件变量可以通过静态赋值:
pthread_cond_t cond=PTHREAD_COND_INITIALIZER:
4.已下函数可以通知线程已经条件发生变化:
#include<pthread.h>
int pthread_cond_signal(pthread_cond_t *cond);
返回值:若成功则返回0,否则返回失败代码。
5.已下函数来等待条件为真后继续执行。
#include<pthread.h>
nt pthread_cond_wait (pthread_cond_t *restrict cond,pthread_cond_t *restrict mutex);
返回值:若成功则返回0,否则返回失败代码。
6.code:基于分组的存取代码片段!
/*定义临界资源互斥量/
prhread_mutex_t mqlock =PTHREAD_MUTEX_INITIALIZER:
/*定义条件变量*/
pthread_cond_t mqlock_ready=[b]PTHREAD_COND_INITIALIZER:[/b]
[b] ...[/b]
/*线程2/
void *thread2_run(void *arg)
{
...
for(;;)
{
[b] ....[/b]
[b] pthread_mutex_loc(&mqlock);/*加锁互斥量*/[/b]
[b] while(list_empty(&mqlock));[/b]
[b] pthread_cond_wait (&mqlock_readym,&mqlock);/若分组队列为空,则休眠等待条件的改变*/[/b]
[b] pkt=getnextpkt(&pkt_queue);/*若分组队列不为空,则取下一个分组*/[/b]
[b] int pthread_mutex_unlock(&mqlock);/*解锁互斥量*/[/b]
[b] handle_packet(pkt);/*处理分组*/[/b]
}
}
/*线程1*/
void *thread1_run(void *arg)
{
...
for(;;)
{
.......
pthread_mutex_loc(&mqlock);/*加锁互斥量*/
list_add_tail(pkt,&pkt_queue);/*分组进入队列尾*/
int pthread_mutex_unlock(&mqlock);/*解锁互斥量*/
int pthread_cond_signal(&mqlock_ready);/*通知条件发生变化*/
......
}
}
相关文章推荐
- 【转帖】在内核模式下创建和终止线程
- 如何:创建和终止线程(C# 编程指南--MSDN)
- 如何:创建和终止线程(C# 编程指南)
- 在VC++6.0下如何用事件与线程通讯使线程同步和终止
- 线程控制——创建、启动及终止
- 【转】C# 创建 终止线程
- 线程概述,优先级,睡眠,创建及终止(VC_Win32)
- 多线程编程-线程的创建和终止
- 多线程编程-线程的创建和终止
- Windows线程的创建和终止
- 创建和终止线程的好方法
- asp.net 创建和终止线程(多线程)
- 如何创建和终止线程(转载自MSDN)
- 线程的创建、管理 与 使用信号灯、互斥量、临界区、事件进行线程同步或互斥
- 转帖】在内核模式下创建和终止线程 收藏
- .NET多线程技术详解(2) 创建、终止线程
- 在内核模式下创建和终止线程
- 多线程编程-线程的创建和终止
- 多线程编程-线程的创建和终止
- Linux 线程(创建/等待/终止)