本文目錄一覽:
- 1、用C語言如何實現多線程同時運行的情況下,各個線程輸出不同的隨機數?
- 2、線程之間的通信例子 求一個WINDOWS下多線程間通信的例子,用C語言編寫!
- 3、C語言如何實現多線程同時運行
- 4、qt 與c程序怎麼通信
- 5、c語言實例,linux線程同步的信號量方式 謝謝
用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