❶ 关于linux下多线程编程
pthread_join 线程停止等待函数没有调用
pthread_create 线程生成后,没有等子线程停止,主线程就先停止了。
主线程停止后,整个程序停止,子线程在没有printf的时候就被结束了。
结论:不是你没有看到结果,而是在子线程printf("..................\n");之前整个程序就已经停止了。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define FALSE -1
#define TRUE 0
void *shuchu( void *my )
{
int j;
printf("..................\n");
}
int main()
{
int i = 0;
int rc = 0;
int ret1;
pthread_t p_thread1;
if(0!=(ret1 = pthread_create(&p_thread1, NULL, shuchu, NULL)))printf("sfdfsdfi\n");
printf("[%d]\n",p_thread1);
pthread_join(p_thread1, NULL);
return TRUE;
}
❷ linux多线程问题:pthread_join 和 pthread_exit 的区别 求指教
pthread_join一般是主线程来调用,用来等待子线程退出,因为是等待,所以是阻塞的,一般主线程会依次join所有它创建的子线程。
pthread_exit一般是子线程调用,用来结束当前线程。
子线程可以通过pthread_exit传递一个返回值,而主线程通过pthread_join获得该返回值,从而判断该子线程的退出是正常还是异常。
❸ 如何在linux上使用boost:thread-C/C++
首先需要安装boost,步骤如下:
下载到boost_1_49_0.tar.bz2 (当然,其他压缩格式也可以)后,可以把它放在用户目录下,即:~/
解压缩:tar -jxvf boost_1_49_0.tar.bz2
这样,出现文件夹:~/boost_1_49_0
然后进入:$ cd boost_1_49_0
你会发现有一个sh命令:bootstrap.sh
运行它:$ ./bootstrap.sh (boost自己的get start文档中说设置参数 --prefix=dir 其中dir为你想指定的安装文件夹,我建议就不用加这个参数,它会默认安装到/usr/local)
结束后出现一个可执行文件: ~/boost_1_49_0/b2
运行这个文件: $ sudo ./b2 install (Ubuntu用户千万别忘了加sudo,不然安装后将无法完全使用)
编译安装时间比较长,根据不同机器的情况20~40分钟。结束后即安装完毕。
boost::thread的使用
#include<boost/thread.hpp>
#include<iostream>
voidtask1(){
//dostuff
std::cout<<"Thisistask1!"<<std::endl;
}
voidtask2(){
//dostuff
std::cout<<"Thisistask2!"<<std::endl;
}
intmain(intargc,char**argv){
usingnamespaceboost;
threadthread_1=thread(task1);
threadthread_2=thread(task2);
//dootherstuff
thread_2.join();
thread_1.join();
return0;
}
编译时的命令为:
$ g++ -I./inlcude -L./lib example.cpp -lboost_thread -o example
编译之后会出现一个 example 的可执行文件,可以运行:./example , 结果显示:
This is task2!
This is task1!
可能你在运行时会出现这样的错误:error while loading shared libraries: libboost_thread.so.1.49.0: cannot open shared object file: No such file or directory
这是因为要用到的库不在默认的环境变量里,可以使用下面的命令添加:
$ sudo ldconfig /usr/local/lib
添加后,再执行./example,这样你就完成了你的第一个boost::thread程序。
❹ 关于Linux 线程pthread_join的用法
Linux系统pthread_join用于挂起当前线程(调用pthread_join的线程),直到thread指定的线程终止运行为止,当前线程才继续执行。
案例代码:
/*******************************************
**Name:pthread_join.c
**用于Linux下多线程学习
**案例解释线程的暂停和结束
**Author:admin
**Date:2015/8/11
**Copyright(c)2015,AllRightsReserved!
**********************************************
#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
void*thread(void*str)
{
inti;
//不调用pthread_join线程函数
for(i=0;i<10;++i)
{
sleep(2);
printf("Thisinthethread:%d ",i);
}
returnNULL;
}
intmain()
{
pthread_tpth;
inti;
intret=pthread_create(&pth,NULL,thread,(void*)(i));
//调用pthread_join线程函数
pthread_join(pth,NULL);
for(i=0;i<10;++i)
{
sleep(1);
printf("Thisinthemain:%d ",i);
}
return0;
}
通过Linux下shell命令执行上面的案例代码:
[root@localhostsrc]#gccpthread_join.c-lpthread
[root@localhostsrc]#./a.out
Thisinthemain:0
Thisinthethread:0
Thisinthemain:1
Thisinthemain:2
Thisinthethread:1
Thisinthemain:3
Thisinthemain:4
Thisinthethread:2
Thisinthemain:5
Thisinthemain:6
Thisinthethread:3
Thisinthemain:7
Thisinthemain:8
Thisinthethread:4
Thisinthemain:9
子线程还没有执行完毕,main函数已经退出,那么子线程也就退出了,“pthread_join(pth,NULL);”函数起作用。
[root@localhostsrc]#gccpthread_join.c-lpthread
[root@localhostsrc]#./a.out
Thisinthethread:0
Thisinthethread:1
Thisinthethread:2
Thisinthethread:3
Thisinthethread:4
Thisinthethread:5
Thisinthethread:6
Thisinthethread:7
Thisinthethread:8
Thisinthethread:9
Thisinthemain:0
Thisinthemain:1
Thisinthemain:2
Thisinthemain:3
Thisinthemain:4
Thisinthemain:5
Thisinthemain:6
Thisinthemain:7
Thisinthemain:8
Thisinthemain:9
这说明pthread_join函数的调用者在等待子线程退出后才继续执行。
❺ Linux线程及同步
linux多线程
1.线程概述
线程是一个进程内的基本调度单位,也可以称为轻量级进程。线程是在共享内存空间中并发的多道执行路径,它们共享一个进程的资源,如文件描述和信号处理。因此,大大减少了上下文切换的开销。一个进程可以有多个线程,也就
是有多个线程控制表及堆栈寄存器,但却共享一个用户地址空间。
2.线程实现
线程创建pthread_create()
所需头文件#include
<pthread.h>
函数原型int
pthread_create
((pthread_t
*thread,
pthread_attr_t
*attr,
thread:线程标识符
attr:线程属性设置
start_routine:线程函数的起始地址
arg:传递给start_routine的参数
函数返回值
成功:0
出错:-1
线程退出pthread_exit();
所需头文件#include
<pthread.h>
函数原型void
pthread_exit(void
*retval)
函数传入值retval:pthread_exit()调用者线程的返回值,可由其他函数如pthread_join
来检索获取
等待线程退出并释放资源pthread_join()
所需头文件#include
<pthread.h>
函数原型int
pthread_join
((pthread_t
th,
void
**thread_return))
函数传入值
th:等待线程的标识符
thread_return:用户定义的指针,用来存储被等待线程的返回值(不为NULL时)
函数返回值
成功:0
出错:-1
代码举例
1.
#include<pthread.h>
2.
#include<stdio.h>
3.
#include<errno.h>
4.
5.
/*线程1*/
6.
void
thread1()
7.
{
8.
int
i=0;
9.
10.
while(1)
11.
{
12.
printf(thread1:%d/n,i);
13.
if(i>3)
14.
pthread_exit(0);
15.
i++;
16.
sleep(1);
17.
}
18.
}
19.
20.
/*线程2*/
21.
void
thread2()
22.
{
23.
int
i=0;
24.
25.
while(1)
26.
{
27.
printf(thread2:%d/n,i);
28.
if(i>5)
29.
pthread_exit(0);
30.
i++;
31.
sleep(1);
32.
}
33.
}
34.
35.
int
main()
36.
{
37.
pthread_t
t1,t2;
38.
39.
/*创建线程*/
40.
pthread_create(&t1,NULL,(void
*)thread1,NULL);
41.
pthread_create(&t2,NULL,(void
*)thread2,NULL);
42.
/*等待线程退出*/
43.
pthread_join(t1,NULL);
44.
pthread_join(t2,NULL);
45.
return
0;
46.
}
3同步与互斥
<1>互斥锁
互斥锁的操作主要包括以下几个步骤。
•
互斥锁初始化:pthread_mutex_init
•
互斥锁上锁:pthread_mutex_lock
•
互斥锁判断上锁:pthread_mutex_trylock
•
互斥锁接锁:pthread_mutex_unlock
•
消除互斥锁:pthread_mutex_destroy
1.
#include<pthread.h>
2.
#include<stdio.h>
3.
#include<errno.h>
4.
5.
int
i=0;/*共享变量*/
6.
pthread_mutex_t
mutex=PTHREAD_MUTEX_INITIALIZER;/*互斥锁*/
7.
8.
void
thread1()
9.
{
10.
int
ret;
11.
while(1)
12.
{
13.
14.
15.
ret=pthread_mutex_trylock(&mutex);/*判断上锁*/
16.
17.
if(ret!=EBUSY)
18.
{
19.
pthread_mutex_lock(&mutex);/*上锁*/
20.
printf(This
is
thread1:%d/n,i);
21.
i++;
22.
pthread_mutex_unlock(&mutex);/*解锁*/
23.
}
24.
sleep(1);
25.
}
26.
}
27.
28.
void
thread2()
29.
{int
ret;
30.
while(1)
31.
{
32.
33.
ret=pthread_mutex_trylock(&mutex);
34.
if(ret!=EBUSY)
35.
{
36.
pthread_mutex_lock(&mutex);
37.
printf(This
is
thread2:%d/n,i);
38.
i++;
39.
pthread_mutex_unlock(&mutex);
40.
}
41.
sleep(1);
42.
}
43.
}
44.
int
main()
45.
{
46.
pthread_t
t1,t2;
47.
pthread_mutex_init(&mutex,NULL);
48.
pthread_create(&t1,NULL,(void
*)thread1,NULL);
49.
pthread_create(&t2,NULL,(void
*)thread2,NULL);
50.
51.
pthread_join(t1,NULL);
52.
pthread_join(t2,NULL);
53.
54.
pthread_mutex_destroy(&mutex);
55.
return
0;
56.
}
<2>信号量
未进行同步处理的两个线程
1.
#include<pthread.h>
2.
#include<stdio.h>
3.
#include<errno.h>
4.
5.
int
i=0;
6.
void
thread1()
7.
{
8.
9.
while(1)
10.
{
11.
printf(This
is
thread1:%d/n,i);
12.
i++;
13.
sleep(1);
14.
}
15.
}
16.
17.
18.
void
thread2()
19.
{
20.
21.
while(1)
22.
{
23.
printf(This
is
thread2:%d/n,i);
24.
i++;
25.
sleep(1);
26.
}
27.
}
28.
29.
int
main()
30.
{
31.
pthread_t
t1,t2;
32.
33.
pthread_create(&t1,NULL,(void
*)thread1,NULL);
34.
pthread_create(&t2,NULL,(void
*)thread2,NULL);