std::list access objects from another thread

Hi, I have the following code below. I am getting a memory access violation when accessing cmd->query_string in the loop function. The loop() function is running in another thread. The cmd object appears to be destroyed after calling the send_command function. How do I create an object on the heap and access the query_string. Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

std::list<my_command_message_que*> my_command_que;

void loop(){

	if(my_command_que.size() > 0){

	    my_command_message_que * cmd = my_command_que.front();
            std::cout << cmd->query_string;

	    my_command_que.pop_front();
	
	}

}
extern "C" __declspec(dllexport) void send_command(char * query_string) {
	my_command_message_que * cmd = new my_command_message_que();	
	cmd->query_string = query_string;	
	my_command_que.push_back(cmd);
}
> I am getting a memory access violation when accessing cmd->query_string in the loop function.

The problem is not with accessing the the object; standard library containers (except std::vector<bool> ) are thread safe for simultaneous read access of the same object, and simultaneous modify access of different objects.

Here, you have modify access of the same object from multiple threads; you have to provide a synchronization mechanism from user code.

See: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
Thanks I'll looking into thread syncing.
Topic archived. No new replies allowed.