Linux多執行緒基礎學習(八)私有資料
/*============================================================================ // Name : thread_privateData.cpp // Author : Ryan // Version : // Copyright : zjut // Description : 測試執行緒私有資料 //============================================================================*/ #include <iostream> #include <stdio.h> #include <pthread.h> //#include "../errors.h" using namespace std; struct private_tag{ pthread_t thread_id; char *thread_name; }; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_key_t key; int count = 0; void *key_get(void *arg) { void *value = NULL; int status; value = pthread_getspecific(key); if(value == NULL){ value = new private_tag;//建立資料的動態記憶體 //初始化資料 ((private_tag *)value)->thread_id = pthread_self(); ((private_tag *)value)->thread_name = (char *)arg; if(value == NULL){ cout<<"new"<<endl; } cout<<pthread_self()<<" value: "<<value<<endl; status = pthread_setspecific(key, value); if(status != 0){ cout<<status<<"pthread_setspecific"<<endl; } } return value; } void *thread_routine(void *arg) { private_tag *value = NULL; value = (private_tag *)key_get(arg); //核對執行緒資料 cout<<"thread_id:"<<value->thread_id<<" thread_name:"<<value->thread_name<<endl; //核對key值 cout<<(value->thread_id)<<" key: "<< pthread_getspecific(key)<<endl; sleep(1); return 0; } //解構函式 void destructor(void *arg) { private_tag *_private = (private_tag *)arg; int status; printf("\"%s\" exiting......\n", _private->thread_name); // free(arg); delete (private_tag*)arg; status = pthread_mutex_lock(&mutex);//lock if(status != 0){ cout<<"mutex lock erro"<<status<<endl; } count--; if(count <= 0) { status = pthread_key_delete(key); if(status != 0){ cout<<"pthread_delete erro"<<status<<endl; } printf("key deleted......\n"); } status = pthread_mutex_unlock(&mutex);//unlock if(status != 0){ cout<<"mutex unlock erro"<<status<<endl; } } int main(int argc, char **argv) { int status; pthread_t thread1, thread2; private_tag *value = NULL; status = pthread_key_create(&key, destructor); if(status != 0){ cout<<"pthread_key_created"<<status<<endl; } count = 2; // value = (private_tag *)key_get(); // value->thread_id = pthread_self(); // value->thread_name = "main thread"; char *thread_1 = "thread 1"; char *thread_2 = "thread 2"; status = pthread_create(&thread1, NULL, thread_routine, thread_1); if(status != 0){ cout<<"create thread1"<<status<<endl; } status = pthread_create(&thread2, NULL, thread_routine, thread_2); if(status != 0){ cout<<"create thread2"<<status<<endl; } pthread_join(thread1, NULL); cout<<"thread1 exited"<<endl; pthread_join(thread2, NULL); cout<<"thread2 exited"<<endl; pthread_exit(NULL); }
相關推薦
Linux多執行緒基礎學習(八)私有資料
/*============================================================================ // Name : thread_privateData.cpp // Author : Ryan // Version
java多執行緒快速入門(八)
設定執行緒優先順序:join() package com.cppdy; class MyThreadA extends Thread{ MyThreadB b; public MyThreadA(MyThreadB b) { this.b=b; }
Java多執行緒-基礎篇(一)
多執行緒---基礎篇,本文主要針對多執行緒的基礎進行復習,內容包括執行緒的理解,建立方式,Thread類函式理解; 1、執行緒的理解:OS系統角度:每個執行的程式都會被作業系統建立對應的程序(包括分配的資源、PCB等),程序是作業系統分配資源的基本單位(理解:程式執行需要空間,作業系統建立對應程序時
多執行緒程式設計學習(1)物件及變數的併發訪問
程序:計算機中的程式關於某資料集合上的一次執行活動,是系統進行資源分配和排程的基本單位,是作業系統結構的基礎。 執行緒:在程序中獨立執行的子任務。 在java中以下3種方法可以終止正在執行的執行緒: 1) 使用退出標誌,使執行緒正常退出,也就是當run方法完成後執行緒終
多執行緒基礎知識(一) 執行緒的概念及建立任務與執行緒
執行緒的概念及建立任務與執行緒 引言 多執行緒使得程式中的多個任務可以同時執行。 java的重要功能之一就是內部支援多執行緒————在一個程式中運行同時執行多個任務。在許多程式設計語言中,多執行緒都是通過呼叫依賴於系統的過程或函式來實現的。在本文中,將介紹執行緒的該
Linux程式設計 多程序,多執行緒求解PI(圓周率)
題目: 連結 多程序: #include <unistd.h> #include <stdio.h> #include <stdlib.h> #define n 100000000.0 int main() { i
多執行緒基礎二(執行緒的啟動、終止,執行緒面臨的三種問題)
一、執行緒的啟動、終止方式 啟動: start native(呼叫外部介面啟動) 終止: stop(類似kill,暴力終止) interrupt 中斷的方式 通過指令的方式 volatile boolean stop
單執行緒的弊端,多執行緒基礎學習
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using
Linux多執行緒Pthread學習小結
簡介POSIX thread 簡稱為pthread,Posix執行緒是一個POSIX標準執行緒.該標準定義內部API建立和操縱執行緒.作用 執行緒庫實行了POSIX執行緒標準通常稱為pthreads.pthreads是最常用的POSIX系統如Linux和Unix,而微軟Wind
Java多執行緒乾貨系列—(一)Java多執行緒基礎
多執行緒併發程式設計是Java程式設計中重要的一塊內容,也是面試重點覆蓋區域,所以學好多執行緒併發程式設計對我們來說極其重要,下面跟我一起開啟本次的學習之旅吧。 正文 執行緒與程序 1 執行緒:程序中負責程式執行的執行單元 執行緒本身依靠程式進行執行 執行緒是程式中的順序控制流,只能使用分配給程式的資源和環
Linux多執行緒程式設計學習(1)--執行緒的概念和執行緒控制
Linux多執行緒程式設計學習總結 一.執行緒的概念 1.什麼是執行緒? 2.程序和執行緒的區別 3.程序和執行緒的關係 4.執行緒的優缺點 4.1 優點 4.2 缺點 5.執行
Linux執行緒入門學習(一)-認識執行緒
1. 執行緒的概念 執行緒,又稱輕量級程序,是程序中執行運算的最小單位,是程序中的一個實體,是被系統獨立排程和分派的基本單位,主要有以下特點: 1)執行緒自己不擁有系統資源,只擁有一點在執行中必不可少的資源; 2)與同屬一個程序的其它執行緒共享程序所擁有的全
多執行緒基礎學習十二:概念瞭解-重入鎖、獨佔鎖與共享鎖、公平鎖與非公平鎖
前面瞭解了J.U.C中兩個重要的類或介面,其它的實現類都是以此為基礎的,現在還需要了解一下一些鎖的概念,有助於後面學習一些實現類或介面。 重入鎖 在併發中,無論是synchronized還是lock也好,內部都有重入的特性,而且特性的含義是一樣的。 重入(
linux環境下ssl多執行緒程式設計例項(整理)
服務端: #include <stdio.h> #include <stdlib.h> #include <memory.h> #include <errno.h> #ifndef _WIN32 #include <
多執行緒詳解(二)
多執行緒詳解(二) 在正式介紹執行緒建立的第二種方法之前,我們接著多執行緒詳解(一),講一下:對執行緒的記憶體圖、執行緒的狀態,為下面的學習打下基礎,小夥伴們不要急喲!! 一、多執行緒執行的記憶體圖(ps.博主沒有找到合適的畫圖工具,歡迎大神們貢獻啊) class pers
多執行緒詳解(一)
[多執行緒詳解(一)](http://www.neilx.com) 一、概念準備 1、程序 (1)直譯:正在進行中的程式 (2)解釋:執行一個程式時,會在記憶體中為程式開闢空間,這個空間就是一個程序。 (3)注意:一個程序中不可能沒有執行緒,只有有了執行緒才能執行; 程序只
python基礎學習(八)元組
元組的定義 Tuple(元組)與列表類似,不同之處在於元組的 元素不能修改 元組 表示多個元素組成的序列 元組 在 Python 開發中,有特定的應用場景 用於儲存 一串 資訊,資料 之間使用 , 分隔 元組用 () 定義 元組的 索引 從 0 開始
java多執行緒快速入門(四)
通過匿名內部類的方法建立多執行緒 package com.cppdy; //通過匿名內部類的方法建立多執行緒 public class ThreadDemo2 { public static void main(String[] args) { new Thread(ne
java多執行緒快速入門(六)
多執行緒應用例項(批量傳送簡訊) 1、建立實體類 package com.cppdy; public class UserEntity { private int id; private String name; public int g
java多執行緒快速入門(七)
什麼是守護執行緒 守護執行緒是為使用者執行緒服務的這麼一個執行緒,主執行緒結束,守護執行緒也結束 package com.cppdy; class MyThread3 extends Thread{ @Override public void run() {