執行緒的基本使用

2022-05-10 14:47:23 字數 3755 閱讀 8672

執行緒

執行緒的絕大多數函式名都以pthread_開頭,我們可以通過如下幾步來執行緒函式庫。

1:定義巨集_reentrant;

2:在程式中包含標頭檔案「pthread.h」;

3:在編譯程式時需要用選項-lpthread來連線線程庫;

步驟說明:

1:我們通過定義巨集_reentrant來告訴編譯器我們需要可重入功能。

2:標頭檔案pthread.h中有我們需要的執行緒函式。

#include

int pthread_create(pthread_t *thread, pthread_attr_t *attr, void*(*start_routine)(void*), void *arg);

void pthread_exit(void* retval);

int pthread_join(pthread_t th ,void **thread_return);

pthread_t pthread_self();

#include

#include

#include

#include

void *thread_function(void *arg);

int run_now = 1;

char message = "hello world";

int main()

pthread_t tid;

pid_t pid;

tid = pthread_self();

pid = getpid();

printf("thread_id and process_id is :[%lu]@[%lu]\n",tid,pid);

printf("\nwaiting for thread to finish...\n");

res = pthread_join(a_thread, &thread_result);

if (res != 0)

printf("thread joined\n");

exit(exit_success);

}void *thread_function(void *arg)

編譯上面的程式的命令是:

cc -d_reentrant -lpthread thread_test.c -o thread_test

訊號量訊號量函式的名字都以sem_開頭。

#include

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

int sem_wait(sem_t *sem);

int sem_post(sem_t *sem);

int sem_destory(sem_t *sem);

#include

#include

#include

#include

#include

#include

void *thread_function(void *arg);

sem_t bin_sem;

#define work_size 1024

char work_area[work_size];

int main()

res = pthread_create(&a_thread, null, thread_function, null);

if (res != 0)

printf("input some text. enter 'end' to finish\n");

while(strncmp("end", work_area, 3) != 0)

printf("\nwaiting for thread to finish...\n");

res = pthread_join(a_thread, &thread_result);

if (res != 0)

printf("thread joined\n");

sem_destroy(&bin_sem);

exit(exit_success);

}void *thread_function(void *arg)

pthread_exit(null);

}互斥量

#include

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr);

int pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_unlock(pthread_mutex_t *mutex);

int pthread_mutex_destroy(pthread_mutex_t *mutex);

舉例:#include

#include

#include

#include

#include

#include

void *thread_function(void *arg);

pthread_mutex_t work_mutex; /* protects both work_area and time_to_exit */

#define work_size 1024

char work_area[work_size];

int time_to_exit = 0;

int main()

res = pthread_create(&a_thread, null, thread_function, null);

if (res != 0)

pthread_mutex_lock(&work_mutex);

printf("input some text. enter 'end' to finish\n");

while(!time_to_exit)

else }}

pthread_mutex_unlock(&work_mutex);

printf("\nwaiting for thread to finish...\n");

res = pthread_join(a_thread, &thread_result);

if (res != 0)

printf("thread joined\n");

pthread_mutex_destroy(&work_mutex);

exit(exit_success);

}void *thread_function(void *arg)

}time_to_exit = 1;

work_area[0] = '\0';

pthread_mutex_unlock(&work_mutex);

pthread_exit(0);

}執行緒的屬性

#include

int pthread_attr_init(pthread_attr_t *attr);

int pthread_attr_destroy(pthread_attr_t *attr);

分離執行緒

分離執行緒之後,新執行緒已經脫離原執行緒,不再能用pthread_join ()進行合併。

#include

int pthread_detach(pthread_t thread);

取消執行緒

#include

int pthread_cancel(pthread_t thread);

執行緒 的基本使用

1 建立執行緒的兩個基本 有 繼承 thread,runnable thread public class test3 extends thread catch interruptedexception e 測試 test3 test new test3 test.start 執行執行緒 try c...

執行緒的基本使用方式

使用threading的thread模組來申明函式使用多執行緒來執行 使用threading的thread庫來初始化執行緒 使用target來指明函式,不要加括號 引數的傳遞寫在 args裡面 以tuple的形式 但是這個函式有乙個問題,他不會執行,因為main函式有乙個隱藏的主線程,所以雖然我們n...

執行緒池的基本使用

import time 使用單執行緒序列方式執行 def get page str time.sleep 2 name list aa bb cc dd start time time.time for i in range len name list get page name list i en...