c語言線程間通信方法,c線程間通信的幾種方法

本文目錄一覽:

用C語言如何實現多線程同時運行的情況下,各個線程輸出不同的隨機數?

1、使用pthread庫執行多線程,這個是Linux下的線程庫 Windows下應該有自己的API,不過這種東西一般還是以Linux為標準。pthread_create()創建一個線程,傳入fun()的函數指針就行了。然後這個Beep()的需求要進行線程間通信,可以用共享內存的方法,設一個bool變數flag共享,然後beep的時候設為false,beep完設成true。fun()裡面每次看一下這個flag,是false的話就不做動作等下一秒,基本可以滿足需求。

2、常式:

#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 :主函數在等我完成任務嗎?\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 :主函數在等我完成任務嗎?\n”);

pthread_exit(NULL);

}

void thread_create(void)

{

int temp;

memset(thread, 0, sizeof(thread)); //comment1

/*創建線程*/

if((temp = pthread_create(thread[0], NULL, thread1, NULL)) != 0) //comment2

printf(“線程1創建失敗!\n”);

else

printf(“線程1被創建\n”);

if((temp = pthread_create(thread[1], NULL, thread2, NULL)) != 0) //comment3

printf(“線程2創建失敗”);

else

printf(“線程2被創建\n”);

}

void thread_wait(void)

{

/*等待線程結束*/

if(thread[0] !=0) { //comment4

pthread_join(thread[0],NULL);

printf(“線程1已經結束\n”);

}

if(thread[1] !=0) { //comment5

pthread_join(thread[1],NULL);

printf(“線程2已經結束\n”);

}

}

int main()

{

/*用默認屬性初始化互斥鎖*/

pthread_mutex_init(mut,NULL);

printf(“我是主函數哦,我正在創建線程,呵呵\n”);

thread_create();

printf(“我是主函數哦,我正在等待線程完成任務阿,呵呵\n”);

thread_wait();

return 0;

}

線程之間的通信例子 求一個WINDOWS下多線程間通信的例子,用C語言編寫!

#include

stdio.h

int

main(int

argc,

char

**argv){

CreateThread(NULL,

0,

thread2,

this,

0,

0);

printf(“主線程正在執行!\n”);

return

0;

}

void

thread2(){

sleep(2);//睡2毫秒

printf(“第二個線程在運行!\n”);

}

這個例子可能很簡單,但能說明問題了。

C語言如何實現多線程同時運行

1、點擊菜單欄的「Project」選項卡,下拉列表的最後一項「Project options…」是對當前工程的的屬性進行設置的。

2、選擇彈出對話框中的「Compiler」選項卡。

3、將其中的「Runtime Library」的選擇改為「Multithreaded (LIB)」。

4、將看到對話框最下面的文本框中發生了一些變化,新增了「-MT」選項,這與編譯器一開始所報的錯誤提示給出的解決方案一致。

5、頁面的設置完成後,再對該源碼進行編譯時,就能愉快地看到編譯完全成功。

qt 與c程序怎麼通信

共享內存、管道都是可以的。但其實現在一般來說,沒有特殊的理由的話,socket是進程間通信的首選。c部分添加發送數據的模塊,qt用一個線程來,收到數據就觸發一個signal, 界面(主線程)的一個slot接受這個signal並更新顯示。

c語言實例,linux線程同步的信號量方式 謝謝

這麼高的懸賞,實例放後面。信號量(sem),如同進程一樣,線程也可以通過信號量來實現通信,雖然是輕量級的。信號量函數的名字都以”sem_”打頭。線程使用的基本信號量函數有四個。

     信號量初始化。

     int sem_init (sem_t *sem , int pshared, unsigned int value);

    這是對由sem指定的信號量進行初始化,設置好它的共享選項(linux 只支持為0,即表示它是當前進程的局部信號量),然後給它一個初始值VALUE。

    等待信號量。給信號量減1,然後等待直到信號量的值大於0。

    int sem_wait(sem_t *sem);

    釋放信號量。信號量值加1。並通知其他等待線程。

    int sem_post(sem_t *sem);

    銷毀信號量。我們用完信號量後都它進行清理。歸還佔有的一切資源。

    int sem_destroy(sem_t *sem);

#include stdlib.h  

    #include stdio.h  

    #include unistd.h  

    #include pthread.h  

    #include semaphore.h  

    #include errno.h  

    #define return_if_fail(p) if((p) == 0){printf (“[%s]:func error!/n”, __func__);return;}  

    typedef struct _PrivInfo  

    {  

        sem_t s1;  

        sem_t s2;  

        time_t end_time;  

    }PrivInfo;  

    static void info_init (PrivInfo* thiz);  

    static void info_destroy (PrivInfo* thiz);  

    static void* pthread_func_1 (PrivInfo* thiz);  

    static void* pthread_func_2 (PrivInfo* thiz);  

    int main (int argc, char** argv)  

    {  

        pthread_t pt_1 = 0;  

        pthread_t pt_2 = 0;  

        int ret = 0;  

        PrivInfo* thiz = NULL;  

        thiz = (PrivInfo* )malloc (sizeof (PrivInfo));  

        if (thiz == NULL)  

        {  

            printf (“[%s]: Failed to malloc priv./n”);  

            return -1;  

        }  

        info_init (thiz);  

        ret = pthread_create (pt_1, NULL, (void*)pthread_func_1, thiz);  

        if (ret != 0)  

        {  

            perror (“pthread_1_create:”);  

        }  

        ret = pthread_create (pt_2, NULL, (void*)pthread_func_2, thiz);  

        if (ret != 0)  

        {  

            perror (“pthread_2_create:”);  

        }  

        pthread_join (pt_1, NULL);  

        pthread_join (pt_2, NULL);  

        info_destroy (thiz);  

        return 0;  

    }  

    static void info_init (PrivInfo* thiz)  

    {  

        return_if_fail (thiz != NULL);  

        thiz-end_time = time(NULL) + 10;  

        sem_init (thiz-s1, 0, 1);  

        sem_init (thiz-s2, 0, 0);  

        return;  

    }  

    static void info_destroy (PrivInfo* thiz)  

    {  

        return_if_fail (thiz != NULL);  

        sem_destroy (thiz-s1);  

        sem_destroy (thiz-s2);  

        free (thiz);  

        thiz = NULL;  

        return;  

    }  

    static void* pthread_func_1 (PrivInfo* thiz)  

    {  

        return_if_fail(thiz != NULL);  

        while (time(NULL)  thiz-end_time)  

        {  

            sem_wait (thiz-s2);  

            printf (“pthread1: pthread1 get the lock./n”);  

            sem_post (thiz-s1);  

            printf (“pthread1: pthread1 unlock/n”);  

            sleep (1);  

        }  

        return;  

    }  

    static void* pthread_func_2 (PrivInfo* thiz)  

    {  

        return_if_fail (thiz != NULL);  

        while (time (NULL)  thiz-end_time)  

        {  

            sem_wait (thiz-s1);  

            printf (“pthread2: pthread2 get the unlock./n”);  

            sem_post (thiz-s2);  

            printf (“pthread2: pthread2 unlock./n”);  

            sleep (1);  

        }  

        return;  

    }

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/183937.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-25 05:50
下一篇 2024-11-25 05:50

相關推薦

  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python線程等待指南

    本文將從多個方面詳細講解Python線程等待的相關知識。 一、等待線程結束 在多線程編程中,經常需要等待線程執行完畢再進行下一步操作。可以使用join()方法實現等待線程執行完畢再…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28
  • Python兩個線程交替列印1到100

    這篇文章的主題是關於Python多線程的應用。我們將會通過實際的代碼,學習如何使用Python兩個線程交替列印1到100。 一、創建線程 在Python中,我們可以使用Thread…

    編程 2025-04-28

發表回復

登錄後才能評論