File: enqueue_dequeue_many.cpp

package info (click to toggle)
concurrentqueue 1.0.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,648 kB
  • sloc: cpp: 37,303; makefile: 88; ansic: 67; python: 46; sh: 18
file content (62 lines) | stat: -rw-r--r-- 1,168 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// ©2013 Cameron Desrochers.
// Distributed under the simplified BSD license (see the LICENSE file that
// should have come with this file).

#include "model-checker/include/threads.h"
#include "corealgo.h"

void consumer(void* param)
{
	int id = *(int*)param;
	int& dequeueCount = *(int*)param;
	dequeueCount = 0;
	
	int last = 0;
	
	int element;
	bool success = try_dequeue(element);
	if (success) {
		MODEL_ASSERT(element > last);
		last = element;
		++dequeueCount;
	}
	success = try_dequeue(element);
	if (success) {
		MODEL_ASSERT(element > last);
		last = element;
		++dequeueCount;
	}
}

void producer(void* param)
{
	for (int i = 1; i <= 8; ++i)
		enqueue(i);

	consumer(param);
}

int user_main(int, char**)
{
	init();
	
	// Start out as thread IDs, but are re-used by the threads
	// to indicate the number of elements each one dequeued
	int w = 1, x = 2, y = 3, z = 4;
	
	thrd_t a, b, c, d;
	
	thrd_create(&a, &producer, &w);
	thrd_create(&b, &consumer, &x);
	thrd_create(&c, &consumer, &y);
	thrd_create(&d, &consumer, &z);
	
	thrd_join(a);
	thrd_join(b);
	thrd_join(c);
	thrd_join(d);
	
	MODEL_ASSERT(w + x + y + z + size_approx() == 8);
	
	return 0;
}