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
相关文档:
1. 相关函数 & ......
在x86 2.4内核下 usleep、select等延时函数无法实现低于10ms延时
而在驱动层在ioctrl中通过udelay、mdelay等等实现延时也无法多进程同时延时
所以实现如下延时函数 能够实现低于10us甚至1us 的延时
unsigned int uDelay(unsigned int delayTime)
{
static struct timeval _tstart, _tend;
static struct timezone ......
http://linux.chinaunix.net/doc/system/2005-02-03/1086.shtml
dd 是 Linux/UNIX
下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换。
dd 的主要选项:
指定数字的地方
若以下列字符结尾乘以相应的数字:
b=512, c=1, k=1024, w=2, xm=number m
if=file
输
入文件名,缺� ......