當前位置:首頁 » 編程軟體 » C的多線程編程

C的多線程編程

發布時間: 2023-02-21 10:18:10

① 編寫一個多線程的C程序 分割數據並分發給每個線程

不如貼英文原版要求,中文翻譯出來的要求看著不是很明白

c語言怎樣實現多線程

首先你要有控制蛇移動方向的全局變數(定義在main以外因為線程函數也要調用它,每次鍵盤輸入都會修改它的值), 比如 char direction 'a' ==左 'w' == 右 'd'==上 's' == 下,然後你在移動時應該是在while裡面操作的吧,你每移動一步前都讀一下direction這個變數的數值然後再控制移動方向(注意s這個鍵可以忽略因為不會倒著走) 然後你可以用pthread.h這個庫 例子是 pthread t;// 定義一個線程 pthread_create(&t, null, listen_keyboard_input, null);//建立線程執行listen_keyboard_input這個函數 這個線程執行的函數 void listen_keyboard_input(){ while(應該通過某個信號來退出這個循環,從而表示游戲結束){ direction =getchar(); } } 但是這里存在同步問題, 比如當這個線程的getchar()在給direction輔助的同時,你控制貪吃蛇移動的線程正在調用 direction的值來判斷下一個移動方向,這就會出問題,所以要加一個鎖,叫 mutex lock;這個也定義成全局變數可以使各線程共享。 pthread_mutex_t mutex; //定義一個鎖 pthread_mutex_init(&mutex, null, null);//初始化 然後把函數修改成 void listen_keyboard_input(){ while(應該通過某個信號來退出這個循環,從而表示游戲結束){ pthread_mutex_lock(&mutex); direction =getchar(); pthread_mutex_unlock(&mutex); } } 另外一個控制貪吃蛇移動的時候也要加鎖 while(.....){ char c; pthread_mutex_lock(&mutex); c = direction; pthread_mutex_unlock(&mutex); switch(c){ ................ } ................................... } 這樣就好了 注意你的控制貪吃蛇移動的部分也必須要放在另外一個pthread 裡面執行,如果放在主線程, 主線程會一直等listen_keyboard_input而什麼事都不會做 你把這兩個線程用 pthread_create 創建完成後 用 t1.join(); t2.join(); 就可以使這兩個線程並發執行了 如果你用的是linux編譯的,你再輸入gcc 指令後加上 -lpthread 就可以了 還有什麼不懂的你可以多找找 pthread 類的例子

③ c語言中怎樣創建多線程

/*這是我寫的最簡單的多線程程序,看懂不?*/
#include <windows.h>
#include <stdio.h>
//#include <strsafe.h>

DWORD WINAPI ThreadProc1( LPVOID lpParam )
{

int i=0,j=0;
while(1)
{
printf("hello,this thread 1 ...\n");

//延時
for(i=0;i<200000000;i++)
{
;
}
}
}

DWORD WINAPI ThreadProc2( LPVOID lpParam )
{

int i=0,j=0;
while(1)
{
printf("hello,this thread 2 ...\n");

//延時
for(i=0;i<200000000;i++)
{
;
}
}
}

void main()
{
int i=0;
//創建線程1
CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc1, // thread function
NULL, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier

//創建線程2
CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc2, // thread function
NULL, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier

//讓主線程進入循環,主線程若退出,子線程1,2會被系統「殺死」
while(1)
{
printf("hello,this thread 0 ...\n");

//延時
for(i=0;i<200000000;i++)
{;}

}
}

④ 關於C++多線程編程教學

在Windows NT和Windows 9x中,多線程的編程實現需要調用一系列的API函數,如CreateThread、ResumeThread等,比較麻煩而且容易出錯。我們使用Inprise公司的新一代RAD開發工具C++Builder,可以方便地實現多線程的編程。與老牌RAD工具Visual Basic和Delphi比,C++Builer不僅功能非常強大,而且它的編程語言是C++,對於系統開發語言是C的Windows系列操作系統,它具有其它編程語言無可比擬的優勢。利用C++Builder提供的TThread對象,多線程的編程變得非常簡便易用。那麼,如何實現呢?且待我慢慢道來,讓你體會一下多線程的強大功能。

1. 創建多線程程序:

首先,先介紹一下實現多線程的具體步驟。在C++Builder中雖然用Tthread對象說明了線程的概念,但是Tthread對象本身並不完整,需要在TThread下新建其子類,並重載Execute方法來使用線程對象。在C++Builder下可以很方便地實現這一點。

在C++Builder IDE環境下選擇菜單File|New,在New欄中選中Thread Object,按OK,接下來彈出輸入框,輸入TThread對象子類的名字MyThread,這樣C++Builder自動為你創建了一個名為TMyThread的TThread子類。同時編輯器中多了一個名為Unit2.cpp的單元,這就是我們創建的TMyThread子類的原碼,如下:

#include
#pragma hdrstop

#include 「Unit2.h」
#pragma package(smart_init)
//---------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall MyThread::UpdateCaption()
// {
// Form1->Caption = 「Updated in a thread」;
// }
//--------------------
__fastcall MyThread::MyThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//--------------------
void __fastcall MyThread::Execute()
{
//---- Place thread code here ----
}
//---------------------

其中的Execute()函數就是我們要在線程中實現的任務的代碼所在處。在原代碼中包含Unit2.cpp,這個由我們創建的TMyThread對象就可以使用了。使用時,動態創建一個TMyThread 對象,在構造函數中使用Resume()方法,那麼程序中就增加了一個新的我們自己定義的線程TMyThread,具體執行的代碼就是Execute()方法重載的代碼。要載入更多的線程,沒關系,只要繼續創建需要數量的TMyThread 對象就成。

⑤ c語言如何編寫一個簡單的多線程程序

這是一個多線程例子,裡面只有兩個線程,是生產者/消費者模式,已編譯通過,注釋很詳細,x0dx0a如下:x0dx0ax0dx0a/* 以生產者和消費者模型問題來闡述Linux線程的控制和通信你 x0dx0a 生產者線程將生產的產品送入緩沖區,消費者線程則從中取出產品。x0dx0a 緩沖區有N個,是一個環形的緩沖池。x0dx0a */x0dx0a#include x0dx0a#include x0dx0ax0dx0a#define BUFFER_SIZE 16x0dx0ax0dx0astruct prodconsx0dx0a{x0dx0a int buffer[BUFFER_SIZE];/*實際存放數據的數組*/x0dx0a pthread_mutex_t lock;/*互斥體lock,用於對緩沖區的互斥操作*/x0dx0a int readpos,writepos; /*讀寫指針*/x0dx0a pthread_cond_t notempty;/*緩沖區非空的條件變數*/x0dx0a pthread_cond_t notfull;/*緩沖區未滿 的條件變數*/x0dx0a};x0dx0ax0dx0a/*初始化緩沖區*/x0dx0avoid pthread_init( struct prodcons *p)x0dx0a{x0dx0a pthread_mutex_init(&p->lock,NULL);x0dx0a pthread_cond_init(&p->notempty,NULL);x0dx0a pthread_cond_init(&p->notfull,NULL);x0dx0a p->readpos = 0;x0dx0a p->writepos = 0;x0dx0a}x0dx0ax0dx0a/*將產品放入緩沖區,這里是存入一個整數*/x0dx0avoid put(struct prodcons *p,int data)x0dx0a{x0dx0a pthread_mutex_lock(&p->lock);x0dx0a /*等待緩沖區未滿*/x0dx0a if((p->writepos +1)%BUFFER_SIZE ==p->readpos)x0dx0a {x0dx0a pthread_cond_wait(&p->notfull,&p->lock);x0dx0a }x0dx0a p->buffer[p->writepos] =data;x0dx0a p->writepos++;x0dx0a if(p->writepos >= BUFFER_SIZE)x0dx0a p->writepos = 0;x0dx0a pthread_cond_signal(&p->notempty);x0dx0a pthread_mutex_unlock(&p->lock);x0dx0a}x0dx0a/*從緩沖區取出整數*/x0dx0aint get(struct prodcons *p)x0dx0a{x0dx0a int data;x0dx0a pthread_mutex_lock(&p->lock);x0dx0a /*等待緩沖區非空*/x0dx0a if(p->writepos == p->readpos)x0dx0a {x0dx0a pthread_cond_wait(&p->notempty ,&p->lock);//非空就設置條件變數notemptyx0dx0a }x0dx0a /*讀書據,移動讀指針*/x0dx0a data = p->buffer[p->readpos];x0dx0a p->readpos++;x0dx0a if(p->readpos == BUFFER_SIZE)x0dx0a p->readpos = 0;x0dx0a /*設置緩沖區未滿的條件變數*/x0dx0a pthread_cond_signal(&p->notfull);x0dx0a pthread_mutex_unlock(&p->lock);x0dx0a return data;x0dx0a}x0dx0a /*測試:生產站線程將1 到1000的整數送入緩沖區,消費者線程從緩沖區中獲取整數,兩者都列印信息*/x0dx0a#define OVER (-1)x0dx0astruct prodcons buffer;x0dx0avoid *procer(void *data)x0dx0a{x0dx0a int n;x0dx0a for( n=0;n<1000;n++)x0dx0a {x0dx0a printf("%d ------>\n",n);x0dx0a put(&buffer,n);x0dx0a }x0dx0a put(&buffer,OVER);x0dx0a return NULL;x0dx0a}x0dx0avoid *consumer(void *data)x0dx0a{x0dx0a int d;x0dx0a while(1)x0dx0a {x0dx0a d = get(&buffer);x0dx0a if(d == OVER)x0dx0a break;x0dx0a elsex0dx0a printf("----->%d\n",d);x0dx0a }x0dx0a return NULL;x0dx0a}x0dx0aint main()x0dx0a{x0dx0a pthread_t th_p,th_c;x0dx0a void *retval;x0dx0a pthread_init(&buffer);x0dx0a pthread_create(&th_p,NULL,procer,0);x0dx0a pthread_create(&th_c,NULL,consumer,0);x0dx0a /*等待兩個線程結束*/x0dx0a pthread_join(th_p, &retval);x0dx0a pthread_join(th_c,&retval);x0dx0a return 0;x0dx0a}

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

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

⑦ C語言能實現多線程么

可以通過調用C語言函數庫pthread里的函數,創建多線程。

多線程是指程序中包含多個執行流,即在一個程序中可以同時運行多個不同的線程來執行不同的任務,也就是說允許單個程序創建多個並行執行的線程來完成各自的任務。

C語言最初並未設計多線程的機制,隨著軟硬體的發展及需求的發展,C語言才開發了線程庫以支持多線程的操作和應用。

⑧ c語言中怎樣創建多線程。最好有一個例子,謝謝!!

/*這是我寫的最簡單的多線程程序,看懂不?*/
#include <windows.h>
#include <stdio.h>
//#include <strsafe.h>

DWORD WINAPI ThreadProc1( LPVOID lpParam )
{

int i=0,j=0;
while(1)
{
printf("hello,this thread 1 ...\n");

//延時
for(i=0;i<200000000;i++)
{
;
}
}
}

DWORD WINAPI ThreadProc2( LPVOID lpParam )
{

int i=0,j=0;
while(1)
{
printf("hello,this thread 2 ...\n");

//延時
for(i=0;i<200000000;i++)
{
;
}
}
}

void main()
{
int i=0;
//創建線程1
CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc1, // thread function
NULL, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier

//創建線程2
CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc2, // thread function
NULL, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier

//讓主線程進入循環,主線程若退出,子線程1,2會被系統「殺死」
while(1)
{
printf("hello,this thread 0 ...\n");

//延時
for(i=0;i<200000000;i++)
{;}

}
}

⑨ c語言實現多線程

目錄:

  1. Linux操作系統,C語言實現多線程

  2. Windows操作系統,C語言實現多線程

  3. Windows下的多線程(不帶停止)

Linux操作系統,C語言實現多線程:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void*ThreadOne(void*threadArg)
{
printf("線程開始啦,參數是:%s ",(char*)threadArg);
returnNULL;
}
intmain(void)
{
pthread_tThreadID;/*記錄線程標識符*/
void*waitingResult;/*等待線程退出的等待結果*/
interrorCode;/*記錄線程的錯誤代碼*/
char*aMessage="這是線程的參數";
/*創建並啟動線程ThreadOne。若返回值非零,則線程創建失敗*/
errorCode=pthread_create(&ThreadID,NULL,ThreadOne,aMessage);
if(errorCode!=0)
{
printf("線程ThreadOne創建失敗。錯誤代碼:%d ",errorCode);
returnEXIT_FAILURE;
}
/*等待線程標識符為的ThreadID的線程結束*/
errorCode=pthread_join(ThreadID,&waitingResult);
if(errorCode!=0)
{
printf("等待線程退出等待失敗。錯誤代碼:%d ",errorCode);
returnEXIT_FAILURE;
}
printf("線程的返回值是%p ",waitingResult);
returnEXIT_SUCCESS;
}

Windows操作系統,C語言實現多線程:

#include<stdio.h>
#include<windows.h>
DWORDAPIENTRYThreadOne(LPVOIDthreadArg)
{
printf("線程開始啦,參數是:%s ",(char*)threadArg);
return0;
}
intmain(void)
{
HANDLEhThread;/*記錄線程句柄*/
DWORDThreadID;/*記錄線程ID號*/
DWORDwaitingResult;/*等待線程退出的等待結果*/
DWORDthreadExitCode;/*記錄線程的返回值*/
char*aMessage="這是線程的參數";
/*創建並啟動線程ThreadOne,返回值為線程句柄,賦值給hThread*/
hThread=CreateThread(NULL,0L,ThreadOne,(LPVOID)aMessage,0L,&ThreadID);
if(hThread==NULL)
{
printf("線程ThreadOne創建失敗。錯誤代碼:%lu ",GetLastError());
returnEXIT_FAILURE;
}
/*等待線程句柄為的hThread線程結束*/
waitingResult=WaitForSingleObject(hThread,INFINITE);
if(waitingResult==WAIT_FAILED)
{
printf("等待線程退出等待失敗。錯誤代碼:%lu ",GetLastError());
returnEXIT_FAILURE;
}
if(GetExitCodeThread(hThread,&threadExitCode))
printf("線程的返回值是%lu ",threadExitCode);
else
printf("獲取線程的返回值獲取失敗。錯誤代碼:%lu ",GetLastError());
returnEXIT_SUCCESS;
}

Windows下的多線程:(不帶停止)

#include<stdio.h>
#include<windows.h>
DWORDWINAPIoxianchen(LPVOIDlpParam);
intmain(intargc,char*argv[])
{
intnum=0;
CreateThread(NULL,NULL,oxianchen,&num,NULL,NULL);
while(1)
{
num++;
printf("主線程!%05d ",nu***eep(40);
}
return0;
}
DWORDWINAPIoxianchen(LPVOIDlpParam)
{
int*a=lpParam;
while(1)
{
++*a;
printf("副線程!%05d0x%p ",*a,a);
Sleep(80);
}
return0;
}

⑩ C語言多線程的操作步驟

線程創建
函數原型:intpthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg);
返回值:若是成功建立線程返回0,否則返回錯誤的編號。
形式參數:pthread_t*restrict tidp要創建的線程的線程id指針;const pthread_attr_t *restrict attr創建線程時的線程屬性;void *(start_rtn)(void)返回值是void類型的指針函數;void *restrict arg start_rtn的形參。
線程掛起:該函數的作用使得當前線程掛起,等待另一個線程返回才繼續執行。也就是說當程序運行到這個地方時,程序會先停止,然後等線程id為thread的這個線程返回,然後程序才會斷續執行。
函數原型:intpthread_join(pthread_tthread, void **value_ptr);
參數說明如下:thread等待退出線程的線程號;value_ptr退出線程的返回值。
返回值:若成功,則返回0;若失敗,則返回錯誤號。
線程退出
函數原型:voidpthread_exit(void *rval_ptr);
獲取當前線程id
函數原型:pthread_tpthread_self(void);
互斥鎖
創建pthread_mutex_init;銷毀pthread_mutex_destroy;加鎖pthread_mutex_lock;解鎖pthread_mutex_unlock。
條件鎖
創建pthread_cond_init;銷毀pthread_cond_destroy;觸發pthread_cond_signal;廣播pthread_cond_broadcast;等待pthread_cond_wait。

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:746
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:1010
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:716
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:878
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:774
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1124
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:349
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:227
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:911
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:873