c語言線程創建
A. c語言怎麼創建線程和使用
用 pthread_t創建線程名字。然後pthread_create開辟線程。
具體使用。
比如有一個函數
void *hello()
{
printf("create pthread!\n");
}
,然後在main函數裡面調用,
int main()
{
pthread_t a_thread;
pthread_create(&a_thread, NULL, (void *)hello, NULL);
}
這樣就完成了hello()函數的創建和使用,接下來hello函數就會在一個線程中運行
B. C語言創建線程問題(急)
你調用pthread_create之後就return掉了,然後程序結束了,在thread_fun執行之前就結束了,自然就沒列印那個出來
你可以在pthread_create之後卡個幾秒鍾(用sleep)或者用pthread_join還是啥的等線程結束再退出,就能看到thread_fun的輸出了
C. c語言創建線程的時候實質上做了什麼
#include
#include
#include
#include
#include
#define MAX 10
pthread_t thread[2];
pthread_mutex_t mut;
int number=0, i;
void *thread1(void*)
{
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(void*)
{
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;
}
以上編譯注意以下問題,在正常編譯下會出現:
undefined reference to 'pthread_create'
undefined reference to 'pthread_join'
錯誤,
解決方式1:在用命令行編譯的時候,在後面加上gcc.......-l pthread;
解決方式2:在eclipse 下,點擊project->c/c++ build-->settings-->gcc linker -->Libraries-->添加pthread
重新build project
在說一個eclipse cdt 下eclipse新手的問題,經常出現binary file not find ....
修改文件,,點擊保存,,project-->build project問題解決,
D. C語言怎麼寫線程代碼
通常使用CreateThread函數來創建新的線程.(Unix下使用pthread_create函數)
首先指出,線程與線程之間,是並列關系,不會存在"父子線程"的概念.
在Windows平台下,CreateThread函數包含在 Windows.h 文件內,包含此文件即可正常使用.
以下為CreateThread函數的聲明:
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,//指向安全性屬性描述結構體的
//指針,通常可以忽略的.
SIZE_T dwStackSize,//指定新線程初始的棧大小,若不關心,可以用0填充,來要求使用
//默認值
LPTHREAD_START_ROUTINE lpStartAddress,//用來充當線程的函數的指針.
LPVOID lpParameter,//要傳遞給函數的參數,這個值本身就是那個參數,而不是參數的地址
DWORD dwCreationFlags,//創建的方式,0表示正常,創建後立即開始運行
LPDWORD lpThreadId//用來接受函數反饋的線程ID的指針.
);
用來充當新的線程的函數格式:
DWORD WINAPI ThreadProc(LPVOID);
CreateThread函數若成功了,返回新線程的句柄,若失敗了,則返回NULL.
若用CREATE_SUSPENDED填充dwCreation Flags則創建的線程先掛起來,並不直接開始運行,要用ResumeThread函數恢復線程,才能繼續運行.
E. 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++)
{;}
}
}
F. C語言中 怎麼實現雙線程 或者 父子線程啊
運行一個程序,這個運行實體就是一個「進程」。
例如,用滑鼠雙擊IE瀏覽器的圖標,你運行了一個IE「進程」。第一個窗未關,你又用滑鼠雙擊IE瀏覽器的圖標,又出來一個瀏覽器的窗。這時,你運行了同一個程序的兩個進程。
對於自己寫的程序也如此。運行它,這個運行實體就是一個「進程」。同時運行兩個,就是兩個進程。計算機分別對兩個進程分配資源,直到進程結束,收回資源。
線程是進程里真真跑的線路,真真執行的運算,每個進程有一個主線程。進程里可以開第二第三條新的執行線路,gcc 用 pthread_create(),VC++ 用 CreateThread(), 這就叫雙線程和多線程。進程是線程的容器,同一進程的線程共享它們進程的資源。線程里建的線程就是父子線程。
兩個或多個進程協同工作時,需要互相交換信息,有些情況下進程間交換的少量信息,有些情況下進程間交換大批信息。這就要通訊。通訊方式不止一種。管道就是一種。VC++ 用 CreatePipe() 函數建立。
管道的實質是一個共享文件,可藉助於文件系統的機制實現,創建、打開、關閉和讀寫.
一個進程正在使用某個管道寫入或讀出數據時,另一個進程就必須等待. 發送者和接收者雙方必須知道對方是否存在,如果對方已經不存在,就沒有必要再發送信息.,發送信息和接收信息之間要協調,當寫進程把一定數量的數據寫入管道,就去睡眠等待,直到讀進程取走數據後,把它喚醒。
VC++ 線程例子:
#include <windows.h>
#include <iostream.h>
DWORD WINAPI fun1(LPVOID lp);
DWORD WINAPI fun2(LPVOID lp);
int piao=500;
int main()
{
HANDLE pthread1,pthread2;
pthread1=CreateThread(0,0,fun1,0,0,0);
pthread2=CreateThread(0,0,fun2,0,0,0);
CloseHandle(pthread1);
CloseHandle(pthread2);
Sleep(3000);
return 0;
}
DWORD WINAPI fun1(LPVOID lp)
{
while(1)
{
if(piao>0)
cout<< "thread-1-"<< piao--<<endl;
else
break;
}
return 0;
}
DWORD WINAPI fun2(LPVOID lp)
{
while(1)
{
if(piao>0)
cout<<"thread-2-"<<piao--<<endl;
else
break;
}
return 0;
}
===================================
建管道函數原形:
BOOL CreatePipe(
PHANDLE hReadPipe, // read handle
PHANDLE hWritePipe, // write handle
LPSECURITY_ATTRIBUTES lpPipeAttributes, // security attributes
DWORD nSize // pipe size
);
G. C語言如何創建線程(windows)系統中
下面為C語言調用WIN API實現創建線程:
1,導入<windows.h>頭文件
2,聲明實現方法DWORD WINAPI ThreadProc1( LPVOID lpParam ) {}
3,在main()方法中調用 CreateThread(NULL,0 ,ThreadProc1,NULL,0,NULL);
要注意的是主線程不能結束,如果主線程結束,則它的子線程也會被殺死。
#include <windows.h>
#include <stdio.h>
#include<time.h>
DWORD WINAPI ThreadProc1( LPVOID lpParam )
{
int i=0;
time_t timer;
while(1)
{
timer=time(NULL);
printf("The current time is: %s\n",asctime(localtime(&timer)));
sleep(1);
}
}
void main()
{
int i=0;
//讓主線程進入循環,主線程若退出,子線程1,2會被系統「殺死」
//創建線程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
for(;;)
{
;
}
}
H. c語言怎麼創建線程和使用
進程的生命周期:
[1].創建 --- fork
[2].執行 --- a. exec
b.子進程實現代碼邏輯
[3].結束 --- exit _exit
僵屍態進程---wait waitpid
孤兒進程
--------------------------------------
進程存在的問題:
(1).進程的創建 --- 復制
(時間 和 空間的開銷很大)
(2).進程的運行 --- 調度-->
I. 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。
J. 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++)
{;}
}
}