How many BUGS, all originate from copy and paste!

In addition to data sharing between multiple lines, there is also the problem of semaphore sharing. The code below is problematic.

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.
}

Because psem is a shared memory semaphore, if the name is the same, the psem in the two classes is the same. This is a very low-level bug, often caused by copying and pasting without paying attention to the same name.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325457484&siteId=291194637