Unix/Linux 编程:多线程编程

来源:互联网 发布:编程自学怎么开始 编辑:程序博客网 时间:2024/06/05 04:02

之前已经用过不少在Windows和C++上多多线程编程技术来。

在Linux上原理大致和Windows是相同多。主要多区别在于了解POSIX规范和一些API

参考文档:http://www.cnblogs.com/skynet/archive/2010/10/30/1865267.html

实际上笔者认为创建线程池和使用epoll(windows上的完成端口)可能更适合开发商用应用程序。但是还是需要理解线程同步多本质。


有关线程操作的函数

#include <pthread.h> int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*func) (void *), void *arg);int pthread_join (pthread_t tid, void ** status);pthread_t pthread_self (void);int pthread_detach (pthread_t tid);void pthread_exit (void *status);


/* 是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:  1)有一int型全局变量g_Flag初始值为0;  2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1  3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2  4)线程序1需要在线程2退出后才能退出  5)主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出   */


/* File: mt.cmultithread demo*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <pthread.h>#include <errno.h>#include <unistd.h>// global variable.int g_Flag = 0;static pthread_mutex_t  mutex   = PTHREAD_MUTEX_INITIALIZER;static pthread_cond_t   cond    = PTHREAD_COND_INITIALIZER;// thread function 1void * thread1(void* );// thread function 2void * thread2(void* );/* * when the process is initialized, the main thread is creatd by auto. * So we just need to create two children thread.*/int main(int argc, char ** argv){    printf("Enter main thread.\n");    pthread_t tid1, tid2;    int rc1 = 0, rc2 = 0;    // create the thread2    rc2 = pthread_create(&tid2, NULL, thread2, NULL);    if(rc2 != 0)        printf("%s: %s\n", __func__, strerror(rc2));        // create the thread1    rc1 = pthread_create(&tid1, NULL, thread1, &tid2);    if(rc1 != 0)        printf("%s: %s\n", __func__, strerror(rc1));        pthread_cond_wait(&cond, &mutex);    printf("Leave main thread.\n");        exit(0);}/* * thread1() will execute by pthread_Create * it will set g_Flag = 1.*/void * thread1(void * arg){    printf("Enter thread1.\n");    printf("this is thread1, g_Flag: %d, thread id is %u.\n",        g_Flag, (unsigned int)pthread_self());        pthread_mutex_lock(&mutex);    if(g_Flag == 2)        pthread_cond_signal(&cond);    g_Flag = 1;    printf("this is thread1, g_Flag: %d, thread id is %u.\n",        g_Flag, (unsigned int)pthread_self());        pthread_mutex_unlock(&mutex);    // wait for the thread2 to be exited at first.    pthread_join(*(pthread_t *)arg, NULL);    printf("Leave thread1.\n");    pthread_exit(0);}/* * thread2() will execute by pthread_Create * it will set g_Flag = 2.*/void * thread2(void * arg){    printf("Enter thread2.\n");    printf("this is thread2, g_Flag: %d, thread id is %u.\n",        g_Flag, (unsigned int)pthread_self());        pthread_mutex_lock(&mutex);    if(g_Flag == 1)        pthread_cond_signal(&cond);    g_Flag = 2;    printf("this is thread2, g_Flag: %d, thread id is %u.\n",        g_Flag, (unsigned int)pthread_self());        pthread_mutex_unlock(&mutex);    printf("Leave thread2.\n");    pthread_exit(0);}

运行结果:

Enter main thread.Enter thread2.this is thread2, g_Flag: 0, thread id is 162025472.Enter thread1.this is thread1, g_Flag: 0, thread id is 162562048.this is thread2, g_Flag: 2, thread id is 162025472.Leave thread2.this is thread1, g_Flag: 1, thread id is 162562048.Leave main thread.LintaodeMBP:MultiThread stjohnson$


原创粉丝点击