Linux 多线程编程
linux 多线程编程非常简单。
一、多线程编程的流程:
创建线程--->当前线程继续工作--->[与创建的线程同步]--->[等待创建的线程结束与返回]--->当前线程继续工作。
--->创建的线程开始工作--->[与别的线程同步]--->退出线程并返回
线程用的几个主要的函数。
1.线程创建函数:pthread_create(pthread_t * id,pthread_attr_t *attr,void *(*start_routine)(void*),void *arg);
id---线程id ,线程创建成功时的返回值,用于对线程的操作。
attr---线程属性,简单的设为NULL就可以了。
void *(*start_routine)(void*) --- 线程函数,也就工作线程的实际运行部分的代码段。
arg --- 线程参数,是双向的,也就是一个输入输出类型的参数。
2.等待线程返回函数:pthread_join(pthread_t id,void *ret);
id--- 线程id。
ret --- 线程的返回值。
3.退出线程:pthread_exit(void *ret);
ret --- 线程返回值。
一个简单的线程的例子:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *thread_function(void *arg);
char message[] = "Hello World";
int main() {
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if (res != 0) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char *)thr
相关文档:
我已经半年没有使用Windows的方式工作了。Linux高效的完成了我所有的工作。
GNU/Linux不是每个人都想用的。如果你只需要处理一般的事务,打游戏,那么你不需要了解下面这些了。
我不是一个狂热的自由软件份子,虽然我很喜欢自由软件。这篇文章也不是用来推行自由软件运动的,虽然我觉得自由软件运动是非常好的。
这篇 ......
两种方法:
自启动程序方法1:
在etc/rc.local在里面加入/home/robin/code/autoruntest > /dev/null &(其中autoruntest 测试程序名称,下同)。即可自启动
注:/etc/rc.local -> /etc/rc.d/rc.local
自启动程序方法2:
创建linux服务,步骤如下:
1) &n ......
A. 为什么要在Linux使用Eclipse开发C/C++程序?
Linux是一个以C/C++开发为主的平台,无论是Kernel或是Application,主要都使用C/C++开发。传统在Linux下开发程序,是在文字模式下,利用vi等文字编辑器撰写C/C++程序存盘后,在Command line下使用gcc编译,若要debug,则使用gdb。
这种开发方式生产力并不高,若只是开发学 ......
aria2是我今天新学到的一个命令行的下载工具,虽然还没研究透,但是他绝对比wget更好.
aria2 是 Linux 下一个不错的高速下载工具
。由于它具有分段下载引擎,所以支持从多个地址或者从一个地址的多个连接来下载同一个文件。这样自然就大大加快了文件的下载速 度。aria2 也具有断点续传功能,这使你随时能够恢复已经中断 ......