欧美自拍小视频_国产片在线免费观看_中文字幕啪啪_成人av高清在线_欧美一区二区视频免费观看_亚洲国产激情


曙海教育集團(tuán)論壇Linux專區(qū)Linux技術(shù)討論區(qū) → 嵌入式基礎(chǔ)__linux多線程編程


  共有6858人關(guān)注過本帖樹形打印

主題:嵌入式基礎(chǔ)__linux多線程編程

美女呀,離線,留言給我吧!
wangxinxin
  1樓 個(gè)性首頁 | 博客 | 信息 | 搜索 | 郵箱 | 主頁 | UC


加好友 發(fā)短信
等級(jí):青蜂俠 帖子:1393 積分:14038 威望:0 精華:0 注冊(cè):2010-11-12 11:08:23
嵌入式基礎(chǔ)__linux多線程編程  發(fā)帖心情 Post By:2010-11-15 9:07:50

簡單的多線程編程
  這里引用的是在百度上找的資料,感覺挺好的,挺適合入門的.
 
轉(zhuǎn):
學(xué)東西,往往實(shí)例才是最讓人感興趣的,老是學(xué)基礎(chǔ)理論,不動(dòng)手,感覺沒有成就感,呵呵。

下面先來一個(gè)實(shí)例。我們通過創(chuàng)建兩個(gè)線程來實(shí)現(xiàn)對(duì)一個(gè)數(shù)的遞加。
或許這個(gè)實(shí)例沒有實(shí)際運(yùn)用的價(jià)值,但是稍微改動(dòng)一下,我們就可以用到其他地方去拉。

下面是我們的代碼:

/*thread_example.c :  c multiple thread programming in linux
  *author : falcon
  *E-mail : tunzhj03@st.lzu.edu.cn
  */

#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10

pthread_t thread[2];
pthread_mutex_t mut;
int number=0, i;

void *thread1()
{
        printf ("thread1 : I'm thread 1\n");

        for (i = 0; i < MAX; i++)
        {
                printf("thread1 : number = %d\n",number);
                pthread_mutex_lock(&mut);
                        number++;
                pthread_mutex_unlock(&mut);
                sleep(2);
        }


        printf("thread1 :主函數(shù)在等我完成任務(wù)嗎?\n");
        pthread_exit(NULL);
}

void *thread2()
{
        printf("thread2 : I'm thread 2\n");

        for (i = 0; i < MAX; i++)
        {
                printf("thread2 : number = %d\n",number);
                pthread_mutex_lock(&mut);
                        number++;
                pthread_mutex_unlock(&mut);
                sleep(3);
        }


        printf("thread2 :主函數(shù)在等我完成任務(wù)嗎?\n");
        pthread_exit(NULL);
}

void thread_create(void)
{
        int temp;
        memset(&thread, 0, sizeof(thread));          //comment1
        /*創(chuàng)建線程*/
        if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)       //comment2
                printf("線程1創(chuàng)建失敗!\n");
        else
                printf("線程1被創(chuàng)建\n");

        if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)  //comment3
                printf("線程2創(chuàng)建失敗");
        else
                printf("線程2被創(chuàng)建\n");
}

void thread_wait(void)
{
        /*等待線程結(jié)束*/
        if(thread[0] !=0) {                   //comment4
                pthread_join(thread[0],NULL);
                printf("線程1已經(jīng)結(jié)束\n");
        }
        if(thread[1] !=0) {                //comment5
                pthread_join(thread[1],NULL);
                printf("線程2已經(jīng)結(jié)束\n");
        }
}

int main()
{
        /*用默認(rèn)屬性初始化互斥鎖*/
        pthread_mutex_init(&mut,NULL);

        printf("我是主函數(shù)哦,我正在創(chuàng)建線程,呵呵\n");
        thread_create();
        printf("我是主函數(shù)哦,我正在等待線程完成任務(wù)阿,呵呵\n");
        thread_wait();

        return 0;


支持(0中立(0反對(duì)(0單帖管理 | 引用 | 回復(fù) 回到頂部

返回版面帖子列表

嵌入式基礎(chǔ)__linux多線程編程








簽名
主站蜘蛛池模板: 真人二十三式性视频(动) | 国产精品美女一区二区三区 | 中文在线а天堂中文在线新版 | 久久超碰极品视觉盛宴 | 国产一级一级片 | 日本一级特黄视频 | 亚洲综合色视频 | 99re6在线视频精品免费下载 | 视频一区二区在线 | 老头天天吃我奶躁我的动图 | 99爱在线视频这里只有精品 | 日韩精品一区二区亚洲av | www亚洲免费 | 性视频网站免费 | 欧美精品成人久久网站 | 丰满人妻一区二区三区免费视频 | 亚洲欧美日韩人成在线播放 | 丰满人妻一区二区三区无码av | 国产精品美女久久久久网站 | 曰韩美女一级视频 | 久久99九九精品免费 | 一本久久a久久免费精品不卡 | 狠狠操综合网 | a级高清 | 曰韩内射六十七十老熟女影视 | 亚洲av极品视觉盛宴 | 国产午夜福利精品一区 | 成人免费视频77777 | 免费爱爱视频 | 久久九九精品一区二区 | 欧美成人免费网在线观看 | 一级爱做片免费观看久久 | 久久国产欧美日韩精品免费 | 18禁亚洲深夜福利入口 | 日韩在线中文 | 日韩 欧美 综合 | 日产亚洲一区二区三区 | 免费亚洲视频 | 青青青在线视频 | 日本a级特黄特黄刺激大片 日本a级网站 | 一区二区免费视频观看 |