[GNU/Linux]线程的终止

#include <stdio.h>

// linux API / pthread
#include <unistd.h>
#include <pthread.h>

int quit_flag = 0; 

// 线程入口函数
void* Thread_Main(void* context)
{
    for(int i=0; i<10 && !quit_flag ; i++)
    {
        printf("in thread main: %d ...\n", i);
        sleep(1);
    }
    printf("thread exit.\n"); // 线程自然退出
    return NULL;
}

// 
int main()
{
    // 创建线程(同时启动线程)
    pthread_t handle;
    if(pthread_create(&handle, NULL, Thread_Main, NULL) < 0)
    {
        printf("failed to create thread!\n");
        return -1;
    }
    
    //getchar();
    //quit_flag=1;
    
    pthread_join(handle, NULL);
    
    printf("main exit.\n");
    return 0;
}

 

[GNU/Linux]pthread_create的参数说明

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

// linux API / pthread
#include <unistd.h>
#include <pthread.h>

struct ThreadParam
{
    int id;
    int max;
};

// 线程入口函数
void* Thread_Main(void* context)
{
    ThreadParam* p = (ThreadParam*) context;
    for(int i=0; i<p->max; i++)
    {
        printf("thread: %d,  count: %d\n", p->id, i);
        sleep(1);
    }
    free(p);
    
    printf("thread : %d  exit . \n");
    return NULL;
}

// 
int main()
{
    // 创建线程(同时启动线程)
    pthread_t h1, h2;
    
    // 创建第1个线程
    ThreadParam* p1 = (ThreadParam*) malloc(sizeof(ThreadParam));
    p1->id = 1;
    p1->max = 10;
    if(pthread_create(&h1, NULL, Thread_Main, p1) < 0)
    {
        printf("failed to create thread!\n");
        return -1;
    }
    
    // 创建第2个线程
    ThreadParam* p2 = (ThreadParam*) malloc(sizeof(ThreadParam));
    p2->id = 2;
    p2->max = 20;
    if(pthread_create(&h2, NULL, Thread_Main, p2) < 0)
    {
        printf("failed to create thread!\n");
        return -1;
    }
    
    
    getchar();
    return 0;
}

 

[GNU/Linux]使用pthread库创建线程

#include <stdio.h>

// linux API / pthread
#include <unistd.h>
#include <pthread.h>

void  msleep(int ms)
{
    timespec ts;
    ts.tv_sec = ms / 1000;
    ts.tv_nsec = (ms % 1000) * 1000000;
    nanosleep(&ts, NULL);
}


// 线程入口函数
void* Thread_Main(void* context)
{
    while(1)
    {
        printf("i am a thread ...\n");
        //::sleep(1);
        msleep(100); // 500ms
    }
    return NULL;
}

// 
int main()
{
    // 创建线程(同时启动线程)
    pthread_t handle;
    if(pthread_create(&handle, NULL, Thread_Main, NULL) < 0)
    {
        printf("failed to create thread!\n");
        return -1;
    }
    
    getchar();
    return 0;
}

 

[GNU/Linux]Linux下读写文件

  • 以写方式打开文件
    int fd = open(“/home/mytest/123.txt”,
    O_WRONLY | O_CREAT,
    0644);
    参数1: 文件路径
    参数2:     标识位,O_WRONLY表示写入,
    O_CREAT表示当不存在时,创建新文件
    参数:0644,表示创建文件时的权限位
    (0644: 字面常量,8进制)

 

  • 以读方式打开文件
    int fd = open(“/home/mytest/123.txt”,
    O_RDONLY);
    参数1:文件路径
    参数2:O_RDONLY: 以只读方式打开
    参数3:省略

 

  • 写文件:
    write(fd,  buf,  n);
    读文件:
    int n = read(fd, buf, maxsize);
    关闭文件:
    close(fd);
    fd:  file descriptor文件描述符,是一个整数

 

  • 以下三者选一:
    O_RDONLY 只读方式
    O_WRONLY 以只写方式打开文件
    O_RDWR 以可读写方式打开文件
    额外的标识位:
    O_CREAT可与O_WRONLY联用,若欲打开的文件不存在则自
    动建立该文件
    O_TRUNC  可与O_WRONLY联用,在打开文件时清空文件
    O_APPEND可与O_WRONLY联用,表示追加内容
    O_NONBLOCK 表示以“非阻塞”方式读/写数据时

read.cpp

// ANSI C
#include <stdio.h>

// Linux API
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
    // 注:你是否对/home/mytest/123.txt有读权限
    int fd = open("/home/mytest/123.txt", O_RDONLY);
    if(fd < 0)
    {
        printf("failed to open file!\n");
        return -1;
    }
    
    char data[12];
    int n = read(fd, data, 12);	
    if(n>0)
    {
        data[n] = 0;
        printf("read: %s \n", data);
    }
    close(fd);
}

write.cpp

// ANSI C
#include <stdio.h>

// Linux API
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
    // 注:你是否对/home/mytest/目录有写权限?
    int fd = open("/home/mytest/123.txt", 
            O_WRONLY|O_CREAT, 
            0644);
    if(fd < 0)
    {
        printf("failed to open file!\n");
        return -1;
    }
    
    char data[12] = "hello";
    write(fd, data, 5);	
    close(fd);
}

stdc.cpp

// ANSI C
#include <stdio.h>
#include <string.h>

int main()
{
    // 注:你是否对/home/mytest/目录有写权限?
    FILE* fp = fopen("/home/mytest/aaa.txt", "wb");
    if(!fp)
    {
        // 无权访问
        printf("failed to open file!\n");
        return -1;
    }
    
    // 使用linux的标准换行符\n
    char buf[] = "hello\nworld\n";
    fwrite(buf, 1, strlen(buf), fp);
    fclose(fp);
}

 

 

信号量机制

#define _CRT_SECURE_NO_WARNINGS /* VS2013,2015需要这一行 */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "osapi/osapi.h"


OS_Mutex g_mutex;
int g_buf[100]; // 缓冲区:最多存放100个数
int g_count = 0;

OS_Semaphore g_sem(0);

// 第一个线程:生产者
class Producer : public OS_Thread
{
public:
    int Routine()
    {
        while(1)
        {
            int r = rand() % 20 + 1; // 生成一个1..20之间的随机数
            OS_Thread::Msleep(50 * r); // 睡觉的间隔50-1000毫秒

            // 存放一个物品(这里就是存一个数, 代表一下物品的意思)
            g_mutex.Lock();
            g_buf[g_count] = r;
            g_count ++;
            printf("放入物品: %d \n", r);
            g_mutex.Unlock();

            g_sem.Post(); // 把信号量的值加1
        }
        return 0;
    }
};

// 第二个线程:消费者
class Consumer : public OS_Thread
{
public:
    int Routine()
    {
        // 轮询机制:频繁查询当前物品的个数
        while(1)
        {
            //OS_Thread::Msleep(800); // 信号量机制不用sleep
            g_sem.Wait(); // 信号量减1

            g_mutex.Lock();
            if(g_count > 0)
            {
                for(int i=0; i<g_count; i++)
                {
                    printf(" ==== 消费物品: %d \n", g_buf[i]);
                }
                g_count = 0;
            }
            g_mutex.Unlock();
        }
        return 0;
    }
};

int main()
{
    srand(time(NULL));

    // 启动第一个线程
    Producer p;
    p.Run();

    // 启动第二个线程
    Consumer c;
    c.Run();

    // 按回车退出程序
    getchar();
    return 0;
}

 

轮询模式

#define _CRT_SECURE_NO_WARNINGS /* VS2013,2015需要这一行 */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "osapi/osapi.h"


OS_Mutex g_mutex;
int g_buf[100]; // 缓冲区:最多存放100个数
int g_count = 0;

// 第一个线程:生产者
class Producer : public OS_Thread
{
public:
    int Routine()
    {
        while(1)
        {
            int r = rand() % 20 + 1; // 生成一个1..20之间的随机数
            OS_Thread::Msleep(50 * r); // 睡觉的间隔50-1000毫秒

            // 存放一个物品(这里就是存一个数, 代表一下物品的意思)
            g_mutex.Lock();
            g_buf[g_count] = r;
            g_count ++;
            printf("放入物品: %d \n", r);
            g_mutex.Unlock();
        }
        return 0;
    }
};

// 第二个线程:消费者
class Consumer : public OS_Thread
{
public:
    int Routine()
    {
        // 轮询机制:频繁查询当前物品的个数
        while(1)
        {
            OS_Thread::Msleep(800);

            g_mutex.Lock();
            if(g_count > 0)
            {
                for(int i=0; i<g_count; i++)
                {
                    printf(" ==== 消费物品: %d \n", g_buf[i]);
                }
                g_count = 0;
            }
            g_mutex.Unlock();
        }
        return 0;
    }
};

int main()
{
    srand(time(NULL));

    // 启动第一个线程
    Producer p;
    p.Run();

    // 启动第二个线程
    Consumer c;
    c.Run();

    // 按回车退出程序
    getchar();
    return 0;
}

 

互斥锁

#define _CRT_SECURE_NO_WARNINGS /* VS2013,2015需要这一行 */
#include <stdio.h>

#include "osapi/osapi.h"

OS_Mutex g_mutex;
char g_key[16]; // Generator更新它,Checker获取它

class KeyGenerator : public OS_Thread
{
private:
    virtual int Routine()
    {
        int times = 0;
        while(1)
        {
            // 生成key: 需要80ms
            char key_new[16];			
            for(int i=0; i<16; i++)
            {
                OS_Thread::Msleep(5);
                key_new[i] = times;
            }

            // 更新key: 占有锁的时间非常短
            g_mutex.Lock();
            memcpy(g_key, key_new, 16);
            g_mutex.Unlock();

            times ++;
            if(times >= 128) times = 0;
            //OS_Thread::Msleep(50);
        }
        return 0; 
    }
};

class KeyChecker : public OS_Thread
{
private:
    // 线程主函数
    virtual int Routine()
    {
        while(1)
        {		
            // 尽量缩短对共享数据的访问时间
            char copy[16];
            g_mutex.Lock();
            memcpy(copy, g_key, 16);
            g_mutex.Unlock();

            // 数据处理
            // 检查完整性			
            for(int i=1; i<16; i++)
            {
                if(copy[i] != copy[i-1])
                {
                    printf("不完整!!\n");
                    PrintKey();
                    //return 0;
                }
            }			

            //OS_Thread::Msleep(50);
        }
        return 0; // 正常退出
    }

    void PrintKey()
    {
        printf("Key: ");
        for(int i=0; i<16; i++)
            printf("%02X ", g_key[i]);					
        printf("\n");
    }
};


int main()
{
    KeyGenerator a;
    a.Run();

    KeyChecker b;
    b.Run();

    getchar();


    return 0;
}

 

线程的停止与回收

#define _CRT_SECURE_NO_WARNINGS /* VS2013,2015需要这一行 */
#include <stdio.h>

#include "osapi/osapi.h"

// 定义一个类
class Buddhist : public OS_Thread
{
public:
    int Start()
    {
        // 其他准备工作
        m_quitflag = false;
        Run();
        return 0;
    }

    void Stop()
    {
        m_quitflag = true;
        Join(this);
    }

private:
    // 线程主函数
    virtual int Routine()
    {
        // 线程体: 执行它的任务
        for(int i=0; !m_quitflag && i<100; i++)
        {
            printf("ma mi ma mi hong ...\n");
            OS_Thread::Sleep(1);
        }
        printf("Task Exit.\n");
        //  保存数据,善后工作
        printf("善后工作...\n");
        return 0; // 正常退出
    }

private:
    bool m_quitflag;

};

int main()
{
    Buddhist  task1;
    task1.Start();

    getchar();
    task1.Stop();

    return 0;
}

 

线程的创建和启动

#define _CRT_SECURE_NO_WARNINGS /* VS2013,2015需要这一行 */
#include <stdio.h>

#include "osapi/osapi.h"

// 定义一个类
class Buddhist : public OS_Thread
{
private:
    virtual int Routine()
    {
        // 线程体: 执行它的任务
        for(int i=0; i<100; i++)
        {
            printf("ma mi ma mi hong ...\n");
            OS_Thread::Sleep(1);
        }
        return 0;
    }
};

class Confucian : public OS_Thread
{
private:
    virtual int Routine()
    {
        for(int i=0; i<500; i++)
        {
            printf("人之初,性本善 ...\n");
            OS_Thread::Sleep(1);
        }
        return 0;
    }
};
int main()
{
    Buddhist  task1;
    task1.Run();

    Confucian  task2;
    task2.Run();

    // 
    printf("--------- 主线程开始 -------\n");
    for(int i=0; i<10; i++)
    {
        printf("********* \n");
        OS_Thread::Sleep(1);
    }

    getchar();

    return 0;
}

 

[语法]文件的创建fopen-fwrite-fclose

(1) fopen: 打开文件
FILE* fopen(const char *filename, const char *mode);
mode: 使用”wb” (w表示write, b表示binary)

c:\abc.txt : 全路径
c:\0\abc.txt

返回值: 文件指针 FILE*
其中,不用关心FILE*的具体类型,只需要知道它是一个指针就行了。

(2) fclose: 关闭文件
int fclose(FILE* stream);

参数: stream就是前面fopen的返回值

示例:
fclose(fp);

 

(3) fwrite: 写入数据
size_t fwrite(const void *buf,
size_t size, size_t count,
FILE *stream);

参数: stream就是前面fopen的返回值
buf, 要写入的数据
size,总是传1
count, 字节数

返回值 :字节数

示例:
char buf[] = “hello”;
fwrite(buf, 1, 5, fp);