黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 多線程同步的例子

多線程同步的例子

更新時間:2021-10-09 10:45:11 來源:動力節(jié)點 瀏覽1224次

線程是一個執(zhí)行單元,由其程序計數(shù)器、堆棧和一組寄存器組成。人們總是混淆線程和進程,區(qū)別很簡單,進程提供執(zhí)行程序所需的資源,而線程是進程內(nèi)可以調(diào)度執(zhí)行的實體。線程比進程有很多優(yōu)點,主要的優(yōu)點是,線程通過并行改進應(yīng)用程序。我們將利用這個并行概念來編寫一個簡單的 C 并理解為什么我們需要線程同步。

讓我們編寫一個具有單線程的簡單 C 程序。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
volatile long int a = 0;
int main()
{
    int i;
    a = 0;
    for(i = 0; i < 100000; i++)
    {
        a = a + i;
    }
    printf("%ld\n",a);
    return 0;
}

上面的代碼很簡單,它是單線程的,不用擔(dān)心并行性。當(dāng)我們執(zhí)行并運行上面的代碼時,我們每次都會得到4999950000。該代碼具有三個主要的機器代碼指令。

LDR R0, a              
ADD R0, R0, R1
STR R0, a

現(xiàn)在讓我們稍微修改一下多線程的代碼,看看它的行為。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
volatile long int a = 0;
pthread_mutex_t aLock;
void threadFunc()
{
    int i;
    for (i = 1; i < 200000 ; i++)
    {
        a = a + 1;
    }
}
void threadFunc2()
{
    int i;
    for(i = 200000; i <= 500000; i++)
    {
        a = a + i;
    }
}
int main()
{
    pthread_t th_one, th_two; 
    int i;
    a = 0;
    pthread_create(&th_one, NULL, (void*)&threadFunc, NULL);  // Create a new thread for threadFunc
    pthread_create(&th_two, NULL, (void*)&threadFunc2, NULL); // Create a new thread for threadFunc2
    pthread_join(th_one, NULL);  // waiting to join thread "th_one" without status
    pthread_join(th_two, NULL);  // waiting to join thread "th_two" without status
    printf("%ld\n",a);
    return 0;
}

我們創(chuàng)建了兩個新線程threadFunc和threadFunc2。讓我們用命令編譯上面的代碼

$ gcc -pthread m_thread.c -o m_thread

當(dāng)我們多次運行二進制 m_thread 時,我們會看到類似這樣的內(nèi)容

令我們驚訝的是,輸出是不確定的。那么,造成這種奇怪現(xiàn)象的原因可能是什么?為什么我的電腦會這樣?答案是,我們沒有處理線程同步。讓我們了解為什么會發(fā)生這種情況。

在 SC1 中,'a' 被加載到 th1 的本地內(nèi)存中,然后它執(zhí)行操作并存儲回主內(nèi)存。類似地,在 SC2 中,'a' 被加載到 th2 的本地內(nèi)存中,并在操作后存儲回主內(nèi)存。現(xiàn)在,在 SC3 'a' 由 th1 和 th2 獲取,因為沒有線程同步,因此我們可以看到 th1 存儲的值丟失了,操作后基本上由 th2 更新。這是我為我們的理解添加的一個結(jié)果。然而,每次我們朗姆酒二進制可執(zhí)行文件時,后臺都會發(fā)生許多這樣的后果,導(dǎo)致不同的輸出。

現(xiàn)在,我們?nèi)绾螌崿F(xiàn)進程同步?一種方法是使用互斥鎖。請參閱下面的修改示例。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
volatile long int a = 0;
pthread_mutex_t aLock;
void threadFunc(void* arg)
{
    int i;
    for (i = 1; i < 200000 ; i++)
    {
        pthread_mutex_lock(&aLock); // Lock a mutex for a
        a = a + 1;
        pthread_mutex_unlock(&aLock); // Unlock a mutex for a
    }
}
void threadFunc2(void *arg)
{
    int i;
    for(i = 200000; i <= 500000; i++)
    {
        pthread_mutex_lock(&aLock); // Lock a Mutex for a
        a = a + i;
        pthread_mutex_unlock(&aLock); // Unlock a mutex for a
    }
}
int main()
{
    pthread_t th_one, th_two; 
    int i;
    a = 0;
    pthread_create(&th_one, NULL, (void*)&threadFunc, NULL);  // Create a new thread for threadFunc
    pthread_create(&th_two, NULL, (void*)&threadFunc2, NULL); // Create a new thread for threadFunc2
    pthread_join(th_one, NULL);  // waiting to join thread "one" without status
    pthread_join(th_two, NULL);  // waiting to join thread "two" without status
    printf("%ld\n",a);
    return 0;

當(dāng)我們執(zhí)行上述二進制文件時,我們每次運行都會得到相同的輸出。

在這種方法中,在代碼的入口部分,在臨界區(qū)內(nèi)修改和使用的關(guān)鍵資源上獲取一個 LOCK,并在退出部分釋放 LOCK。由于資源在進程執(zhí)行其臨界區(qū)時被鎖定,因此其他進程無法訪問它。

以上就是多線程同步的例子,大家若想了解更多相關(guān)信息,可以關(guān)注一下動力節(jié)點的Java多線程編程教程,里面有更詳細(xì)的知識可以學(xué)習(xí),希望對大家能夠有所幫助。

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 久青草视频在线观看 | 99久久伊人 | 99在线看 | 一级黄色日b片 | 男女日批视频在线永久观看 | 午夜一级毛片免费视频 | 亚洲福利三区 | 免费国产成人高清在线观看不卡 | 一一本大道香蕉大无l吗 | 一本久道久久综合 | 人人看人人搞 | 色天天天综合色天天碰 | 九九久久国产精品免费热6 九九久久亚洲综合久久久 九九伦理 | 欧美a色爱欧美综合v | 亚洲剧场午夜在线观看 | 人成18亚洲资源在线 | 亚洲欧美网址 | 成年视频国产免费观看 | 2020国产成人久久精品 | 丝袜国产| 午夜一级免费视频 | 久久久www免费人成看片 | 一级做a免费观看大全 | 国产黄色片一级 | 日日摸日日添夜夜爽97 | 夜色私人影院永久入口 | 亚洲人成网站在线观看播放 | 欧美日韩在线视频观看 | 日韩精品免费观看 | 一级录像免费录像 | 麻豆工作室 | 在线99视频 | 国产精品2020观看久久 | 高h浪诱受肉耽文 | 中国一级毛片aaa片 中国一级毛片 | 手机看片1204国内基地在线 | 国产成人啪精品视频免费软件 | 又色又爽又黄的三级视频在线观看 | 国产在线欧美日韩一区二区 | 国产成人久久精品激情91 | 人人射人人插 |