![]() can change its size at runtime) but usually doesn't incur any additional costs when accessed like a normal array. Another alternative would be to use the arguably most common standard container, std::vector, which is completely dynamic (i.e. Let's see whether any of these points apply to your code. Why is allocating memory manually unsafe? The most simple reason is: You might forget to free it (in which case you cause a memory leak), or accidentally free it twice (in which case you cause undefined behavior), or use a pointer to manually allocated memory after free (also causing undefined behavior). You should be able to compile with: g++ -std=c++11 -g -o circularptrqueuetest main.cpp -lpthreadĪs to question 1: Yes, what you are doing is unsafe. Std::cout << "Reader Null Count: " << readerNullCount << " Writer Null Count: " << writerNullCount << std::endl Std::thread reader_thread(reader, std::ref(struct_circular_queue_mt), std::ref(readerNullCount)) Std::thread writer_thread(writer, std::ref(struct_circular_queue_mt), std::ref(writerNullCount)) Int readerNullCount = 0, writerNullCount = 0 Std::cout timestamp struct_circular_queue_mt Struct data* popped_value = cptr_q.PopAndGetPtrToData() Void reader(CircularPtrQueue& cptr_q, int& readerNullCount) Void writer(CircularPtrQueue& cptr_q, int& writerNullCount) Allocate enough memory in advance for the queue. Used for tracking in which locations of A pointer to an array of the templated type. If my queue isn't threadsafe, how may I break it? ![]() Is my queue threadsafe if the push operation is limited to one of the threads and pop to another one? What am I missing here? In the sample test run in the implementation file below it performs fine. Is my implementation inherently unsafe in some way? Are there any other simpler way of doing this? I know this is extremely unorthodox, but for me memory is not a constraint and I need speed and avoid expensive copies. My strategy of copying pointers rather than data by allocating all the memory initially in the constructor and then just copying pointers rather than entire structures during push and pop operations. Mostly I am trying to get feedback about two things: By "zero copy" I mean no copying of data (mostly C++ structures) will take place during push and pop operations. there is a push-thread and a pop-thread and the push-thread can't pop and vice versa). One thread is limited to one action (i.e.
0 Comments
Leave a Reply. |