本文目錄一覽:
- 1、c語言多線程編程問題
- 2、C語言多線程實現
- 3、用C語言寫多線程程序
- 4、C語言如何創建線程(windows)系統中
- 5、c語言怎麼創建線程和使用?
- 6、用C語言在windows或者Linux上面,編寫一個多線程程序
c語言多線程編程問題
C語言中多線程的實現原理就是線程的原理,本人只了解Linux下面的C,linux下面的線程,不就是進程中的一個控制流么,相對來說代碼很簡單,但是原理卻是很複雜,很難說清,還需要自己詳細學習研究,下面是一個很簡單的例子,哪邊都能找到,自己運行下看看吧
#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;
}
C語言多線程實現
多線程隨機選號程序
以下程序運行後看起來比較有意思,像一個隨機選號程序,但不是完全按照問題所說的寫的
可供參考,要改很容易
//多線程隨機選號程序示例
#include
stdio.h
#include
Windows.h
#include
ctime
#include
cstdlib
#include
process.h
bool
g_run
=
true;
//是否運行
void
userInput(void*)
//監視輸入的線程函數
{
while
(true)
{
if
(getchar()==’\n’)
//是否輸入回車
{
g_run
=
!g_run;
//回車運行
回車暫停
}
Sleep(10);
//延遲
}
}
int
main()
{
srand(time(0));
//隨機數種子
_beginthread(userInput,0,NULL);
//開線程
while
(true)
{
if
(g_run)
{
system(“cls”);
//清屏
int
t
=
rand()
%
1000+
1;//1-1000的隨機數
printf(“\n
%d”,t);
//輸出
}
Sleep(50);
//延遲50毫秒
}
return
0;
}
用C語言寫多線程程序
thread_creation.c
gcc thread_creation.c
會在當前目錄下,生成可執行的a.out文件
./a.out
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
#includetime.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(;;)
{
;
}
}
c語言怎麼創建線程和使用?
進程的生命周期:
[1].創建 — fork
[2].執行 — a. exec
b.子進程實現代碼邏輯
[3].結束 — exit _exit
殭屍態進程—wait waitpid
孤兒進程
————————————–
進程存在的問題:
(1).進程的創建 — 複製
(時間 和 空間的開銷很大)
(2).進程的運行 — 調度–
pthread_create創建一個線程,thread是用來表明創建線程的ID,attr指出線程創建時候的屬性,我們用NULL來表明使用缺省屬性。start_routine函數指針是線程創建成功後開始執行的函數,arg是這個函數的唯一一個參數。表明傳遞給start_routine的參數。
pthread_exit函數和exit函數類似用來退出線程.這個函數結束線程,釋放函數的資源,並在最後阻塞,直到其他線程使用pthread_join函數等待它。然後將*retval的值傳遞給**thread_return.由於這個函數釋放所以的函數資源,所以retval不能夠指向函數的局部變量。
pthread_join和wait調用一樣用來等待指定的線程。下面我們使用一個實例來解釋一下使用方法.在實踐中,我們經常要備份一些文件。下面這個程序可以實現當前目錄下的所有文件備份。
參考資料
CSDN.CSDN [引用時間2018-1-9]
用C語言在windows或者Linux上面,編寫一個多線程程序
#includestdio.h
#includestdlib.h
#includewindows.h
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
int *pt=(int*)lpParam;
printf(“I am tread %d\r\n”,*pt);
}
int main()
{
const int Count=4;
int datas[Count];
DWORD dwThreadId[Count];
HANDLE hThread[Count];
int i;
for(i=0;iCount;i++)
{
datas[i]=i+1;
hThread[i]=CreateThread(NULL,0,ThreadProc,datas[i],0,dwThreadId[i]);
}
WaitForMultipleObjects(Count,hThread,TRUE,INFINITE);
for(i=0;iCount;i++)
{
CloseHandle(hThread[i]);
}
system(“PAUSE”);
return EXIT_SUCCESS;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/237553.html