多少BUGS,盡源於拷貝粘貼!

多線之間的數據共享,除此以外,還有信號量共享的問題。下面的代碼是有問題的。this

class cthread1{
	void start();
	void thread_proc();

private:
	sem_t* psem;
};

static void *do_thread1(void* data){
	cthread1* ptread = (cthread1*)data;
	pthread->thread_proc();
}

void cthread1:start(){
	psem = sem_open("http", O_CREAT, 0644, 1);
	pthread_t thread_t;
	pthread_create(&thread_t, NULL, do_thread1, this);
	pthread_detach(thread_t);
}

void cthread1:thread_proc(){
	//use psem,do somthing.
}

class cthread2{
	void start();
	void thread_proc();

private:
	sem_t* psem;
};

static void *do_thread2(void* data){
	cthread2* ptread = (cthread2*)data;
	pthread->thread_proc();
}

void cthread2:start(){
	psem = sem_open("http", O_CREAT, 0644, 1);
	pthread_t thread_t;
	pthread_create(&thread_t, NULL, do_thread2, this);
	pthread_detach(thread_t);
}

void cthread2:thread_proc(){
	//use psem,do somthing.
}

由於psem是個共享內存信號量,名字同樣的話,兩個類裏的psem是同樣的,這是個很低級的BUG, 經常因爲拷貝粘貼而沒有注意同名致使。code

相關文章
相關標籤/搜索