大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

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

多線程同步的例子

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

線程是一個執(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;
}

上面的代碼很簡單,它是單線程的,不用擔心并行性。當我們執(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

當我們多次運行二進制 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ā)生許多這樣的后果,導致不同的輸出。

現(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;

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

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

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

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 蜜桃日本一道无卡不码高清 | 久草免费在线观看 | 欧美一区亚洲 | 欧美亚洲h在线一区二区 | 久久精品影视 | 天天躁日日躁狠狠躁中文字幕 | 日日艹夜夜 | 不卡视频在线 | www亚洲视频 | 性感美女香蕉视频 | 亚洲欧美一区二区三区国产精品 | 亚洲精品福利一区二区 | 亚洲一区中文字幕在线观看 | 国产一区二区在线免费观看 | 欧美日韩亚洲国产精品 | 欧美一级成人毛片影院 | 奇米在线视频观看 | 成人高清毛片a | 亚洲va欧美va人人爽夜夜嗨 | 国产网红在线观看 | 国产中文字幕亚洲 | 日本不卡1 | 欧美一级毛片欧美一级 | 大学生一级毛片高清版 | 日韩中文字幕高清在线专区 | 欧美成人h精品网站 | 夜夜爽网站 | 亚洲精品日韩中文字幕久久久 | 日本无翼乌全彩无遮挡动漫 | 欧美国产在线观看 | 9久9久热精品视频在线观看 | 99久久精品男女性高爱 | 五月天色中色 | 国产一区二区三区在线 | 欧美成人毛片在线视频 | 色婷婷影视| 丁香婷婷影音先锋5566 | 亚洲精品高清国产一久久 | 青青热在线精品视频免费 | 成人美女黄网站色大色费 | 国产精品嫩草研究所永久网址 |