linux多線程demo

來源:互聯網
上載者:User
============================================================博文原創,轉載請聲明出處電子咖啡(原id藍岩)============================================================

from:http://programming-in-linux.blogspot.com/2008/03/multithreading-example-in-cc-using.html

在網上找到了一個linux多線程的demo,原部落格已經刪除了,我做了些注釋編輯一下,希望對讀者有用。

demo展示了如何建立線程,如何初始化,如何傳遞參數等。

thread_example.cc

#include <pthread.h>#include <errno.h>#include <signal.h>#include <stdio.h>#include <stdlib.h>#include <time.h>#include <unistd.h>#include <string.h>//define a struct to pass parameterstypedef struct my_struct {int data;int random_number;}my_struct_t;/*=== define the thread method ======*/void  * thread(void * str) {my_struct_t *local_str = (my_struct_t *)str;int i;for (i=0;i<5;i++) { sleep(local_str->random_number); printf("Hello! I am thread %d, i was sleeping for %d seconds\n",  local_str->data,  local_str->random_number);}}/*===========================================================================*//*=== main method ====*/int main(int argc, char **argv){sigset_t oSignalSet;//initialise and empty a signal setsigemptyset(&oSignalSet);sigaddset(&oSignalSet, SIGINT);sigaddset(&oSignalSet, SIGABRT);sigaddset(&oSignalSet, SIGQUIT);pthread_sigmask(SIG_BLOCK, &oSignalSet, NULL);pthread_t iThreadId;//// Declare variable to hold seconds on clock.//time_t seconds;//// Get value from system clock and// place in seconds variable.//time(&seconds);//// Convert seconds to a unsigned// integer.//srand((unsigned int) seconds);my_struct_t st1;my_struct_t st2;my_struct_t st3;st1.data = 1;st2.data = 2;st3.data = 3;st1.random_number = rand()%10;st2.random_number = rand()%10;st3.random_number = rand()%10;printf("Parent: Creating and calling threads...\n");int iReturnValue1 = pthread_create(&iThreadId, NULL, &thread, (void *)&st1);int iReturnValue2 = pthread_create(&iThreadId, NULL, &thread, (void *)&st2);// pthread_cancel(iThreadId);int iReturnValue3 = pthread_create(&iThreadId, NULL, &thread, (void *)&st3);//cancel pThreadpthread_cancel(iThreadId);if (iReturnValue1 < 0 || iReturnValue2 < 0 || iReturnValue2 < 0) {  printf("error creating thread '%s'.\n", strerror(errno));}int iSignalNumber;sigwait(&oSignalSet, &iSignalNumber);printf("exit after receiving signal #%d.\n", iSignalNumber);exit(0);}

編譯運行:

ericyang$ g++ -o thread_example.o thread_example.cc ericyang$thread_example.o 

輸出結果:

Parent: Creating and calling threads...Hello! I am thread 2, i was sleeping for 1 secondsHello! I am thread 2, i was sleeping for 1 secondsHello! I am thread 2, i was sleeping for 1 secondsHello! I am thread 1, i was sleeping for 4 secondsHello! I am thread 2, i was sleeping for 1 secondsHello! I am thread 2, i was sleeping for 1 secondsHello! I am thread 1, i was sleeping for 4 secondsHello! I am thread 1, i was sleeping for 4 secondsHello! I am thread 1, i was sleeping for 4 secondsHello! I am thread 1, i was sleeping for 4 seconds

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.